PHP curl并获取HTTPS站点的html源代码

时间:2016-09-02 14:32:34

标签: php curl https

我为获取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#ajȌ## 3J ##ù./# CW @,Q =ߓķ。” &LT;˞#%# t [ d :| s@#$! ( M ߛ#w'#u“我的错误在哪里?为什么要归还这些角色?

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

1 个答案:

答案 0 :(得分:0)

也许你应该删除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.