Danny620 Posted September 26, 2012 Share Posted September 26, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/ Share on other sites More sharing options...
Christian F. Posted September 26, 2012 Share Posted September 26, 2012 OK... What's the question/problem? I recommend that you read this article. Quote Link to comment https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/#findComment-1381006 Share on other sites More sharing options...
Danny620 Posted September 26, 2012 Author Share Posted September 26, 2012 (edited) im just wondering if i server is offline is the responsive 503 or if i dont get a 200 then something has gone wrong is this right? Edited September 26, 2012 by Danny620 Quote Link to comment https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/#findComment-1381027 Share on other sites More sharing options...
DavidAM Posted September 27, 2012 Share Posted September 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/#findComment-1381253 Share on other sites More sharing options...
requinix Posted September 27, 2012 Share Posted September 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/#findComment-1381388 Share on other sites More sharing options...
nishantverma95 Posted October 5, 2012 Share Posted October 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268817-help-with-curlinfo_http_code/#findComment-1382826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.