handling Twitter API response codes (cURL, PHP)

A quick switch structure in PHP for handling Twitter error codes when using the Twitter API.

$val = $resultArray['http_code'];
switch ($val) {
case "200":
echo "Tweet sent.";
case "304":
echo "There was no new data to return.";
case "400":
echo "The request was invalid."
case "401":
echo "Invalid login credentials.";
case "403":
echo "The request is understood, but it has been refused."; // An accompanying error message will explain why. This code is used when requests are being denied due to update limits.
case "404":
echo "Invalid user name.";
case "406":
echo "Not Acceptable."; //Returned by the Search API when an invalid format is specified in the request.
case "500":
echo "Twitter internal server error.";
case "502":
echo "Twitter is down or being upgraded.";
case "503":
echo "The Twitter servers are up, but overloaded with requests. Try again later.";
echo "Communication error. Please try again.";

This script can be used to handle the return codes from tweeting via cURL. Simply replace the

if ($resultArray['http_code'] == 200)
echo ‘Tweet Posted’;
echo ‘Could not post Tweet to Twitter right now. Try again later.’;

section of the tweeting script with the script from this post, and you’ll be sorted. Nothing much more to say, I couldn’t even think of comments for the script because it’s self-explanatory.


About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

One Response to handling Twitter API response codes (cURL, PHP)

  1. Pingback: PHP script to automate tweeting « whileloop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: