cURL takes too long to load

前端 未结 2 717
臣服心动
臣服心动 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.

    0 讨论(0)
  • 2021-02-10 06:05
    $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, "set ur url");
                        curl_setopt($ch, CURLOPT_ENCODING , "gzip"); 
                        curl_setopt($ch, CURLOPT_ENCODING, '');    
    
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        $response  = curl_exec($ch);
                        curl_close($ch);
    

    Please check this example

    0 讨论(0)
提交回复
热议问题