原文地址:http://www.jb51.net/article/25193.htm
我们可以使用PHP的扩展库-Curl,这个扩展库通常是默认在安装包中的,你可以它来获取其他站点的内容,也可以来干别的。
备注:这两段代码需要php_curl扩展库的支持,查看phpinfo(),如果curl support enabled则表示支持curl库。下面我们来看看完成的实例代码:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.nl/”);
// grab URL and pass it to the browser
curl_exec($ch);
// close curl resource, and free up system resources
curl_close($ch);
?>
我们刚刚把另外一个站点的内容,获取过来以后自动输出到浏览器,我们有没有其他的方式组织获取的信息,然后控制其输出的内容呢?完全没有问题,在curl_setopt()函数的参数中,如果希望获得内容但不输出,使用 CURLOPT_RETURNTRANSFER参数,并设为非0值/true!,完整代码请看:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.nl/”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL, and return output
$output = curl_exec($ch);
// close curl resource, and free up system resources
curl_close($ch);
// Replace ‘Google' with ‘PHPit'
$output = str_replace('Google', ‘PHPit', $output);
// Print output
echo $output;
?>
第一个很有意思的参数是 CURLOPT_FOLLOWLOCATION ,当你把这个参数设置为true时,curl会根据任何重定向命令更深层次的获取转向路径,举个例子:当你尝试获取一个PHP的页面,然后这个PHP的页面中有一段跳转代码 ,curl将从http://new_url获取内容,而不是返回跳转代码。完整的代码如下:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.com/”);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// grab URL, and print
curl_exec($ch);
?>
其他形式的页面,无须其实在表单中填入。下面的例子表明我的意思:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,”http://projects/phpit/content/using%20curl%20php/demos/handle_form.php”);
// Do a POST
$data = array('name' => ‘Dennis', 'surname' => ‘Pallett');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// grab URL, and print
curl_exec($ch);
?>
And the handle_form.php file:
echo ‘Form variables I received:';
echo ‘';
print_r ($_POST);
echo ‘';
?>
最后一个选项是 CURLOPT_USERAGENT,它允许你自定义请求是的客户端名称,比如webspilder或是IE6.0.示例代码如下:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://sc.jb51.net/”);
curl_setopt($ch, CURLOPT_USERAGENT, ‘My custom web spider/0.1′);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// grab URL, and print
curl_exec($ch);
?>
函数curl_getinfo()可以使得我们获取接受页面各种信息,你能编辑这些信息通过设定选项的第二个参数,你也可以传递一个数组的形式。就像下面的例子:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.com”);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
// grab URL
$output = curl_exec($ch);
// Print info
echo ‘';
print_r (curl_getinfo($ch));
echo ‘';
?>
大部分返回的信息是请求本身的,像:这个请求花的时间,返回的头文件信息,当然也有一些页面的信息,像页面内容的大小,最后修改的时间。
curl库的第一用途可以查看一个URL页面是否存在,我们可以通过查看这个URL的请求返回的代码来判断比如404代表这个页面不存在,我们来看一些例子:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.com/does/not/exist”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL
$output = curl_exec($ch);
// Get response code
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Not found?
if ($response_code == ‘404′) {
echo ‘Page doesn\'t exist';
} else {
echo $output;
}
?>