cURL takes too long to load

前端 未结 2 715
臣服心动
臣服心动 2021-02-10 05:51

I am calling a REST endpoint in PHP using cURL to fetch some JSON data:



        
2条回答
  •  执念已碎
    2021-02-10 06:01

    I found the solution to my problem. As I had mentioned in the question, the service was loading the fastest in browsers. So, I checked the 'Request Headers' of the request in the 'Network' tab of Google Chrome Inspector. I copied those headers and used them in my cURL request in PHP. After scraping those headers I found that all I needed to do was to add an Accept-Encoding header. I passed a value of gzip like so:

    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    

    but setting it to an empty string also works.

    curl_setopt($ch, CURLOPT_ENCODING, '');
    

    According to the php.net manual for CURLOPT_ENCODING:

    The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent.

提交回复
热议问题