问题 访问极客时间内容出错

    public function gk_get()
    {
        $courseId = 185;

        $url = 'http://time.geekbang.org/serv/v1/article';
        //初始化
        $curl = curl_init();
        //设置抓取的url

        $headerOptions = [
            "Accept: application/json, text/plain, */*",
            "Accept-Encoding: gzip, deflate, br",
            "Accept-Language: zh-CN",
            "Connection: keep-alive",
            "Content-Length: 58",
            "Content-Type: application/json",
            "Cookie: gksskpitn=3159b21b-0651-4bf0-b2df-5d04d3c5d461; _ga=GA1.2.452630884.1585702530; _gid=GA1.2.1543509748.1585702530; LF_ID=1585702533807-2608779-9979434; GCID=b45fa5c-e31f2e2-6e9b07d-f85a389; GRID=b45fa5c-e31f2e2-6e9b07d-f85a389; GCESS=BAcE2YitrQoEAAAAAAsCBAAFBAAAAAABBE2SFwAGBLXkg34EBAAvDQAJAQEIAQMMAQEDBJoKhF4CBJoKhF4-; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1585705098,1585705866,1585711765,1585711782; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1585711784; SERVERID=1fa1f330efedec1559b3abbcb6e30f50|1585711825|1585702519; _gat=1",
            "Host: time.geekbang.org",
            "Origin: https://time.geekbang.org",
            "Referer: https://time.geekbang.org/column/article/185",
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36 Maxthon/5.3.8.2000'
        ];
        $postOptions = [
            "id" => $courseId,
            "include_neighbors" => false,
            "is_freelyread" => true
        ];

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSLVERSION, 1);

        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerOptions);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postOptions));


        $proxy = '';
        curl_setopt($curl, CURLOPT_PROXY, $proxy);
        curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
        //执行命令
        $data = curl_exec($curl);
        $errno = curl_errno($curl);
        //关闭URL请求
        curl_close($curl);
        var_dump($data);
        var_dump($errno);
    }

使用账号登陆,通过浏览器获取header信息,替换代码中的header参数,获取并不能抓取,

你可能感兴趣的:(php)