PHP curl 獲取錯誤

 

if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo '操作完成沒有任何錯誤';
}

獲取curl返回信息

$info = curl_getinfo($ch);
dump($info);die;

url:網絡地址。
content_type:內容編碼。
http_code:HTTP狀態碼。
header_size:header的大小。
request_size:請求的大小。
filetime:文件創建的時間。
ssl_verify_result:SSL驗證結果。
redirect_count:跳轉計數。
total_time:總耗時。
namelookup_time:DNS查詢耗時。
connect_time:等待連接耗時。
pretransfer_time:傳輸前準備耗時。
size_uplpad:上傳數據的大小。
size_download:下載數據的大小。
speed_download:下載速度。
speed_upload:上傳速度。
download_content_length:下載內容的長度。
upload_content_length:上傳內容的長度。
starttransfer_time:開始傳輸的時間表。
redirect_time:重定向耗時。

 

curl post 請求

/**
 * 模擬post進行url請求
 * @param string $url
 * @param string $param
 */
function request_post($url = '', $param = '') {
    ini_set('max_execution_time','0');
    set_time_limit('0');
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁
    curl_setopt($ch, CURLOPT_HEADER, 0);//設置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果爲字符串且輸出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);

    $data = curl_exec($ch);//運行curl
    curl_close($ch);
    return $data;
}

curl get 請求
 

/**
 * 使用curl獲取遠程數據
 * @param  string $url url連接
 * @return string      獲取到的數據
 */
function curl_get($url){
    ini_set('max_execution_time','0');
    set_time_limit('0');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

curl get/post請求
 

//參數1:訪問的URL,參數2:post數據(不填則爲GET),
//參數3:提交的$cookies,參數4:是否返回$cookies
function curl_request($url,$post='',$cookie='', $returnCookie=0){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
    if($post) {
        curl_setopt($curl, CURLOPT_POST, 1);
        // curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if($cookie) {
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    if (curl_errno($curl)) {
        return curl_error($curl);
    }
    curl_close($curl);
    if($returnCookie){
        list($header, $body) = explode("\r\n\r\n", $data, 2);
        preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
        $info['cookie']  = substr($matches[1][0], 1);
        $info['content'] = $body;
        return $info;
    }else{
        return $data;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章