Guzzle的简单使用

概述

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送HTTP请求。
中文文档地址:https://guzzle-cn.readthedocs.io/zh_CN/latest/index.html

环境要求

  • PHP >= 5.5.0
  • 使用PHP的流, allow_url_fopen 必须在php.ini中启用。
  • 要使用cURL,你必须已经有版本cURL >= 7.19.4,并且编译了OpenSSL 与 zlib。

安装

安装方法一:使用composer安装(略)
安装方法二:直接下载,require。

1.进入下载页面

进入网址:https://php-download.com/,在搜索框中搜索“guzzle”,点击“GO TO DOWNLOAD”按钮:

2.选择版本

3.下载并解压:

简单示例

发送GET请求

<?php 
require_once("vendor/autoload.php"); 
use GuzzleHttp\Client;

$client = new Client(['verify'=>false]);
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'password']
]);
//获取响应状态码
echo $res->getStatusCode().'<hr>';
//获取响应头部信息
$header = $res->getHeader('content-type');
echo $header[0].'<hr>';
//get all headers: $res->getHeaders();
//获取响应主体
echo $res->getBody();

结果如下:

发送一个异步请求

<?php 
require_once("vendor/autoload.php"); 
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client(['verify'=>false]);
$request = new Request('GET', 'http://httpbin.org/get');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});
$promise->wait();

结果:

I completed! { "args": {}, "headers": { "Host": "httpbin.org", "User-Agent": "GuzzleHttp/6.3.3 curl/7.50.1 PHP/5.6.25" }, "origin": "118.122.250.228, 118.122.250.228", "url": "https://httpbin.org/get" }

发送一个POST请求

<?php 
require_once("vendor/autoload.php"); 
use GuzzleHttp\Client;

$client = new Client(['verify'=>false]);
$res = $client->request('POST', 'http://httpbin.org/post',[
    'form_params' => [
        'field_name' => 'abc',
        'other_field' => '123',
        'nested_field' => [
            'nested' => 'hello'
        ]
    ]
]);
echo $res->getBody();

结果:

{ "args": {}, "data": "", "files": {}, "form": { "field_name": "abc", "nested_field[nested]": "hello", "other_field": "123" }, "headers": { "Content-Length": "61", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "GuzzleHttp/6.3.3 curl/7.50.1 PHP/5.6.25" }, "json": null, "origin": "118.122.250.228, 118.122.250.228", "url": "https://httpbin.org/post" }

发送并发请求

有时候我只想发送一个请求,并不想获取到响应结果,例如发送短信,发送邮件等。那么可以将timeout参数设置为小一点,例如1秒。

<?php 
require_once("vendor/autoload.php"); 
use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client([
    'base_uri' => 'http://php70.jay520.com/',
    'timeout'  => 1,
    'verify'=>false
]);

// Initiate each request but do not block
$data =  [
    'form_params' => [
        'field_name' => 'abc',
        'other_field' => '123',
        'nested_field' => [
            'nested' => 'hello'
        ]
    ]
];
$promises = [
    'post1' => $client->requestAsync('POST','/guzzle/post1.php',$data),
    'post2' => $client->requestAsync('POST','/guzzle/post2.php',$data),
    'post3' => $client->requestAsync('POST','/guzzle/post3.php',$data),
];

// Wait on all of the requests to complete.
//$results = Promise\unwrap($promises);
$results = Promise\settle($promises)->wait();
echo 'finish';exit();

《Guzzle的简单使用》上有2条评论

  1. 我想知道发送了一个异步请求,但是我怎么确定这个异步的接口请求成功获取了数据,怎么渲染到blade页面上

发表评论

邮箱地址不会被公开。 必填项已用*标注