php 的 curl请求

<?php

namespace app\controller;

use app\BaseController;
use GatewayClient\Gateway;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use think\facade\Log;
use GuzzleHttp\Psr7\Request;


class Index extends BaseController
{
    public function index()
    {
        //echo $this->request->post();
        Gateway::$registerAddress = '127.0.0.1:1238';
        $res = Gateway::sendToAll(json_encode(['name' => 'chenlong', 'age' => 100]));
        //print_r($res);

        $data = file_get_contents("php://input");
        Log::write(input('param.'), 'notice1');
        Log::write(input('get.'), 'notice2');
        Log::write(input('post.'), 'notice3');
        Log::write($_POST, 'notice4');
        Log::write($_REQUEST, 'notice5');
        Log::write($data, 'notice6');
        return json($data);
        //echo 3333;

    }

    public function curl()
    {
        $params = [
            'key' => '*****',
            'userid' => 'yemeishu'
        ];


        $client = new Client(['timeout' => 1.0]);
        $options = json_encode($params, JSON_UNESCAPED_UNICODE);
        $data = [
            'body' => $options,
            //'headers' => ['content-type' => 'application/json']
            //'headers' => ['content-type' => 'text/html']
        ];

        // 发送 post 请求
        //$url= 'https://baidu.com';
        $url = 'http://127.0.0.1/index.php';
        //$url= 'http://share.topdinghui.com/index.php';
        //$response = $client->post('http://47.112.137.157:8090/api.php/platform/list', $data);
        $response = $client->post($url, $data);
        //print_r($response);
        $callback = $response->getBody()->getContents();
        print_r($callback);
    }


    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    public function test()
    {
        $urls=[
            'http://127.0.0.1/index.php',
            'http://127.0.0.1/index1.php',
            'http://share.topdinghui.com/index.php',
            'http://47.112.137.157:8090/api.php/platform/list',
        ];
        $client = new Client();

        $requests = function ($urls) {
            $uri = 'http://127.0.0.1/index.php';
            //$uri = 'http://47.112.137.157:8090/api.php/platform/list';


            foreach ($urls as  $v){
                yield new Request('GET', $v, [
                    'headers' => ['content-type' => 'application/xhtml+xml'],
                    'timeout' => 5
                    //'decode_content' => false
                ]);
            }
        };

        $pool = new Pool($client, $requests($urls), [
            'concurrency' => 5,
            'fulfilled' => function ($response, $index) {
                $body = $response->getBody()->getContents();
                print_r($body);
                echo 'successful --' . $index . PHP_EOL;
                echo '=========================================================='. PHP_EOL;
                // this is delivered each successful response
            },
            'rejected' => function ($reason, $index) {
                echo 'failed --' . $index . PHP_EOL;
                // this is delivered each failed request
            },
        ]);

        // Initiate the transfers and create a promise
        $promise = $pool->promise();

        // Force the pool of requests to complete.
        $promise->wait();
    }


    //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
    protected function curl_request($url,$post='',$cookie='', $returnCookie=0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        if($returnCookie){
            list($header, $body) = explode("\r\n\r\n", $data, 2);
            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
            $info['cookie']  = substr($matches[1][0], 1);
            $info['content'] = $body;
            return $info;
        }else{
            return $data;
        }
    }

    public function testCurl(){
        $uri = 'http://127.0.0.1/index.php';
        echo $this->curl_request($uri);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值