logo

PHP使用curl函数库详细采集教程

时间:2010-2-15 19:46:49 来源:中国站长站综合 作者:编辑整理

 

目前为目最全的CURL中文说明了,学PHP的要好好掌握。有很多的参数。大部份都很有用。真正掌握了它和正则,一定就是个采集高手了。

先写一个简单的抓取页面函数

function GetSources($Url,$User_Agent='',$Referer_Url='') //抓取某个指定的页面
{
//$Url 需要抓取的页面地址
//$User_Agent 需要返回的user_agent信息 如“baiduspider”或“googlebot”
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $Url);
curl_setopt ($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt ($ch, CURLOPT_REFERER, $Referer_Url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$MySources = curl_exec ($ch);
curl_close($ch);
return $MySources;
}

参数取值:

$Url = "http://www.baidu.com";

$User_Agent = "baiduspider+(+http://www.baidu.com/search/spider.htm)";

$Referer_Url = 'http://www.chinaz.com/';

执行GetSources($Url,$User_Agent,$Referer_Url)后的结果为:

http://test.huangchao.org/curl/curl_test1.php

 


标签:
100%

回复:PHP使用curl函数库详细采集教程

0 / 5000