Hello everyone this is the first big tutorial at flash rhino and its going to be a cool one. Today we are going to be using the twitter API to post a message to twitter.
Ok lets start!
Start by making two (2) files one named
twitter_message.php (this will be where we type in our 'tweet') and another named
parse_tweet.php (this is where the real action will happen).
First off we are going to be working with the twitter_message.php page so no need to worry about the other one, so open it up and first of all we need to type this in:
We are setting up the variables of the twitter name and pass of the account which it will be posted to.
(replace NAME and PASSWORD with your twitter name and password.
Make sure you open the php with a php tag "$twitter_username ='NAME';
$twitter_password ='PASSWORD';
Now we have defined the variables we need to contact the other file that will post the message to twitter to we need to use phps require function.
Insert this after the last piece of code:
require('parse_tweet.php');
So next off we are going to put somthing in a unction that we will be creating later (i know we are doing it in a weird order but who cares)
so post this under your other code:
if(isset($_POST['twitter_msg'])){
$twitter_message=$_POST['twitter_msg'];
if(strlen($twitter_message)<1){
$error=1;
} else {
$twitter_status = postToTwitter($twitter_username, $twitter_password, $twitter_message);
}
}
Now make sure you close this off with a php tag "?>"
This will then get the variable that you will be creating later in the text box ($twitter_message) and putting it in the function (postToTwitter()) that you will be creating later in the other file (parse_tweet.php).
Now we need to do some quick html to make the form so we have somthing to type into.
Put this into the 'head' section of your file: (this is the CSS which will make your form look a ittle prettier)
Next we will make the form: (insert this between the "body" tags in your page.
Once you have done that you will need to do a bit more php and then we are done with this page.
Insert this in a pair of php tags: "" at the end.
if(isset($_POST['twitter_msg']) && !isset($error)){
echo '$twitter_status
';
} else if(isset($error)){
echo 'Error: please insert a message!
';
}
So now your file should look like this:
twitter_message.php
___________________________________________________________________________________
Now we have done the twitter_message.php file we can now do the parse_tweet.php file.
This is a simple function script and i will exsplain more in the source codes (top of the page)
Insert this into parse_tweet.php: (inside a pair of php tags ""
function postToTwitter($username,$password,$message){
$host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));
$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);
// Look at the returned header
$resultArray = curl_getinfo($ch);
curl_close($ch);
if($resultArray['http_code'] == "200"){
$twitter_status='Your message has been sended! See your profile';
} else {
$twitter_status="Error posting to Twitter. Retry";
}
return $twitter_status;
}
Your parse_tweet.php should look like this
parse_tweet.php
So when you have done that load both the files to a php enabled server and enter you name and password then it will post to your twitter profile.
Hope you like this tutorial and if you have any problem please download the source codes from the top or you can comment here and i will reply as soon as possible with an answer.