当前位置:Gxlcms > PHP教程 > PHP采集相关教程之一:CURL函数库_PHP

PHP采集相关教程之一:CURL函数库_PHP

时间:2021-07-01 10:21:17 帮助过:3人阅读

采集

目前为目最全的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

人气教程排行