Jump to content

Recommended Posts

Hi, I Have a client side api receiver my problem is when sometimes the main api server it queryies goes offline from time to time. So what I want to do is check http code and if its not 200 use cached version instead here is the code i have

 

    $con = $this->api_url . $this->request . '&api_key=' . $this->api_key;

    curl_setopt( $ch, CURLOPT_URL, $con );
    curl_setopt( $ch, CURLOPT_HEADER, $this->header );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, $this->follow );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, $this->ssl );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout );

    $response = curl_exec( $ch );

    /* Check for 404 (file not found). */
    $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );

 curl_close( $ch );

    if ( $httpCode == 404 ) {
	    return '404';
    } elseif ( $httpCode != 200 ) {
  return '503';
 }else{
	    return $response;
    }

Link to comment
https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/
Share on other sites

HTTP status codes are three digits. The first digit gives the "overall" status: 2xx codes indicate success (at some level), 3xx codes indicate a redirect (the resource is available somewhere else), 4xx codes indicate the WEB server refuses to provide the requested resource, 5xx codes indicate that the server encountered a serious problem.

 

If the server is offline, then it will not respond at all --- offline means not available to the Internet. So, I don't think you get any status code back from curl. If you really want to know, try a url with a domain that does not exist and see what you get.

 

Note that the function you are using to get the status code will return the "Last received HTTP code", which means if you successfully retrieved another page before the one that timed-out, you may get the status code from the first one, instead of the last one (since the timed-out request will never receive an HTTP code).

 

If you check the PHP Manual for curl_errno(), you see how to check for a TIMEOUT condition.

It's possible you might get a 503 but that's because you were able to contact a machine that sits in front of the actual API server, such as a load balancer or proxy.

 

Keep an eye on the service. If/when it goes down, try to use it and see what happens.

Hi,

I'm running a similar code but CURLINFO_HTTP_CODE is not able find 404s although there are links which give 404.

This is the code i m using:

 

$handle = curl_init($url);

/

curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

 

/* Get the HTML or whatever is linked in $url. */

$response = curl_exec($handle);

 

 

/* Check for all Status Codes and count */

$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

 

 

if ($httpCode== 404) {

$count_404++;

}

 

 

I've checked for a url which has 3-4 404s(using some other tool) but my script is not generating the desired result.Instead i'm guessing its showing these 404s as 0.

Any suggestions please.

Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.