php 爬虫 爬取12306接口

来源:互联网 发布:乐云记事关闭 数据怎么 编辑:程序博客网 时间:2024/05/23 14:18
public function actionGet_12306($session,$str){        $url="https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2016-07-05&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=AKY&purpose_codes=ADULT";    $this->getcurl($url);    return $this->renderPartial('index');}public function doget($url){    if(!function_exists('file_get_contents')) {        $optionget = array('http' => array('method' => "GET", 'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:https://kyfw.12306.cn/otn/leftTicket/init"));        $file_contents = file_get_contents($url, false , stream_context_create($optionget));    } else {        $ch = curl_init();        $timeout = 5;        $header = array(            'Accept:*/*',            'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3',            'Accept-Encoding:gzip,deflate,sdch',            'Accept-Language:zh-CN,zh;q=0.8,ja;q=0.6,en;q=0.4',            'Connection:keep-alive',            'Host:kyfw.12306.cn',            'Referer:https://kyfw.12306.cn/otn/leftTicket/init',        );        curl_setopt ($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 200);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');//如果数据是压缩处理,需要这个参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回        $file_contents = curl_exec($ch);        curl_close($ch);    }    $file_contents = json_decode($file_contents,true);    return $file_contents;}public static function getcurl($url){    $ch=curl_init($url);    $header = array(        'Accept:*/*',        'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3',        'Accept-Encoding:gzip,deflate,sdch',        'Accept-Language:zh-CN,zh;q=0.8,ja;q=0.6,en;q=0.4',        'Connection:keep-alive',        'Host:kyfw.12306.cn',        'Referer:https://kyfw.12306.cn/otn/leftTicket/init',    );    curl_setopt ($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 200);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');//如果数据是压缩处理,需要这个参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回 $output = curl_exec($ch) ; $json_str=json_decode($output,true); var_dump($output);die; /*return access_token*/ return $json_str;}
0 0
原创粉丝点击