How to check if an external service is up and running in php

$url = “”;

$assignlist = @file_get_contents($url); ---- this shows a 504 Gateway Timeout error

If I directly access the url in browser then it shows “NetworkError: 503 Service Unavailable…d server is at capacity” — before calling file_get_contents function is it possible to check whether the external service is available at all or not.

$host = ''; 
$port = 80; 
$waitTimeoutInSeconds = 1; 
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){   
   // It worked 
} else {
   // It didn't work 


Edit : file_get_contents will not work with URIs without allow_url_fopen set to ON.
You should get the content with cURL and this way you can also check if the resource is available (cURL works almost like fsockopen)

You might consider using the get_headers() function.

In the comments of that page, you will notice an example to get the response code.

function get_http_response_code($theURL) {
    $headers = get_headers($theURL);
    return substr($headers[0], 9, 3);

You could then use that like…

$url = "";
$assignlist = '';
if(get_http_response_code($url) == '200') {
    $assignlist = @file_get_contents($url); 

The same could also be done via cURL like vectorialpx mentions.