Feb 15, 2010 09:45 PM
(This post was last modified: Sep 26, 2010 06:46 PM by ChineseFountain.)
Twitter API Tutorial
DoN't RiP!! iMmA wAtChIn YoU!
Ok. :D Lets get started! And by the way, if this tutorial helps you AT ALL, please thank me. :) Its always appreciated!
Note: I got this script off of the twitter website and have slightly modified it to help with the tutorial. :) I am not taking credit for writing this script, I am taking credit for writing this tutorial and explaining how the script works. :)
Part A. - The API Script
The first part of this tutorial is getting the API script up and running.
Spoiler (Click to View)
Lets go through that bit by bit. :)
Code
<?php
function tweet($username,$password,$message){Start the PHP script and create a new function called tweet() with the parameters $username, $password, $message.
Code
$host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));Create a new variable called $host with an API URL and your message.
Code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
curl_close($ch);This is a bit more advanced, and it would take too long to try and explain all of it, so if you want to learn more, read up on php cURL.
Code
if($resultArray['http_code'] == "200"){
$twitter_status='Your message has been sent! <a href="http://twitter.com/'.$username.'">See your profile</a>';
} else {
$twitter_status="Error posting to Twitter. Retry";
}
return $twitter_status;
}
?>Announce if the message was sent or not.
Part B. - The FORM
Ok, now that you understand this script, save that as twitterAPI.php.
Now make a new file called index.php.
Put this inside it:
Spoiler (Click to View)
That script is the form to update your twitter.
Now time to go bit by bit.
Code
//add your twitter username and password below
//don't worry, this is safe, noone but you will see this
$twitter_username ='yourTwitterUserName';
$twitter_psw ='yourTwitterPassword';This has some comments to let you know that this is safe. This also sets variables for your twitter username and password.
Code
require('twitterAPI.php'); //include the twitter API fileCode
if(isset($_POST['twitter_msg'])){
$twitter_message=$_POST['twitter_msg'];//include the message in a variable
if(strlen($twitter_message)<1){ //if the length is less than one
$error=1;
} else {
$twitter_status=tweet($twitter_username, $twitter_psw, $twitter_message); //tweet the damn message!
}
}I'll narrate this line by line.
If there is a variable set called $_POST['twitter_msg'] (not if it is empty or not, but if the server is receiving it then...
Set a variable called $twitter_message including your message.
if the string length is less than 1 then...
set an error message
if not then..
Tweet the message!
end the first if
Hopefully you could understand that! :D
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Send a message to Twitter using PHP</title>
</head>
<body>
<h2>Tweet!</h2>
<p>HackForums RULE!</p>
<?php if(isset($_POST['twitter_msg']) && !isset($error)){?>
<div class="msg"><?php echo $twitter_status ?></div>
<?php } else if(isset($error)){?>
<div class="msg">Error: please insert a message!</div>
<?php }?>
<p><strong>Twitter message:</strong></p>
<form action="index.php" method="post">
<input name="twitter_msg" type="text" id="twitter_msg" size="40" maxlength="140" />
<input type="submit" name="button" id="button" value="post" />
</form>
[b]<!--[/b] END -->
</body>
</html>This includes the form. This is in HTML, and this is a PHP tutorial, so I won't be explaining it.
WOW! Now you've set up your own Twitter API with PHP! But, you may be thinking, "Whats the point of an API, why not go to twitter.com?". So, here are some creative ideas you can use to use the Twitter API.
Part C. - Some Ideas!
- Make a Twitter iPhone app (will need to learn how to use the API in another language)
- Make a Twitter desktop client (will need to learn how to use the API in another language)
- When a user registers on your website, have your script automatically welcome them via twitter!
- When a user on your website has a birthday, wish them a happy b-day via Twitter!
- Make a collaborative twitter account, where if someone has a good idea, then they can submit it, and your script can automatically add it to twitter!
There are many more ways to use this twitter API, and I'm sure you'll figure them out! Good luck and have fun using the Twitter API!
Part D. - Credits
65% me - For writing the tutorial and explaining the code
24% the API programmers - for programming the script I used to explain the API.
10% the Twitter founder - for making this "amazing" service :D
Thank you very much for reading this tutorial! If you spot an error with the code or the tutorial, do not hesitate to PM me and report it. I'll quickly add it and credit you in the above section!
~AndrewP 🎉
![[Image: JcVeD.png]](https://i.imgur.com/JcVeD.png)
“Be Who You Are and Say What You Feel Because Those Who Mind Don't Matter and Those Who Matter Don't Mind.” - Dr. Seuss

