袁来如此的工作笔记
袁来如此的工作笔记
竹杖芒鞋轻胜马,谁怕? 一蓑烟雨任平生。

PHP curl抓取https协议接口数据

浏览量:327

how:

找到请求的头部,方法:

浏览器开发者工具——网络——XHR——点击文件,查看请求头。

如果没有找到文件,就刷新页面再看看。


详细代码:

public function catchData(){
        $url="https://www.xxx.com/api/data.php?pair_id=8830&pair_id_for_news=8830&chart_type=area&pair_interval=3600&candle_count=120&events=yes&volume_series=yes&period=";
        $headers=[
            \Accept:text/html,application/xhtml+xml,application/xml,application/json, text/javascript, ; q=0.01\,
            \Accept-Encoding:gzip, deflate, br\,
            \Accept-Language:en-US,en;q=0.5\,
            \Cache-Control:max-age=0\,
            \Connection:keep-alive\,
            \cookie:StickySession=id.22115874781.5…gat=1; _gat_allSitesTracker=1\,
            \Host:cn.investing.com\,
            \Referer:https://www.xxx.com\,
            \User-Agent:Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/65.0\,
            \X-Requested-With:XMLHttpRequest\,
        ];
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//指定头部参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//结果不输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_ENCODING, \gzip,deflate\);//这个是解析gzip,deflate压缩的内容

        $data = curl_($ch);//运行curl
        curl_close($ch);

        print_r($data);
    }



打赏