guzzle connectionexception code example
Example 1: guzzle catch exceptions
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
$client = new Client();
try{
$response = $client->request('GET', 'http://github.com');
}
catch (ConnectException $e) {
echo "Internet, DNS, or other connection error\n";
die;
}
catch (RequestException $e) {
echo "Request Exception\n";
die;
}
Example 2: concurrent requests guzzle
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(['base_uri' => 'http://httpbin.org/']);
$promises = [
'image' => $client->getAsync('/image'),
'png' => $client->getAsync('/image/png'),
'jpeg' => $client->getAsync('/image/jpeg'),
'webp' => $client->getAsync('/image/webp')
];
$responses = Promise\unwrap($promises);
echo $responses['image']->getHeader('Content-Length')[0];
echo $responses['png']->getHeader('Content-Length')[0];
$responses = Promise\settle($promises)->wait();
echo $responses['image']['state'];
echo $responses['image']['value']->getHeader('Content-Length')[0];
echo $responses['png']['value']->getHeader('Content-Length')[0];
Example 3: post request data not getting from guzzle laravel
use Illuminate\Support\Facades\Http;
Http::fake();
$response = Http::post(...);