国内美食网站欣赏,佛山企业网站排名优化,昆明做企业网站哪家好,做电影网站一年赚多少钱这篇文章主要介绍了 PHP 中使用 CURL 之 php curl 详细解析和常见大坑 #xff0c;现在分享给大家#xff0c;也给大家做个参考。一起跟随小编过来看看吧。好啦#xff0c;长话短说再长说#xff0c;祭出今天的工具——CURL(Client URL Library)#xff0c;当然今天以 PHP… 这篇文章主要介绍了 PHP 中使用 CURL 之 php curl 详细解析和常见大坑 现在分享给大家也给大家做个参考。一起跟随小编过来看看吧。好啦长话短说再长说祭出今天的工具——CURL(Client URL Library)当然今天以 PHP 的方式来使用这件工具。 前言curl 是个什么东西 PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and userpassword authentication. 这是 PHP 对于 curl 的一个解释简单地说就是curl 是一个库能让你通过 URL 和许多不同种的服务器进行勾搭、搭讪和深入交流并且还支持许多协议。并且人家还说了 curl 可以支持 https 认证、http post、ftp 上传、代理、cookies、简单口令认证等等功能啦。
说了那么多其实没什么感觉吧在应用中才有感觉我起初也是需要在服务器端向另一个服务器发起一个 POST 请求才开始接触 curl 的然后才有了感觉。
在正式讲怎么用之前啊先提一句你得先在你的 PHP 环境中安装和启用 curl 模块具体方式我就不讲了不同系统不同安装方式可以 google 查一下或者查阅 PHP 官方的文档还挺简单的。
1. 拿来先试试手 工具到手先要把玩试试顺不顺手不然一拿来就用把你自己的代码搞得乌烟瘴气还怎么去撩服务器呢 比如我们以著名的 “测试网络是否连接” 的网站——百度为例来尝试下 curl // create curl resource$ch curl_init();// set urlcurl_setopt($ch, CURLOPT_URL, baidu.com);//return the transfer as a stringcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// $output contains the output string$output curl_exec($ch);//echo outputecho $output;// close curl resource to free up system resourcescurl_close($ch);当你在本地环境浏览器打开这个 php 文件时页面出现的是百度的首页特么我刚才输入的 “localhost” 呢
上面的代码和注释已经充分说明了这段代码在干啥。
$ch curl_init()创建了一个 curl 会话资源成功返回一个句柄 curl_setopt($ch, CURLOPT_URL, baidu.com)设置 URL不用说
上面两句可以合起来变一句$ch curl_init(baidu.com)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0)这是设置是否将响应结果存入变量1 是存入0 是直接 echo 出
$output curl_exec($ch)执行然后将响应结果存入$output变量供下面 echo
curl_close($ch)关闭这个 curl 会话资源。
PHP 中使用 curl 大致就是这么一个形式其中第二步通过curl_setopt方法来设置参数是最复杂也是最重要的感兴趣可以去看官方的关于可设置参数的详细参考长地让你看得想吐还是根据需要熟能生巧吧。
小结一下php 中 curl 用法就是创建 curl 会话 - 配置参数 - 执行 - 关闭会话。
下面我们来看一些常用的情景我们需要如何 “打扮自己”配置参数才能正确 “撩妹”正确撩到服务器。
2. GET 和 POST 请求以及 HTTPS 协议处理 先和服务器打个招呼吧给服务器发个 Hello 看她怎么回这里最方便的方式就是向服务器发出 GET 请求当然 POST 这种小纸条也 OK 咯。 2.1 GET 请求
我们以 “在某著名同性交友网站 github 中搜索关键词” 为例
// 通过curl进行GET请求的案例
// create curl resource
$ch curl_init();// set url
curl_setopt($ch, CURLOPT_URL, https://github.com/search?qreact);//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 个可以提的点
默认请求方式是 GET所以不需要显式指定 GET 方式https 请求非 http 请求可能有人在各个地方看到过 HTTPS 请求需要加几行代码绕过 SSL 证书的检查等方式来成功请求到资源但是这里好像并不需要原因是什么 The two Curl options are defined as: CURLOPT_SSL_VERIFYPEER - verify the peers SSL certificate
CURLOPT_SSL_VERIFYHOST - verify the certificates name against hostThey 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 的脚本
//testRespond.php
$phpInputfile_get_contents(php://input);
echo urldecode($phpInput);发送普通数据
然后在本地写一个请求
$dataarray(name Lei,msg Are you OK?
);$ch curl_init();
curl_setopt($ch, CURLOPT_URL, http://测试服务器的IP马赛克/testRespond.php);
curl_setopt($ch, CURLOPT_POST, 1);//The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$output curl_exec($ch);
echo $output;
curl_close($ch);浏览器运行结果是
nameLeimsgAre you OK?这里我们是构造了一个数组作为 POST 数据传给服务器 curl_setopt($ch, CURLOPT_POST, 1)表明是 POST 请求 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60)设置一个最长的可忍受的连接时间秒为单位总不能一直等下去变成木乃伊吧 curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data))设置 POST 的数据域因为这里是数组数据形式的等会来讲 json 格式所以用http_build_query处理一下。
对于 json 数据呢又怎么进行 POST 请求呢
$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 上传文件
同样远程服务器端我们先传好一个接收脚本, 接收图片并且保存到本地注意文件和文件夹权限问题需要有写入权限
if($_FILES){$filename $_FILES[upload][name];$tmpname $_FILES[upload][tmp_name];//保存图片到当前脚本所在目录if(move_uploaded_file($tmpname,dirname(__FILE__)./.$filename)){echo (上传成功);}}然后我们再来写我们本地服务器的php curl部分
$data array(nameboy, uploadboy.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 及以后的实现
$data array(nameboy, 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现在我要去获取这张照片。
$ch curl_init();$fpfopen(./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);现在在我们当前目录下就有了一张刚拿到的照片啦是不是很激动呢
这里值得一说的是curl_getinfo方法这是一个获取本次请求相关信息的方法对于调试很有帮助要善用。
4. HTTP 认证怎么搞 这个时候呢服务器的家长说这个我们女儿还太小不能找对象就将她女儿关了起来并且上了一个密码锁所谓的 HTTP 认证服务器呢偷偷托信鸽将 HTTP 认证的用户名和密码给了你要你去见她带她私奔。 那么拿到了用户名和密码我们怎么通过PHP CURL搞定 HTTP 认证呢
PS: 这里偷懒就不去搭 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);这里有一个地方比较有意思 curl_setopt_array 这个方法可以通过数组一次性地设置多个参数防止有些需要多处设置的出现密密麻麻的curl_setopt方法。
5. 利用 cookie 模拟登陆 这时你成功见到了服务器妹子想带她私奔但是无奈没有盘缠走不远服务器妹子说她妈服务器上有金库可以登陆上去搞一点下来。 首先我们先来分析一下这个事情分两步一是去登陆界面通过账号密码登陆然后获取 cookie二是去利用 cookie 模拟登陆到信息页面获取信息大致的框架是这样的。
//设置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;
}至此总算是模拟登陆成功一切顺利啦通过php CURL“撩” 服务器就是这么简单。
当然CURL的能力远不止于此本文仅希望就后端 PHP 开发中最常用的几种场景做一个整理和归纳。最后一句话具体问题具体分析。