Get HTTP status code of cURL call in PHP
September 10, 2009
With all the fancy cURL-based API’s out there these days (Facebook and Twitter immediately come to mind), using cURL to directly access and manipulate data is becoming quite common. However like all programming, there’s always the chance for an error to occur, and thus these calls must be immediately followed by error checks to ensure everything went as planned.
Most decent API’s will return their own custom errors when an internal problem occurs, but that does not account for issues dealing directly with the connection. So before your application goes looking for API-based errors, they should first check the returned HTTP status code to ensure the connection itself went well.
For example, Twitter-specific error messages are always paired with a “400 Bad Request” status. The message is of course helpful, but it’s far easier (as you’ll see) to find the status code from the response headers and then code for the exceptions as necessary, using the error text for logging and future debugging.
Anyway, the HTTP status code, also called the “response code,” is a number that corresponds with the result of an HTTP request. Your browser gets these codes every time you access a webpage, and cURL calls are no different. The following codes are the most common (excerpted from the Wikipedia entry on the subject)…
200 OK Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.
301 Moved Permanently This and all future requests should be directed to the given URI.
400 Bad Request The request contains bad syntax or cannot be fulfilled.
401 Unauthorized Similar to 403 Forbidden, but specifically for use when authentication is possible but has failed or not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.
403 Forbidden The request was a legal request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference.
404 Not Found The requested resource could not be found but may be available again in the future. Subsequent requests by the client are permissible.
500 Internal Server Error A generic error message, given when no more specific message is suitable.
So now that we know what we’re looking for, how do we go about actually getting them? Fortunately, PHP’s cURL support makes performing these checks pretty easy, they just don’t make the process plain. We need a function called
curl_getinfo(). It returns an array full of useful information, but we only need to know the status number. Fortunately, we can set the arguments so that we only get this number back, like so…
// must set $url first. Duh... $http = curl_init($url); // do your curl thing here $result = curl_exec($http); $http_status = curl_getinfo($http, CURLINFO_HTTP_CODE); curl_close($http); echo $http_status;
curl_getinfo() returns data for the last curl request, so you must execute the cURL call first, then call
curl_getinfo(). The key is the second argument; the predefined constant
CURLINFO_HTTP_CODE tells the function to forego all the extra data, and just return the HTTP code as a string.
Echoing out the variable
$http_status gets us the status code number, typically one of those outlined above.