# Testing a connection with HttpSocket

**URL:** <https://discourse.cakephp.org/t/testing-a-connection-with-httpsocket/1077>\
**Category:** Need Help\
**Created:** [August 31, 2016, 3:18pm UTC](https://discourse.cakephp.org/t/testing-a-connection-with-httpsocket/1077 "2016-08-31T15:18:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinkbug](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@justinkbug](https://discourse.cakephp.org/u/justinkbug)\
**Post date:** [August 31, 2016, 3:18pm UTC](https://discourse.cakephp.org/t/testing-a-connection-with-httpsocket/1077/1 "2016-08-31T15:18:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [September 1, 2016, 2:47am UTC](https://discourse.cakephp.org/t/testing-a-connection-with-httpsocket/1077/2 "2016-09-01T02:47:21Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![justinkbug](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@justinkbug](https://discourse.cakephp.org/u/justinkbug)\
**Post date:** [September 1, 2016, 3:25am UTC](https://discourse.cakephp.org/t/testing-a-connection-with-httpsocket/1077/3 "2016-09-01T03:25:55Z")

</div>

Got it. Thanks for the help mark.
