laravel框架中使用GuzzleHttp并发请求多个接口

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。

  • 接口简单:构建查询语句、POST请求、分流上传下载大文件、使用HTTP cookies、上传JSON数据等等。
  • 发送同步或异步的请求均使用相同的接口。 使用PSR-7接口来请求、响应、分流,允许你使用其他兼容的PSR-7类库与Guzzle共同开发。
  • 抽象了底层的HTTP传输,允许你改变环境以及其他的代码,如:对cURL与PHP的流或socket并非重度依赖,非阻塞事件循环。
  • 中间件系统允许你创建构成客户端行为。
use GuzzleHttp\Client;
use GuzzleHttp\Promise;


class MyService extends BasicService
{
    public function getCityListOther()
    {
        $url = Common::loadConfig('finance', 'getCityUrl', true);
        $client = new Client(['connect_timeout' => 3, 'timeout' => 5,]);

        $promises = [
            'buying'  => $client->getAsync($url.'?businessType=1'),
            'license' => $client->getAsync($url.'?businessType=2'),
            'deliver' => $client->getAsync($url.'?businessType=3'),
        ];
        $results = Promise\unwrap($promises);

        $myInfo1 = (string)$results['buying']->getBody();
        $myInfo2 = (string)$results['license']->getBody();
        $myInfo3 = (string)$results['deliver']->getBody();

        $buyingArr = json_decode($myInfo1, true);
        $licenseArr = json_decode($myInfo2, true);
        $deliverArr = json_decode($myInfo3, true);
        var_dump($buyingArr,$licenseArr,$deliverArr);
        }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值