php - 如何在 PHP 中使用 curl 发送接受编码 header

标签 php curl gzip

如何在 PHP 中使用 curl 发送 Accept Encoding header

  $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);

如何解压响应

最佳答案

您可以使用 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

或者,您可以发送 header :
$headers = array(
        'Accept: text/plain'
    );

强制响应 txt

关于php - 如何在 PHP 中使用 curl 发送接受编码 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37192693/

相关文章:

javascript - Deobfuscate JS 混淆脚本用于在服务器端执行它

php - 模拟来自不同客户端的不同行每秒 400 次更新

c - 如何在 2013 zlib API 接口(interface)中模仿(被黑)1998 uncompress() 上的 "use_crc"标志?

web - 如何为 Web 和 Amazon CloudFront 手动 gzip 文件

PHP - IF 语句普通用户或管理员有权访问

php - 将 css 的 url 站点更改为一致的

PHP在多维数组上嵌套foreach

php - PHP 简单 HTML DOM 解析器的字符编码问题

javascript - Curl 获取 Ajax 数据的问题

ruby-on-rails - Rails 3.2 + Heroku + S3 + CloudFront : Not serving gzip css js