加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – cURL返回二进制数据而不是html

发布时间:2020-12-13 21:52:55 所属栏目:PHP教程 来源:网络整理
导读:function curl($url) {$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/25.0.1");curl_setopt($ch,CURLOPT_RETURNTRANSFER,
function curl($url) {

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/25.0.1");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_COOKIE,'long cookie here');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$output = curl_exec($ch);
curl_close($ch);
return $output;

}

我正在喂它的原始网址是http://example.com/i-123.html,但如果我在浏览器中打开,我会被重定向到https://example.com/item-description-123.html(所以我添加了CURLOPT_FOLLOWLOCATION).

但是,此函数的输出是二进制数据.

1f8b 0800 0000 0000 0003 ed7d e976 db38
f2ef e7f8 2930 9ac9 d86e 9b92 b868 f3a2
3e5e 9374 67fb c7ee 74f7 e4e6 f880 2428
31a6 4835 172f 3dd3 8f74 3fde 17b8 f7c5
6e15 008a 8ba8 2db1 3ce9 25a7 dba4 4810
......

我该如何解决?我尝试添加

curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);

(从某处复制).没工作.

file_get_contents()给了我相同的输出.

解决方法

好吧,解决方案很可悲……

使用wget -S http://example.com我发现内容是压缩的(gzip).使用gunzip我成功提取了html.

还添加到我原来的PHP脚本中

curl_setopt($ch,CURLOPT_ENCODING,"");

它就像一个魅力.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读