最近做一個小項目用到cURl庫函數,從網上找了一些資料,參考國外某博和官方手冊,總結一下。
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page 返回網頁
CURLOPT_HEADER => false, // 不返回頭信息
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // 設置UserAgent
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect 連接超時
CURLOPT_TIMEOUT => 120, // timeout on response 回復超時
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header[errno] = $err;
$header[errmsg] = $errmsg;
$header[content] = $content;
return $header;
}
英文原文:
http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl