handling Twitter API response codes (cURL, PHP)

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

<?php
$val = $resultArray['http_code'];
switch ($val) {
case "200":
echo "Tweet sent.";
break;
case "304":
echo "There was no new data to return.";
break;
case "400":
echo "The request was invalid."
break;
case "401":
echo "Invalid login credentials.";
break;
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.
break;
case "404":
echo "Invalid user name.";
break;
case "406":
echo "Not Acceptable."; //Returned by the Search API when an invalid format is specified in the request.
break;
case "500":
echo "Twitter internal server error.";
break;
case "502":
echo "Twitter is down or being upgraded.";
break;
case "503":
echo "The Twitter servers are up, but overloaded with requests. Try again later.";
break;
default:
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’;
else
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.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: