Guzzle 첫걸음
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();
}
}
Reference
이 문제에 관하여(Guzzle 첫걸음), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/YoshitakaOkada/items/e19377af23b07d1b6571텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)