HTTP 통신 방법은 대표적으로 curl 과 Guzzle 이 있습니다.
curl 의 PHP http 통신은 범용적으로 많이 쓰이지만 비동기 처리가 되지 않아 앞의 요청이 끝날 때까지 기다려야 합니다.
그래서 나온 것이 구즐(Guzzle) 입니다. 비동기 처리가 가능한데요, 라라벨에 기본적으로 같이 설치됩니다.
링크 : [바로가기]
curl -sS https://getcomposer.org/installer | php컴포저를 먼저 설치해야 합니다. curl을 기반으로 검색하여 설치하면 도움이 됩니다.
composer require guzzlehttp/guzzle:^7.0$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type')[0];
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
// Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();