Home > Backend Development > PHP Tutorial > PHP采集相关教程之一:CURL函数库_PHP

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

WBOY
Release: 2016-06-01 12:20:52
Original
923 people have browsed it

采集

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template