laravel guzzle6 使用

官网:http://docs.guzzlephp.org

1、使用要求

①、php >= 5.5

②、php.ini 中启用 allow_url_fopen

③、curl >= 7.19.4

2、安装

composer require guzzlehttp/guzzle:~6.0

3、发送请求

3.1、快速开始

use GuzzleHttp\Client;

$client = new Client();
$response = $client->request('GET', 'http://xx.com');

$code = $response->getStatusCode();//获取http响应状态码
$data = $response->getBody()->getContents();//获取响应内容

//--GET请求添加参数
$params = [
    'headers' => [//请求头信息
        'token' => 'VBkmpUn8BPMPPNXJnP8CvneYBpQFDdfnetnxQT3C...'
    ],
    'query' => [//附加参数
        'type' => 1
    ]
];
$response = $client->request('GET','http://xx.com',$params);

//--POST请求添加参数
$params = [
    'headers' => [//请求头信息
        'token' => 'VBkmpUn8BPMPPNXJnP8CvneYBpQFDdfnetnxQT3C...'
    ],
    'form_params' => [//附加参数
        'first_name' => '周末啦',
        'last_name' => 'lalala',
        'email' => '123@qq.com',
        'desc' => '周末啦周末啦周末啦周末啦',
    ],
    'json' => [//传数组
        ['name'=>'zs','age'=>10],
        ['name'=>'zs','age'=>10],
        ['name'=>'zs','age'=>10],
    ]


];
$response = $client->request('POST','http://loc.nva.com/app/message',$params);
                

3.2、魔术方法发送

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

3.3、创建请求发送

//创建请求发送
use GuzzleHttp\Psr7\Request;
$request = new Request('PUT', 'http://httpbin.org/put');
$response = $client->send($request, ['timeout' => 2]);

4、异步发送请求

4.1、快速开始

$client = new Client();
$promise = $client->requestAsync('GET', 'https://xx.com');
$promise->then(
    function (ResponseInterface $res) {
        echo $res->getStatusCode() . "\n";
        \App\Utils\CommonTrait::Logs('StatusCode',$res->getStatusCode(),'pine');
    },
    function (RequestException $e) {
        echo $e->getMessage() . "\n";
        echo $e->getRequest()->getMethod();
        \App\Utils\CommonTrait::Logs('Message',$e->getMessage(),'pine');
    }
);
$promise->wait();

4.2、魔术方法发送

$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');

4.3、创建请求发送

use GuzzleHttp\Psr7\Request;

$headers = ['X-Foo' => 'Bar'];
$body = 'Hello!';
$request = new Request('HEAD', 'http://httpbin.org/head', $headers, $body);
$promise = $client->sendAsync($request);
$promise = $client->requestAsync('GET', 'http://httpbin.org/get');

5、同时发送多个请求

即并发请求,通过异步实现,避免每次请求等待再进行下次请求

$url = 'http://xx.com?id=';
$arr = ['1','2','3','4','5','6'];

$promises = [];
foreach ($arr as $k=>$v){
    $tmpUrl = $url.$v;
    $promises[$v] = $client->requestAsync('GET', $tmpUrl,$params);
    //同时发请求:
    //http://xx.com?id=1
    //... ...
    //http://xx.com?id=6
}
$results = Promise\settle($promises)->wait();//响应结果

foreach ($results as $k=>$v){
    if($v['state'] == 'fulfilled'){
        $code = $v['value']->getStatusCode();//响应码
        $data = $v['value']->getBody()->getContents();//响应内容
        dump(['code'=>$code,'data'=>$data]);
    }
}

暂时只会那么多,有机会后面补上,详细见官方文档。

posted @ 2019-07-17 18:12  pine007  阅读(1327)  评论(0编辑  收藏  举报