- 拿来先试试手
比如我们以著名的“测试网络是否连接”的网站——百度为例,来尝试下curl
当你在本地环境浏览器打开这个php文件时,页面出现的是百度的首页,特么我刚才输入的“localhost”呢?
上面的代码和注释已经充分说明了这段代码在干啥。
c h = c u r l i n i t ( ) , 创 建 了 一 个 c u r l 会 话 资 源 , 成 功 返 回 一 个 句 柄 ; c u r l s e t o p t ( ch = curl_init(),创建了一个curl会话资源,成功返回一个句柄; curl_setopt( ch=curlinit(),创建了一个curl会话资源,成功返回一个句柄;curlsetopt(ch, CURLOPT_URL, “baidu.com”),设置URL,不用说;
上面两句可以合起来变一句$ch = curl_init(“baidu.com”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0)这是设置是否将响应结果存入变量,1是存入,0是直接echo出;
o u t p u t = c u r l e x e c ( output = curl_exec( output=curlexec(ch)执行,然后将响应结果存入$output变量,供下面echo;
curl_close($ch)关闭这个curl会话资源。
PHP中使用curl大致就是这么一个形式,其中第二步,通过curl_setopt方法来设置参数是最复杂也是最重要的,感兴趣可以去看官方的关于可设置参数的详细参考,长地让你看得想吐,还是根据需要熟能生巧吧。
小结一下,php中curl用法就是:创建curl会话 -> 配置参数 -> 执行 -> 关闭会话。
下面我们来看一些常用的情景,我们需要如何“打扮自己”(配置参数)才能正确“撩妹”(正确撩到服务器)。
2.GET和POST请求以及HTTPS协议处理
2.1 GET请求
我们以“在某网站github中搜索关键词”为例
//通过curl进行GET请求的案例
<?php // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "https://github.com/search?q=react"); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); //echo output echo $output; // close curl resource to free up system resources curl_close($ch); ?>好像和之前那个例子没啥差别,但这里有2个可以提的点:
1.默认请求方式是GET,所以不需要显式指定GET方式;
2.https请求,非http请求,可能有人在各个地方看到过HTTPS请求需要加几行代码绕过SSL证书的检查等方式来成功请求到资源,但是这里好像并不需要,原因是什么?
The two Curl options are defined as:
CURLOPT_SSL_VERIFYPEER - verify the peer’s SSL certificate
CURLOPT_SSL_VERIFYHOST - verify the certificate’s name against host
They both default to true in Curl, and shouldn’t be disabled unless you’ve got a good reason. Disabling them is generally only needed if you’re sending requests to servers with invalid or self-signed certificates, which is only usually an issue in development. Any publicly-facing site should be presenting a valid certificate, and by disabling these options you’re potentially opening yourself up to security issues.
即,除非用了非法或者自制的证书,这大多数出现在开发环境中,你才将这两行设置为false以避开ssl证书检查,否者不需要这么做,这么做是不安全的做法。
2.2 POST请求
那如何进行POST请求呢?为了测试,先在某个测试服务器传了一个接收POST的脚本:
发送普通数据
然后在本地写一个请求:
浏览器运行结果是:
name=Lei&msg=Are you OK?
这里我们是构造了一个数组作为POST数据传给服务器:
curl_setopt($ch, CURLOPT_POST, 1)表明是POST请求;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60)设置一个最长的可忍受的连接时间,秒为单位,总不能一直等下去变成木乃伊吧;
curl_setopt( c h , C U R L O P T P O S T F I E L D S , h t t p b u i l d q u e r y ( ch, CURLOPT_POSTFIELDS , http_build_query( ch,CURLOPTPOSTFIELDS,httpbuildquery(data))设置POST的数据域,因为这里是数组数据形式的(等会来讲json格式),所以用http_build_query处理一下。
对于json数据呢,又怎么进行POST请求呢?
<?php $data='{"name":"Lei","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_exec($ch); echo $output; curl_close($ch); ?>浏览器执行,显示:
{“name”:“Lei”,“msg”:“Are you OK?”}
3. 如何上传和下载文件
3.1 POST上传文件
同样远程服务器端我们先传好一个接收脚本,接收图片并且保存到本地,注意文件和文件夹权限问题,需要有写入权限:
然后我们再来写我们本地服务器的php curl部分:
<?php $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); ?>浏览器中运行一下,什么都米有,去看一眼远程的服务器,还是什么都没有,并没有上传成功。
为什么会这样呢?上面的代码应该是大家搜索curl php POST图片最常见的代码,这是因为我现在用的是PHP5.6以上版本,@符号在PHP5.6之后就弃用了,PHP5.3依旧可以用,所以有些同学发现能执行啊,有些发现不能执行,大抵是因为PHP版本的不同,而且curl在这两版本中实现是不兼容的,上面是PHP5.3的实现。
下面来讲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); ?>这里引入了一个CURLFile对象进行实现,关于此的具体可查阅文档进行了解。这时候再去远程服务器目录下看看,发现有了一张图片了,而且确实是我们刚才上传的图片。
3.2 获取远程服务器的图片
远程服务器在她自己的目录下存放了一个图片叫girl.jpg,地址是她的web服务器根目录/girl.jpg,现在我要去获取这张图片。
现在,在我们当前目录下就有了一张刚拿到的照片啦,是不是很激动呢!
这里值得一说的是curl_getinfo方法,这是一个获取本次请求相关信息的方法,对于调试很有帮助,要善用。
- HTTP认证怎么搞
那么拿到了用户名和密码,我们怎么通过PHP CURL搞定HTTP认证呢?
PS:这里偷懒就不去搭HTTP认证去试了,直接放一段代码,我们分析下。
function curl_auth(
u
r
l
,
url,
url,user,$passwd){
c
h
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
a
r
r
a
y
(
ch = curl_init(); curl_setopt_array(
ch=curlinit();curlsetoptarray(ch, [
CURLOPT_USERPWD =>
u
s
e
r
.
′
:
′
.
user.':'.
user.′:′.passwd,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
]);
r
e
s
u
l
t
=
c
u
r
l
e
x
e
c
(
result = curl_exec(
result=curlexec(ch);
curl_close($ch);
return $result;
}
$authurl = ‘http://要请求HTTP认证的地址’;
echo curl_auth($authurl,‘vace’,‘passwd’);
这里有一个地方比较有意思:
curl_setopt_array 这个方法可以通过数组一次性地设置多个参数,防止有些需要多处设置的出现密密麻麻的curl_setopt方法。
5.利用cookie模拟登陆
首先我们先来分析一下,这个事情分两步,一是去登陆界面通过账号密码登陆,然后获取cookie,二是去利用cookie模拟登陆到信息页面获取信息,大致的框架是这样的。
然后我们思考下下面两个方法的实现:
login_post($url, $cookie, $post)
get_content($url2, $cookie)
//模拟登录
function login_post($url, $cookie, $post) {
c
u
r
l
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
(
curl = curl_init(); curl_setopt(
curl=curlinit();curlsetopt(curl, CURLOPT_URL,
u
r
l
)
;
c
u
r
l
s
e
t
o
p
t
(
url); curl_setopt(
url);curlsetopt(curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl, CURLOPT_COOKIEJAR,
c
o
o
k
i
e
)
;
c
u
r
l
s
e
t
o
p
t
(
cookie); curl_setopt(
cookie);curlsetopt(curl, CURLOPT_POST, 1);
curl_setopt(
c
u
r
l
,
C
U
R
L
O
P
T
P
O
S
T
F
I
E
L
D
S
,
h
t
t
p
b
u
i
l
d
q
u
e
r
y
(
curl, CURLOPT_POSTFIELDS, http_build_query(
curl,CURLOPTPOSTFIELDS,httpbuildquery(post));
curl_exec(
c
u
r
l
)
;
c
u
r
l
c
l
o
s
e
(
curl); curl_close(
curl);curlclose(curl);
}
//登录成功后获取数据
function get_content($url, $cookie) {
c
h
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
(
ch = curl_init(); curl_setopt(
ch=curlinit();curlsetopt(ch, CURLOPT_URL,
u
r
l
)
;
c
u
r
l
s
e
t
o
p
t
(
url); curl_setopt(
url);curlsetopt(ch, CURLOPT_HEADER, 0);
curl_setopt(
c
h
,
C
U
R
L
O
P
T
R
E
T
U
R
N
T
R
A
N
S
F
E
R
,
1
)
;
c
u
r
l
s
e
t
o
p
t
(
ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt(
ch,CURLOPTRETURNTRANSFER,1);curlsetopt(ch, CURLOPT_COOKIEFILE, $cookie);
r
s
=
c
u
r
l
e
x
e
c
(
rs = curl_exec(
rs=curlexec(ch);
curl_close($ch);
return $rs;
}