PHP GuzzleHttp 使用


PHP GuzzleHttp 使用

 

namespace app\service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use think\facade\Log;

class QqpayService
{
    private $result = '';

    function test() {

        $url    = url('index/test/qf', '', true, true);
        $client = new Client();
        $result = '';
        // 1.GET同步请求
        $res = $client->request('GET', $url, [
            // post 提交参数, Request::param() 获取
            'query'   => [
                'name' => 'lobtao',
            ],
            // Request::header() 获取
            'headers' => [
                'name'         => '我是乖乖',
                'content-type' => 'application/json',
            ],
        ]);

        $this->result = $res->getBody()->getContents();


        // 2.POST同步请求
        $res = $client->request('POST', $url, [
            // post 提交参数, Request::param() 获取
            'form_params' => [
                'name' => 'lobtao',

            ],
            // Request::header() 获取
            'headers'     => [
                'name' => '我是乖乖',
            ],
        ]);

        $this->result = $res->getBody()->getContents();

        // 3.异步请求
        $request = new Request('POST', $url, [
            'name' => '我是乖乖',
        ], json_encode([
            'name' => 'lobtao',
        ]));
        $promise = $client->sendAsync($request)->then(function ($response) {
            // 请求完成后的操作
            $this->result = $response->getBody()->getContents();
        });
        $promise->wait();


        return $this->result;

    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM