php通过登录后的cookie以及用户代理然后通过curl获取网页内容

function curl_get_https($url, $data=array(), $header=array(), $timeout=30){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, false);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);


    $response = curl_exec($ch);
    if($error=curl_error($ch)){
        die($error);
    }
    curl_close($ch);
    return $response;

}


//$list是通过数据库查询获取的一个包含公司名称的二维数组

foreach($list as $key=>$val)
{
    if($val['ok']==0)
    {
        $url="https://www.tianyancha.com/search?key=".urlencode($val['company']);
        $header = array();
        $header[] = "这里是登录天眼查之后复制下来的cookie";

        $header[]="User-Agent这里是复制下来的用户代理";

        $arr=[];
        $html  =  curl_get_https($url,$arr,$header);
        $regex="/<span class=\"overflow-width over-hide vertical-bottom in-block\" style=\"max-width:500px;\">.*?<\/span>/im";
        preg_match_all($regex, $html, $matches);
        $phone=strip_tags($matches[0][0]);
       $phones[]=$phone;
        }


}

猜你喜欢

转载自blog.csdn.net/Jsonxiang/article/details/78820749