phasync
HTTP client / NetworkException

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#

Example 1Connection refused
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)
Example 2
try {
    $response = $client->sendRequest($request);
} catch (\Psr\Http\Client\NetworkExceptionInterface $e) {
    // retry later; $e->getRequest() is the request that failed
}

See also#