hyperf使用guzzle请求第三方外部接口api

发布时间:2021-12-30 14:20:20 阅读:2741次

在hyperf中如何调用外部接口

安装composer require hyperf/guzzle

代码如下

<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\View\RenderInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use GuzzleHttp\Client;
use Hyperf\Guzzle\CoroutineHandler;
use GuzzleHttp\HandlerStack;
/**
 * @AutoController;
 */
class ViewController
{
    public function index(RenderInterface $render)
    {
        $client = new Client([
            'base_uri' => 'http://www.test.com.cn',
            'handler' => HandlerStack::create(new CoroutineHandler()),
            'timeout' => 5,
            'swoole' => [ // 看这里看这里
                'timeout' => 10,
                'socket_buffer_size' => 1024 * 1024 * 2,
            ],
        ]);

        $response = $client->get('/index/goods/list/is_ajax/1?goods_type=2 ');
        error_log(print_r($response->getBody()->getContents(),true),3,"/tmp/1.txt");

        $result = $response->getBody()->getContents();
        return $result;
    }
}

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:hyperf使用guzzle请求第三方外部接口api 出自老鄢博客 | 欢迎分享