Exception
NetworkException
No response arrived, or the response stopped arriving, because of a network failure.
Introduction#
Thrown for a refused or reset connection, a DNS failure, a TLS failure and a timeout. When the
failure comes after the response headers, it is thrown from reading the body instead of from
sendRequest(). The code is cURL's error number, such as CURLE_OPERATION_TIMEDOUT.
Class synopsis#
class NetworkException extends ClientException implements \Throwable, \Stringable, ClientExceptionInterface, NetworkExceptionInterface {
}Examples#
use phasync\HttpClient\HttpClient;
use phasync\HttpClient\NetworkException;
use phasync\Psr\Request;
// A port nothing listens on
$socket = stream_socket_server('tcp://127.0.0.1:0');
$url = 'http://' . stream_socket_get_name($socket, false) . '/';
fclose($socket);
try {
(new HttpClient())->sendRequest(Request::create('GET', $url));
} catch (NetworkException $e) {
echo get_class($e), "\n";
echo 'cURL error ', $e->getCode(), ' (CURLE_COULDNT_CONNECT is ', CURLE_COULDNT_CONNECT, ")\n";
}Output
phasync\HttpClient\NetworkException cURL error 7 (CURLE_COULDNT_CONNECT is 7)
try {
$response = $client->sendRequest($request);
} catch (\Psr\Http\Client\NetworkExceptionInterface $e) {
// retry later; $e->getRequest() is the request that failed
}See also#
RequestExceptionThe request could not be sent because it is invalid.ClientExceptionA request failed for a reason that is neither an invalid request nor a network failure.HttpClient::sendRequestSend a PSR-7 request with the client's default options.