您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php curl惯用函数

2024/4/19 10:39:12发布6次查看
php curl常用函数
php curl常用于:get,post,http验证,302重定向,设置curl的代理。
1、开启php的curl功能在windows平台下,或者使用xampp之类的集成服务器的程序,会非常简单,你需要改一改你的php.ini文件的设置,找到php_curl.dll,并取消前面的分号注释就行了。如下所示:
//取消注释,开启curl功能
extension=php_curl.dll
在linux下面,那么,你需要重新编译你的php了,编辑时,你需要打开编译参数——在configure命令上加上“–with-curl” 参数。
2、使用curl来get数据curl最简单最常用的采用get来获取网页内容的php函数
function getcurl($url){
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_header, 0);
curl_setopt($curl, curlopt_timeout, 3);//超时时间
curl_setopt($curl, curlopt_returntransfer, true);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
3、使用curl来post数据当我们需要对curl请求的页面采用post的请求方式时,我们使用下面的php函数
function _curl_post($url, $vars) {
$ch = curl_init();
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $vars);
$data = curl_exec($ch);
curl_close($ch);
if ($data)
return $data;
else
return false;
}
4、使用curl,需要http服务器认证当我们请求地址需要加上身份验证,即http服务器认证的时候,我们就要使用下面的函数了,对于curl中get方法使用验证也是采用相同的方式。
function postcurlhttp($url, $str) {
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_userpwd, “验证的用户名:密码”);
curl_setopt($ch, curlopt_postfields, $str);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
if ($headers['http_code'] == 200) {
return $data;
} else {
return false;
}
}
5、使用curl获取302重定向的页面下面函数$data为重定向后页面的内容,这里我们写一个简单的curl post的302重定向后返回重定向页面url的函数,有时候返回页面的url更加重要。
function _curl_post_302($url, $vars) {
$ch = curl_init();
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_followlocation, 1); // 302 redirect
curl_setopt($ch, curlopt_postfields, $vars);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
if ($data&&$headers)
return s$headers[url];
else
return false;
}
6、给curl加个代理服务器$ch = curl_init();
curl_setopt($ch, curlopt_url, ‘http://www.js8.in‘);
curl_setopt($ch, curlopt_header, 1);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_httpproxytunnel, 1);
curl_setopt($ch, curlopt_proxy, ‘代理服务器地址(www.js8.in):端口’);
curl_setopt($ch, curlopt_proxyuserpwd, ‘代理用户:密码’);
$data = curl_exec();
curl_close($ch);
7、一个curl简单的类callback = $func_name;
}
function dorequest($method, $url, $vars) {
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_header, 1);
curl_setopt($ch, curlopt_useragent, $_server['http_user_agent']);
curl_setopt($ch, curlopt_followlocation, 1);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_cookiejar, ‘cookie.txt’);
curl_setopt($ch, curlopt_cookiefile, ‘cookie.txt’);
if ($method == ‘post’) {
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $vars);
}
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
if ($this->callback)
{
$callback = $this->callback;
$this->callback = false;
return call_user_func($callback, $data);
} else {
return $data;
}
} else {
return curl_error($ch);
}
}
function get($url) {
return $this->dorequest(‘get’, $url, ‘null’);
}
function post($url, $vars) {
return $this->dorequest(‘post’, $url, $vars);
}
}
?>
转自:http://blog.phpmake.com/?p=326
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录