【问题标题】:PHP curl and getting HTTPS site's html source codePHP curl和获取HTTPS站点的html源代码
【发布时间】:2016-09-02 14:32:33
【问题描述】:

我为获取 https 页面内容编写了这段代码,但我无法成功。

 <?php

    function bot($url)
    {
        $header ="Host: tr-tr.facebook.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0\r\n
Accept: */*
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate, br";
        $options = array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => TRUE,
                CURLOPT_PORT => 443,
                CURLOPT_SSL_VERIFYPEER => true,
                CURLOPT_SSL_VERIFYHOST => 2,
                CURLOPT_CAINFO => "C:\\xampp\\htdocs\\curl-ca-bundle.crt",
                CURLOPT_HTTPHEADER => explode("\r\n",$header)

            );
        $ch = curl_init();
        curl_setopt_array($ch, $options);

        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
        echo bot("https://tr-tr.facebook.com");
    ?>

当我运行该代码时,它会返回该代码。 "��0#a�jȌ#�#.3�j�##u�.����/#cw@,�q=落���K"

我不想使用CURLOPT_SSL_VERIFYPEER = false。我想用 curl 进行 https 握手..

【问题讨论】:

  • 看起来响应是Brotli 编码的。要么尝试协商不同的编码,要么自己解码。 Content-Encoding: br
  • 当我删除接受编码标头时,我得到纯文本 requestable.pieterhordijk.com/eZd7o 或者如果你支持,你可以协商 gzip,因为服务器很乐意为你提供 gzip。
  • 是的,这行得通。谢谢。

标签: php curl https


【解决方案1】:

也许您应该删除CURLOPT_RETURNTRANSFER 或妥善处理它。

来自手册

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

你真的想要这个吗?!

> CURLOPT_RETURNTRANSFER

> TRUE to return the transfer as a string of the
> return value of curl_exec() instead of outputting it out directly.

【讨论】:

  • 感谢您的回答。我试过 CURLOPT_RETURNTRANSFER = false 但输出是一样的。你有没有试过用 curl 连接 https,而不是 curlopt_ssl_verifypeer=false?我想知道为什么我会得到这个输出?
  • 那么设置CURLOPT_VERBOSE 看看会发生什么。我也可以是压缩的东西。所以只需删除整个 Accept... 标头并使用详细信息进行尝试。
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 2017-08-31
  • 1970-01-01
相关资源
最近更新 更多