guzzle/guzzle 日常使用
来源:互联网 发布:java bigdecimal取整 编辑:程序博客网 时间:2024/06/14 02:48
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 itecho $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
阅读全文
1 0
- guzzle/guzzle 日常使用
- 使用PHP依赖管理工具Composer安装Guzzle
- PHP HTTP 客户端 - Guzzle
- guzzle中文文档
- laravel框架调用Guzzle
- 什么是PHP Guzzle?
- PHP使用Guzzle依赖实现对外部接口封装
- [Guzzle] PHP HTTP 客户端和框架
- PHP HTTP客户端-Guzzle原理解析
- PHP HTTP客户端-Guzzle原理解析
- The PHP cURL extension must be installed to use Guzzle 错误: ubuntu 安装 php-curl 拓展
- aapt日常日常使用记录
- Subversion 的日常使用
- Eclipse日常使用技巧
- cvs 日常使用 zz
- CVS的日常使用
- oracle日常使用小结
- CVS的日常使用
- 上拉加载
- Thread.currentThread()与this的区别
- 栈
- cdq分治(bzoj 1176: [Balkan2007]Mokia && bzoj 2683: 简单题)
- C语言栈的使用
- guzzle/guzzle 日常使用
- 禁止Ctrl+鼠标滚轴浏览器页面缩放
- Java的堆、栈和垃圾回收
- Android自定义View构造函数、自定义属性详解
- 关于retrofit 传值传null的处理(同样适用于okhttp)
- win7自定义共享文件夹名称
- 机器学习相关定义(更新完监督学习)
- MVVM之DataBinding入门
- 13:反向输出一个三位数