Guzzle

Guzzle 是一个 PHP HTTP 客户端,致力于让发送 HTTP 请求以及与 Web 服务进行交互变得简单。
Github:https://github.com/guzzle/guzzle
Composer:https://packagist.org/packages/guzzlehttp/guzzle

发送请求

use GuzzleHttp\Client;$client = new Client([//跟域名'base_uri' => 'http://localhost/test',// 超时'timeout'  => 2.0,
]);$response = $client->get('/get'); //http://localhost/get
$response = $client->delete('delete');  //http://localhost/get/delete
$response = $client->head('http://localhost/get');
$response = $client->options('http://localhost/get');
$response = $client->patch('http://localhost/patch');
$response = $client->post('http://localhost/post');
$response = $client->put('http://localhost/put');

POST

$response = $client->request('POST', 'http://localhost/post', ['form_params' => ['username' => 'webben','password' => '123456','multiple' => ['row1' => 'hello']]
]);

响应

# 状态码
$code = $response->getStatusCode(); // 200
$reason = $response->getReasonPhrase(); // OK# header
// Check if a header exists.
if ($response->hasHeader('Content-Length')) {echo "It exists";
}// Get a header from the response.
echo $response->getHeader('Content-Length');// Get all of the response headers.
foreach ($response->getHeaders() as $name => $values) {echo $name . ': ' . implode(', ', $values) . "\r\n";
}# 响应体
$body = $response->getBody();
// Implicitly cast the body to a string and echo it
echo $body;
// Explicitly cast the body to a string
$stringBody = (string) $body;
// Read 10 bytes from the body
$tenBytes = $body->read(10);
// Read the remaining contents of the body as a string
$remainingBytes = $body->getContents();

自定义header

// Set various headers on a request
$client->request('GET', '/get', [//header'headers' => ['User-Agent' => 'testing/1.0','Accept'     => 'application/json','X-Foo'      => ['Bar', 'Baz']],//下载'save_to'=> $filename,//referer'allow_redirects' => ['referer'     => '',],
]);

cookie 访问

$client = new \GuzzleHttp\Client();$url = 'https://www.baidu.com/getUserInfo';$jar = new \GuzzleHttp\Cookie\CookieJar();
$cookie_domain = 'www.baidu.com';
$cookies = ['BAIDUID'    => '221563C227ADC44DD942FD9E6D577EF2CD',
];$cookieJar = $jar->fromArray($cookies, $cookie_domain);$res = $client->request('GET', $url, ['cookies' => $cookieJar,// 'debug' => true,
]);
$body = $res->getBody();

手册地址:http://docs.guzzlephp.org/en/stable/request-options.html#headers

爬虫脚本

include './vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;class Metting{private $client;/*** 可以有多个候选,防止会议室被提前预定。* 预定成功自动break**/public $rooms = [195 => '阿基米德',196 => '费曼',203 => '特斯拉',202 => '爱因斯坦',188 => '牛顿',];public $time_start  = '15:00';public $time_end    = '16:30';/*** tip 从Charles复制过来的是utf8需要转码**/private $cookies = ['xdf_openid'  => 'ktxPYw7mXiAFvoTno8yIVZUXo7FkahPMQMpXvqrH0q0=',];public $base_uri        = 'http://smr.xdf.cc/';public $submit_uri      = '/bespoke/Bespoke/addSaveAPP/';public $lock_uri        = '/bespokepc/Index/addSavePCMeeting/';public $cookie_domain   = 'smr.xdf.cc';// 员工IDpublic $staffid         = 10000;//public $base_uri      = 'http://127.0.0.1:1999/';//public $lock_uri      = '/metting/lock';//public $submit_uri    = '/metting/submit';//public $cookie_domain = '127.0.0.1:1999';public function __construct(){$cookieJar = CookieJar::fromArray( $this->cookies, $this->cookie_domain);$this->client = new Client(['base_uri'  => $this->base_uri,'timeout'   => 5,'cookies'    => $cookieJar,'headers'    => ['User-Agent' => $this->user_agent,],]);}public function run(){$day = date('Y-m-d',strtotime('+1 day'));foreach( $this->rooms as $roomid => $roomname){$lockid = $this->lock( $roomid, $day);if( $lockid > 0){$ret = $this->submit( $lockid, $roomid, $day);if( $ret){break;}}usleep(10*1000);}}/*** 锁定成功返回lockid,失败(被别人预定)为0,有其他异常可能未处理* lock 接口未限制权限,也可以用postman模拟**/public function lock( $roomid, $day){//$uri = '/metting/lock';$data = ['day'       => $day,'start'     => $this->time_start,'end'       => $this->time_end,'roomid'    => $roomid,];print_r( $data);$resp = $this->client->request('POST', $this->lock_uri,['form_params' => $data]);$body = (string) $resp->getBody();return $body;}public function submit( $lockid, $roomid, $day){//$uri = '/metting/submit';$data = ['theme'         => '研发周会','content'       => '','renshu'        => 1,'emailstr'      => '','mail_rili'     => [0 => 1, 1=>1],'canjiaemail'   => '','canjiastaff1'  => '','canjiastaff'   => $this->staffid,'appointID'     => 0,'roomid'        => $roomid,'appORweb'      => 0,'keyuyue'       => $this->time_start.'~'.$this->time_end,'day'           => $day,'urgent'        => 0,'meetID'        => $lockid];$resp = $this->client->request('POST', $this->submit_uri, ['form_params' => $data]);print_r( $data);$body = $resp->getBody()->getContents();print_r( $body);$array = json_decode( $body, true);print_r( $array);if( $array['ed4'] == 200){return true;}return false;}private $user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/18D52 AliApp(DingTalk/6.0.10) com.laiwang.DingTalk/14684863 Channel/201200 language/zh-Hans-CN UT4Aplus/0.0.6 WK';
}
$obj = new Metting();
$obj->run();

