curl

function grap_data($curlUrl,$referUrl='',$isPost = false,$data =[],$connectTime=10,$activeTime=0,$proxy=false){

        $ch = curl_init();

        $useragent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11"; //浏览器useragent

        $header = array();

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_URL, $curlUrl); //要抓取的网址

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

        curl_setopt($ch, CURLOPT_REFERER, $referUrl);  //模拟来源网址

        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); //模拟常用浏览器的useragent

        curl_setopt($ch, CURLOPT_POST, $isPost); // 发送一个常规的Post请求

        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));    // Post提交的数据包

        if($proxy){

            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式

            curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1"); //代理服务器地址

            curl_setopt($ch, CURLOPT_PROXYPORT, 1080); //代理服务器端口

            //curl_setopt($ch, CURLOPT_PROXYUSERPWD, ":"); //http代理认证帐号,username:password的格式

            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); //使用代理模式

        }

        // 在尝试连接时等待的秒数

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $connectTime);

        // 最大执行时间

        curl_setopt($ch, CURLOPT_TIMEOUT, $activeTime);

        $page_content = curl_exec($ch);

        if($page_content === false){

            $page_content = '{"code":500,"msg":"Curl error:'.curl_error($ch).'"}';

        }

        curl_close($ch);

        return $page_content;

    }

你可能感兴趣的:(curl)