通过fsockopen()方法从中国*网获取双色球历史中奖数据

public function history_draw()
{
    $fp = fsockopen('www.cwl.gov.cn', 80, $errno, $errstr, 60) or die('fsockopen失败:' . $errno . $errstr);

    $out = <<<REQ
GET http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=2003-01-01&dayEnd=2019-05-09&pageNo=1 HTTP/1.1
Host: www.cwl.gov.cn
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Referer: http://www.cwl.gov.cn/kjxx/ssq/kjgg/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: UniqueID=sI611WdNxE1ycO4W1557392848234; Sites=_21; _ga=GA1.3.1189624613.1556598902; 21_vq=11; _gid=GA1.3.1590231225.1557392849; _gat_gtag_UA_113065506_1=1
REQ;
    $out .= "\r\n\r\n"; // !!!必须有两个换行,否则会一直报超时!!!
    fwrite($fp, $out);
    $data = '';
    while (!feof($fp)) {
        $data .= fgets($fp, 128);
    }
    fclose($fp);

    $result = substr($data, strpos($data, '{'));
    $result = json_decode($result, true);
    foreach ($result['result'] as $value) {
        $red = explode(',', $value['red']);
        $new[] = array(
            'issue' => $value['code'],
            'draw_date' => substr($value['date'], 0, 10),
            'blue' => $value['blue'],
            'red_1' => $red[0],
            'red_2' => $red[1],
            'red_3' => $red[2],
            'red_4' => $red[3],
            'red_5' => $red[4],
            'red_6' => $red[5],
        );
    }

    $this->db->insert_batch('dual_ball', $new);
}

 

上一篇:PHP通过远程调用获取页面的内容


下一篇:通过sftp php连接到服务器