guzzle/guzzle 日常使用相关推荐

  1. laravel 安装guzzlehttp/guzzle

    composer require guzzlehttp/guzzle Guzzle是一个PHP HTTP客户端,可以轻松发送HTTP请求,并且可以轻松集成Web服务. 用于构建查询字符串,POST请求 ...

  2. [guzzlehttp/guzzle]使用起来更优雅的HTTP客户端

    在处理业务时,我们总是会发起一个http请求,比如请求远程接口,或者下载一个文件.很显然,在PHP中需要使用CURL,但是curl写起来实在是太不舒服了,又难写,也不易阅读.实际上PHP有很多扩展可以 ...

  3. Golang的interface实践

    这是第二个我在别的语言里面没有见过的实现,go的interface可以说是独树一帜,让我们仔细来实践一下. interface类型是什么?interface类型定义了一组方法,如果某个对象实现了某个接 ...

  4. 各种实用的 PHP 开源库推荐

    PHP 是一种通用开源脚本语言.语法吸收了 C 语言.Java 和 Perl 的特点,利于学习,使用广泛,主要适用于 Web 开发领域,是大多数后端开发者的首选. PHP 作为最受欢迎的编程语言之一, ...

  5. PHP各种实用的开源库推荐

    本文从众多 PHP 开源库中选出了几款实用有趣的工具,希望对你的学习工作有帮助. PHP 日志工具 Monolog Monolog 是一种支持 PHP 5.3+ 以上的日志记录工具.并为 Symfon ...

  6. PHP45个方便的工具

    PHP是为Web开发设计的服务器脚本语言,但也是一种通用的编程语言.超过2.4亿个索引域使用PHP,包括很多重要的网站,例如 Facebook.Digg和WordPress.和其它脚本语言相比,例如P ...

  7. php组件是啥,浅谈PHP组件、框架以及Composer

    本篇文章主要介绍了PHP组件.框架以及Composer,具有一定的学习价值,感兴趣的朋友可以了解一下. 什么是组件 组件是一组打包的代码,是一系列相关的类.接口和Trait,用于帮助我们解决PHP应用 ...

  8. php顶级框架,10个顶级PHP开源项目「2019」

    1.Laravel Laravel是一个为Web开发者打造的PHP开发框架. GitHub Stars: 43.5k+ 网址:https://github.com/laravel/laravel 2. ...

  9. 2020年GitHub上50个最受程序员欢迎的PHP开源项目

    GitHub上50个最受欢迎的PHP开源项目[2019] 1.Laravel Laravel是一个为Web开发者打造的PHP开发框架. GitHub Stars: 43.5k+ 网址:https:// ...

  10. 必学PHP类库/常用PHP类库大全

    [JingwenTian]awesome-php [ziadoz]awesome-php 依赖管理( Dependency Management ) 用于依赖管理的包和框架 Composer / Pa ...

最新文章

  1. mysql数据库商业版与社区版的区别
  2. [Xcode 实际操作]七、文件与数据-(17)解析JSON文档
  3. 加速度计和陀螺仪数据融合
  4. iphone7防水_iPhone11系列防水测试,其结果令人意外
  5. 2012二级java真题_2012年计算机二级JAVA模拟试题及答案详解汇总
  6. 软件机器人从幕后到台前 RPA+Chatbot带来“端到端的自动化”
  7. 系统修复对电脑的影响有哪些
  8. CXF之jaxws:endpoint对spring bean的引用
  9. linux中docker容器与宿主系统之间文件拷贝
  10. 如何通过Excel文件批量生成PDF417二维码
  11. python平稳性检验程序_用Python检验时间序列的平稳性
  12. 离散数学(下)第十章 群与环
  13. Linux ARM平台开发系列讲解(摄像头V4L2子系统) 2.12.4 V4L2子设备操作函数结构体分析
  14. mongodb-报错FailedToParse: Password must be URL Encoded for mongodb:// URL:
  15. 广西艺术学院2012年本科招生专业考试通知
  16. 惠普笔记本按开机键后电源灯亮的,但是屏幕一直是黑的,只有大写锁定键闪烁,闪3次一个循环,听得到风扇...
  17. 用Python提取图片主要颜色
  18. 全球卫星导航定位技术的现状
  19. wxpy实现微信机器人
  20. 轩辕谷,黄帝的诞生地

热门文章

  1. 书香小说APP界面设计
  2. [POJ 3683]Priest Johns Busiest Day
  3. vue中使用tsx语法
  4. 计算机青岛科技大学济南大学,山东考生在山东理工,济大,山东科技和青岛科技中该如何选择?...
  5. Proximal Gradient for LASSO
  6. 环信php修改头像,环信客服 如何正确设置用户的头像和昵称?
  7. 【云原生之Docker实战】使用Docker部署siyuan个人笔记系统
  8. “创业吃过饼,国企养过老,android开发零基础
  9. Linux云计算好学吗?Linux云计算运维学习资料 Vim编辑器
  10. kaggle房价预测代码