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