Testing a connection with HttpSocket

I’m currently trying to use HttpSocket to grab a page. When the server is down or the URL is wrong, I’m getting errors and execution appears to stop. I though that HttpSocket->request() would return false on failure. Is this not the case?

If not, and I can’t depend on HttpSocket to determine failure of a connection, what is the best way to determine if a connection will fail before using HttpSocket?

HttpSocket will raise an exception when the connection fails. This is because a connection failing is a somewhat ‘exceptional’ event. You can use HttpSocket to test a connection, but you’ll need to catch the exceptions.

$http = new HttpSocket();
try {
  $response = $http->get('http://example.com');
  // Connection worked, do other things.
} catch (SocketException $e) {
  // Connection failed, bad things.
}

Got it. Thanks for the help mark.