Guzzle 첫걸음

5102 단어 심포니PHPGuzzle
그냥 메모. 첫 첫걸음을 내디뎠다

GuzzleUtil.php
<?php


namespace App\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;

class GuzzleUtil
{
    private Client $client;

    public function __construct()
    {
        $this->client = new Client(['base_uri' => 'http://httpbin.org','timeout' => 2.0,]);
    }

    public function retrieveGetResponse(): ResponseInterface
    {
        return $this->client->get('/get');
    }

    public function getResponseAsync()
    {
        // Send an asynchronous request.
        $request = new Request('GET', 'http://httpbin.org');
        $promise = $this->client->sendAsync($request)->then(function ($response) {
            echo 'I completed! ' . $response->getStatusCode();
        });
        $promise->wait();
    }
}

GuzzleUtilTest.php
<?php

namespace App\Service;

use PHPUnit\Framework\TestCase;

class GuzzleUtilTest extends TestCase
{
    public function testGetStatusCode()
    {
        $g = new GuzzleUtil();
        $res = $g->retrieveGetResponse();
        $this->assertEquals(200, $res->getStatusCode());

        $g->getResponseAsync();
    }
}


좋은 웹페이지 즐겨찾기