争取能在百度搜索到把青春献给代码的你;一个年轻程序员的渺小梦想-杨永飞
NOTES DETAIL
Guzzle基本使用发送请求use Gu
 1根小腿毛        100

Guzzle基本使用

发送请求

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => '//httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);

$response = $client->get('//httpbin.org/get');
$response = $client->delete('//httpbin.org/delete');
$response = $client->head('//httpbin.org/get');
$response = $client->options('//httpbin.org/get');
$response = $client->patch('//httpbin.org/patch');
$response = $client->post('//httpbin.org/post');
$response = $client->put('//httpbin.org/put');


设置查询字符串

$response = $client->request('GET', '//httpbin.org?foo=bar');


或使用 query 请求参数来声明查询字符串参数

$client->request('GET', '//httpbin.org', [
    'query' => ['foo' => 'bar']
]);


设置POST表单

$response = $client->request('POST', '//httpbin.org/post', [
    'form_params' => [
        'field_name' => 'abc',
        'other_field' => '123',
        'nested_field' => [
            'nested' => '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();
所属标签: 笔记

3 条评论

  • 咳咳咳咳 2018-02-02 05:04:47

    2016新的一年希望老朋友新朋友都开开心心

  • Mia 2018-01-24 15:12:20

    郑州二七万达出租床位

  • (_知足 2018-01-23 20:19:37

    我回来了!

既来之,说两句吧!

关于我

莫失莫忘,莫惆怅,莫提前生,莫妄后生。知人,知世事,自知,知余于心,力不逮~~

1根小腿毛

浏览最多

话剧《命运之影》如何再现诺

2018-05-26

暂时没有任何描述哦~

陈冠希,一直在音乐

2018-05-26

上海国际艺术节|纪

2018-05-26

照片墙

文件下载

  • 自适应图片大小

    [ 1 ]

  • 贝壳云笔记mac版本

    [ 1 ]

  • 判断移动端设备并且跳转

    [ 1 ]

  • 简体繁体互转PHP

    [ 0 ]

  • phalcon-devto

    [ 0 ]

  • PhalconYang-P

    [ 0 ]