php中的curl使用心得详解
这两天做的工作使用到了curl
当要请求的url和自己不在一台server上面,不能直接访问
这种情况下使用curl是最好不过了,模拟post请求做一些事,简单方便
下面记录一下在使用过程中的积累
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3933')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3933>/*
远程post请求
*/
function getRemoteUrl($get_url) {
$curl = curl_init();#启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $get_url);#设置一个Url
curl_setopt($curl, CURLOPT_POST, true);#发送一个常规的Post请求
curl_setopt($curl, CURLOPT_TIMEOUT, 30);#设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0);#显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);#获取的信息以文件流的形式返回
$return = curl_exec($curl); #执行操作
if (curl_errno($curl)) {
return false;
}
curl_close($curl); #关闭CURL会话
return $return;
}
function getCurlData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
总结一下使用curl方法:
先初始化curl
使用curl_setopt设置目标url,和其他选项
curl_exec,执行curl
执行后,关闭curl
最后一步就是输出
curl()效率挺高的,支持多线程,不过需要开启下curl扩展。下面是curl扩展开启的步骤:
1、将PHP文件夹下的三个文件php_curl.dll,libeay32.dll,ssleay32.dll复制到system32下;
2、将php.ini(c:WINDOWS目录下)中的;extension=php_curl.dll中的分号去掉;
3、重启apache或者IIS
您可能感兴趣的文章:
php中的curl使用心得详解
php下载css中图片函数
php curl中x-www-form-urlencoded与multipart/form-data 方式 Post 提交数据详解
php curl模拟post请求的例子
PHP CURL获取cookies模拟登录的方法介绍
php 获取远程网页内容简单函数
php如何使用curl?(用法介绍)
php模拟登录qq邮箱(curl命令详解)
php实现post php curl模拟post传输数据的函数
PHP Curl出现403错误怎么办?curl错误解决方法