一个简单的例子
<php?
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"baidu.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exce($ch);
echo $output;
curl_close($ch);
?>
先简单说一下语法,<code>$ch=curl_init()</code>创建了一个curl会话,成功返回一个句柄。<code>curl_setopt($ch,CURLOPT_URL,"baidu.com")</code>设置url,上面可以通过<code>$ch=curl_init(“baidu.com”)</code>实现。<code>curl_setopt($ch,CURLOPT_RETURNTRANSFER,0)</code>是否将结构存入变量,1是存入,0是直接echo出去。<code>$output=curl_exce($ch)</code>直接执行结果存入<code>$output</code>,然后echo出去。<code>curl_close($ch)</code>
1、实现http get请求
<?php
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"https://github.com/search?q=react");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exce($ch);
echo $output;
curl_close($ch);
?>
2、实现http post请求
<?php
$data=array(
"name"=>"li",
"msg"=>"are you ok"
);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://测试服务器的IP马赛克/testRespond.php");
curl_setopt($ch,CURL_POST,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exce($sh);
curl_close($ch)
?>
浏览器运行结果是:
name=Lei&msg=Are you OK?
这里我们是构造了一个数组作为POST数据传给服务器:
<code>curl_setopt($ch, CURLOPT_POST, 1)<code>表明是POST请求;
<code>curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60)<code>设置一个最长的可忍受的连接时间,秒为单位,总不能一直等下去变成木乃伊吧;
<code>curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data))<code>设置POST的数据域,因为这里是数组数据形式的(等会来讲json格式),所以用http_build_query处理一下。
3、通过json实现http post请求
$data={"name":"li","msg":"are you ok"};
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://测试服务器的IP马赛克/testRespond.php");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json', 'Content-Length:' . strlen($data)));
curl_setopt($ch,CURLOPT_POSTFIELDS ,data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exce($ch);
echo $output;
curl_close($output);
4、上传图片
远程服务器
<?php
if($_FILES){
$filename =$_FILES['upload']['name'];
$tmpname=$_FILES['upload']['tmp_name'];
if(move_upload_file($tmpname,dirname('__FILE__')./.$filename)){
echo "成功"
}
}
?>
然后我们再来写我们本地服务器的php curl部分:
php5.3实现方法
$data = array('name'=>'boy', "upload"=>"@boy.png");
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://远程服务器地址马赛克/testRespond.php");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
下面来讲PHP5.6及以后的实现:
<?php
$data = array('name'=>'boy', "upload"=>"");
$ch = curl_init();
$data['upload']=new CURLFile(realpath(getcwd().'/boy.png'));
curl_setopt($ch, CURLOPT_URL, "http://115.29.247.189/test/testRespond.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
5、远程获取图片
$ch = curl_init();
$fp=fopen('./girl.jpg', 'w');
curl_setopt($ch, CURLOPT_URL, "http://远程服务器地址马赛克/girl.jpg");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_FILE, $fp);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
fclose($fp);
$size = filesize("./girl.jpg");
if ($size != $info['size_download']) {
echo "下载的数据不完整,请重新下载";
} else {
echo "下载数据完整";
}
curl_close($ch);
6、HTTP认证
function curl_auth($url,$user,$passwd){
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_USERPWD => $user.':'.$passwd,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
]);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$authurl = 'http://要请求HTTP认证的地址';
echo curl_auth($authurl,'vace','passwd');
7、利用cookie模拟登陆
首先我们先来分析一下,这个事情分两步,一是去登陆界面通过账号密码登陆,然后获取cookie,二是去利用cookie模拟登陆到信息页面获取信息,大致的框架是这样的。
<?php
//设置post的数据
$post = array (
'email' => '账户',
'pwd' => '密码'
);
//登录地址
$url = "登陆地址";
//设置cookie保存路径
$cookie = dirname(__FILE__) . '/cookie.txt';
//登录后要获取信息的地址
$url2 = "登陆后要获取信息的地址";
//模拟登录
login_post($url, $cookie, $post);
//获取登录页的信息
$content = get_content($url2, $cookie);
//删除cookie文件
@ unlink($cookie);
var_dump($content);
?>
然后我们思考下下面两个方法的实现:
login_post($url, $cookie, $post)
get_content($url2, $cookie)
//模拟登录
function login_post($url, $cookie, $post) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
curl_exec($curl);
curl_close($curl);
}
//登录成功后获取数据
function get_content($url, $cookie) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$rs = curl_exec($ch);
curl_close($ch);
return $rs;
}