PHP Guzzle包 用法

總結: 
1.GUZZLE包最終還是用的curl的curl_exec和curl_multi_exec去請求,可以添加自定義配置,這些配置最終還是會對應到 curl_setopt_array($easy->handle, $conf); 設置 
2.還有一個就是可以做一些請求前或者請求返回後的中間件。介入請求的過程中處理$stack = new HandlerStack(); push,handler進去 
//使用默認配置去構建客戶端
        $client = new Client();

        //設置CURL,使用去構建客戶端
        $client = new \GuzzleHttp\Client([
            'curl' => [
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => 1,
            ]
        ]);

        //代理設置
        $proxy = [
            //http域名代理
            'http' => config('app.fileserver_proxy') . ':' . config('app.fileserver_port'), // Use this proxy with "http"
            //https域名代理
            'https' => config('app.fileserver_proxy') . ':' . config('app.fileserver_port'), // Use this proxy with "https",
            //不需要代理的域名
            'no' => ''

        ];

        $response = $client->request('GET','https://www.baidu.com', [
            'proxy' => $proxy
                ]);


        //異步請求,要用wait才能返回
        $promise = $client->requestAsync('GET', 'https://www.baidu.com');
        $promise = $promise->then(
            function (ResponseInterface $res) {
                dd($res->getBody());
                echo $res->getStatusCode() . "\n";
            },
            function (RequestException $e) {
                dd($e);
                echo $e->getMessage() . "\n";
                echo $e->getRequest()->getMethod();
            }
        )->wait();
        dd($promise);


        function add_response_header($header, $value)
        {
                return function (callable $handler) use ($header, $value) {
                    return function (
                        RequestInterface $request,
                        array $options
                    ) use ($handler, $header, $value) {
                        $promise = $handler($request, $options)
                return $promise->then(
                    function (ResponseInterface $response) use ($header, $value) {
                        return $response->withHeader($header, $value);
                    }
                );
            }
        };
        }

        $stack = new HandlerStack();
        $stack->setHandler(new CurlHandler());
        $stack->push(add_response_header('X-Foo', 'bar'));
        $client = new Client(['handler' => $stack]);

 

 $conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
                return $body->read($length);
            };

讀取本地文件句柄流,提供POST數據, 可以解決那些大文件上傳的, 另外一種方案就是切割成多個小文件上傳。

番外:

可以設置CURLOPT_READFUNCTION 和CURLOPT_READDATA選項來爲POST提供數據,同時,不能再設置CURLOPT_POSTFIELDS選項。當使用callback函數來提供數據時,一定要使用大塊數據傳輸編碼(chunked transfer-encoding)或者用CURLOPT_POSTFIELDSIZE 或CURLOPT_POSTFIELDSIZE_LARGE選項設置數據大小。chunked transfer-encoding可以用CURLOPT_HTTPHEADER來設置字段。

private function applyBody(RequestInterface $request, array $options, array &$conf)
    {
        $size = $request->hasHeader('Content-Length')
            ? (int) $request->getHeaderLine('Content-Length')
            : null;

        // Send the body as a string if the size is less than 1MB OR if the
        // [curl][body_as_string] request value is set.
        if (($size !== null && $size < 1000000) ||
            !empty($options['_body_as_string'])
        ) {
            $conf[CURLOPT_POSTFIELDS] = (string) $request->getBody();
            // Don't duplicate the Content-Length header
            $this->removeHeader('Content-Length', $conf);
            $this->removeHeader('Transfer-Encoding', $conf);
        } else {
            $conf[CURLOPT_UPLOAD] = true;
            if ($size !== null) {
                $conf[CURLOPT_INFILESIZE] = $size;
                $this->removeHeader('Content-Length', $conf);
            }
            $body = $request->getBody();
            if ($body->isSeekable()) {
                $body->rewind();
            }
            $conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
                return $body->read($length);
            };
        }

        // If the Expect header is not present, prevent curl from adding it
        if (!$request->hasHeader('Expect')) {
            $conf[CURLOPT_HTTPHEADER][] = 'Expect:';
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章