Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Accept Encoding header with curl in PHP

Tags:

php

curl

gzip

How to send Accept Encoding header with with curl in PHP

  $data=json_encode($data);
$url = 'url to send';
$headers = array(
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);        
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSLVERSION, 4);
    $datas  = curl_exec($ch);
    curl_close($ch);

How to Decompress the response

like image 245
vellai durai Avatar asked May 12 '16 16:05

vellai durai


1 Answers

You can use CURLOPT_ENCODING:

curl_setopt($ch, 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.

http://php.net/manual/en/function.curl-setopt.php


Alternatively, you can send an header:

$headers = array(
        'Accept: text/plain'
    );

To force the response in text/plain

like image 164
Pedro Lobito Avatar answered Oct 06 '22 14:10

Pedro Lobito