无为清净楼资源网 Design By www.qnjia.com

七夕啦,作为开发,妹子没得撩就“撩”下服务器吧,妹子有得撩的同学那就左拥妹子右抱服务器吧,况且妹子是要礼物的,服务器又不用。好啦,长话短说再长说,祭出今天的工具——CURL(Client URL Library),当然今天以PHP的方式来使用这件工具。

0. 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 user+password authentication.

这是PHP对于curl的一个解释,简单地说就是,curl是一个库,能让你通过URL和许多不同种的服务器进行勾搭、搭讪和深入交流,并且还支持许多协议。并且人家还说了curl可以支持https认证、http post、ftp上传、代理、cookies、简单口令认证等等功能啦。

说了那么多其实没什么感觉吧,在应用中才有感觉,我起初也是需要在服务器端向另一个服务器发起一个POST请求才开始接触curl的,然后才有了感觉。

在正式讲怎么用之前啊,先提一句,你得先在你的PHP环境中安装和启用curl模块,具体方式我就不讲了,不同系统不同安装方式,可以google查一下,或者查阅PHP官方的文档,还挺简单的。

1. 拿来先试试手

工具到手,先要把玩,试试顺不顺手,不然一拿来就用,把你自己的代码搞得乌烟瘴气还怎么去撩服务器呢?

比如我们以著名的“测试网络是否连接”的网站——百度为例,来尝试下curl

<"baidu.com"); 

  //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);   
"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请求的案例
<"https://github.com/search"); 

  //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);   
"codetitle">复制代码 代码如下: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的脚本:

//testRespond.php
<"htmlcode">
<"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);   
"color: #800000">对于json数据呢,又怎么进行POST请求呢?

<"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上传文件

同样远程服务器端我们先传好一个接收脚本,接收图片并且保存到本地,注意文件和文件夹权限问题,需要有写入权限:

<"htmlcode">
<"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);     
"htmlcode">
<"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);     
"htmlcode">
<"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);  
"htmlcode">
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模拟登陆到信息页面获取信息,大致的框架是这样的。

<"登陆地址"; 
 //设置cookie保存路径 
 $cookie = dirname(__FILE__) . '/cookie.txt'; 
 //登录后要获取信息的地址 
 $url2 = "登陆后要获取信息的地址"; 
 //模拟登录 
 login_post($url, $cookie, $post); 
 //获取登录页的信息 
 $content = get_content($url2, $cookie); 
 //删除cookie文件 
 @ unlink($cookie);

 var_dump($content);  
"htmlcode">
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开发中最常用的几种场景做一个整理和归纳。最后一句话,具体问题具体分析。

标签:
php,curl,post,php,curl,教程,php,curl,get

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?