PHP通过cURl库函数获取网页详解_PHP教程
程序员文章站
2022-05-03 17:18:02
...
最近做一个小项目用到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
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
上一篇: Symfony2联合查询实现方法
下一篇: 解决ajax+php中文乱码的方法详解