How to send Accept Encoding header with curl in PHP
16,801
Solution 1
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
Solution 2
If you mean how to ungzip the response I did it like this:
<?php
....
$headers = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.3",
"Accept-Language:en-US;q=0.6,en;q=0.4",
"Connection: keep-alive",
);
....
$response = curl_exec($curl);
// check for curl errors
if ( strlen($response) && (curl_errno($curl)==CURLE_OK) && (curl_getinfo($curl, CURLINFO_HTTP_CODE)==200) ) {
// check for gzipped content
if ( (ord($response[0])==0x1f) && (ord($response[1])==0x8b) ) {
// skip header and ungzip the data
$response = gzinflate(substr($response,10));
}
}
// now $response has plain text (html / json / or something else)
Author by
vellai durai
Updated on June 23, 2022Comments
-
vellai durai almost 2 years
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
-
Mike about 8 yearsThe
Content-Type
header is sent from the server to the client. You're looking forAccept*
headers. (e.g.Accept
,Accept-Encoding
,Acccept-Language
). This tells the server what your user agent supports. -
Pedro Lobito about 8 years@Mike not exactly, the client can also send
Accept-Encoding
header requests. -
Mike about 8 years@PedroLobito Interesting. How does that differ from
Accept
? -
Pedro Lobito about 8 years
14.3 Accept-Encoding
- w3.org/Protocols/rfc2616/rfc2616-sec14.html -
Mike about 8 years@PedroLobito Sorry, I misread your first comment here. I thought you were saying the client can send
Content-Type
. From the link above,The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient
, so I was right; it can only be sent from the server to the client. -
Pedro Lobito about 8 years@mike here's a request send from my browser to this page - pastebin.com/GaTXDnfM - You can see clearly that me, the client, is requesting
Accept-Encoding:
-
Pedro Lobito about 8 yearsI misunderstood your first comment, we're both right... :)
-
-
dudeNumber4 over 7 yearsAccept: text/plain doesn't force anything. Results still HTML.