PHP中使用CURL

 Winodw下验证通过
1、Windows下的PHP开启curl库支持:
打开php.ini,将extension=php_curl.dll前的;号去掉。

将php下的libeay32.dll和ssleay32.dll拷贝到systemw32下重启apache就可以了
测试代码:

<?php
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.cn/”);
// 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!,完整代码请看:

<?php

// 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;
?>

(查看在线demo )

在上面的2个实例中,你可能注意到通过设置函数curl_setopt() 的不同参数,可以获得不同结果,这正是curl强大的原因,下面我们来看看这些参数的含义。

CURL的相关选项:

如果你看过php手册中的curl_setopt()函数,你可以注意到了,它下面长长的参数列表,我们不可能一一介绍,更多的内容请查看PHP手册,这里只介绍常用的和有的一些参数。

第一个很有意思的参数是 CURLOPT_FOLLOWLOCATION ,当你把这个参数设置为true时,curl会根据任何重定向命令更深层次的获取转向路径,举个例子:当你尝试获取一个PHP的页面,然后这个PHP的页 面中有一段跳转代码 <?php header(”Location:http://new_url”);….?>,curl将从http://new_url获取内容,而不是返回 跳转代码。完整的代码如下:

<?php

// 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);
?>

(查看在线demo ),
如果Google发送一个转向请求,上面的例子将根据跳转的网址继续获取内容,和这个参数有关的两个选项是CURLOPT_MAXREDIRSCURLOPT_AUTOREFERER .
参数CURLOPT_MAXREDIRS 选项允许你定义跳转请求的最大次数,超过了这个次数将不再获取其内容。如果CURLOPT_AUTOREFERER 设置为true时,curl会自动添加Referer header在每一个跳转链接,可能它不是很重要,但是在一定的案例中却非常的有用。

下一步介绍的参数是CURLOPT_POST ,这是一个非常有用的功能,因为它可以让您这样做POST请求,而不是GET请求,这实际上意味着你可以提交
其他形式的页面,无须其实在表单中填入。下面的例子表明我的意思:

<?php
// 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);
?>

(View Live Demo )
And the handle_form.php file:

<?php
echo ‘<h3>Form variables I received: </h3>’;
echo ‘<pre>’;
print_r ($_POST);
echo ‘</pre>’;
?>

正如你可以看到,这使得它真的很容易提交形式,这是一个伟大的方式来测试您的所有形式,而不以填补他们在所有的时间。
参数CURLOPT_CONNECTTIMEOUT 通常用来设置curl尝试请求链接的时间,这是一个非常重要的选项,如果你把这段时间设置的太短了,可能会导致curl请求失败。
但是如果你把它设置的时间太长了,可能PHP脚本将死掉。和这个参数相关的一个选项是 CURLOPT_TIMEOUT ,这是用来设置curl允许执行的时间需求。如果您设置这一个很小的值,它可能会导下载的网页上是不完整的,因为他们需要一段时间才能下载。
最后一个选项是 CURLOPT_USERAGENT,它允许你自定义请求是的客户端名称,比如webspilder或是IE6.0.示例代码如下:

<?php
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.useragent.org/”);
curl_setopt($ch, CURLOPT_USERAGENT, ‘My custom web spider/0.1′);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// grab URL, and print
curl_exec($ch);
?>

(View Live Demo)

现在我们把最有意思的一个参数都介绍过了,下面我们来介绍一个curl_getinfo() 函数,看看它能为我们做些什么。

获取页面的信息:

函数curl_getinfo()可以使得我们获取接受页面各种信息,你能编辑这些信息通过设定选项的第二个参数,你也可以传递一个数组的形式。就像下面的例子:

<?php
// 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 ‘<pre>’;
print_r (curl_getinfo($ch));
echo ‘</pre>’;
?>

(View Live Demo )

大部分返回的信息是请求本身的,像:这个请求花的时间,返回的头文件信息,当然也有一些页面的信息,像页面内容的大小,最后修改的时间。

那些全是关于curl_getinfo()函数的,现在让我们看看它的实际用途。

实际用途:

curl库的第一用途可以查看一个URL页面是否存在,我们可以通过查看这个URL的请求返回的代码来判断比如404代表这个页面不存在,我们来看一些例子:

<?php
// 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;
}
?>

(View Live Demo )

其他的用户可能是创建一个自动检查器,验证每个请求的页面是否存在。
我们可以用curl库来写和google类似的网页蜘蛛(web spider),或是其他的网页蜘蛛


2、Linux下的PHP开启curl库支持:
编译PHP时在./configure后加上 –with-curl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值