4th Jul 2013 07:30am
5,082 views | 0 comments
Flattr this

You should now have the API running with v1.1, your application setup and your keys/secrets all together? Yes? Great, lets begin...

Hopefully your directory structure is currently like this:

+ Root Folder + twitteroauth - OAuth.php - twitteroauth.php

If so, then in the "Root Folder", create a file called "hello.php".

Now we will begin. At first we will need to assign all the keys and secrets as seperate varibles, like so:

<?php // Consumer key $consumerKey = ''; // Consumer secret $consumerSecret = ''; // Access token $OAuthToken = ''; // Access token secret $OAuthSecret = ''; ?>

Within the single quotes, you will need to put your values that're shown on your Twitter application's page.

Now the keys and secrets are varibles, we can continue with the script. We will now need to include the "twitteroauth.php" file, which we've got in the "twitteroauth" folder, like so:

<?php // Consumer key $consumerKey = ''; // Consumer secret $consumerSecret = ''; // Access token $OAuthToken = ''; // Access token secret $OAuthSecret = ''; // Include Twitter OAuth Class include("twitteroauth/twitteroauth.php"); ?>

At this stage everything is set to start with your first Tweet. We now need to initiate the class and have it as a varible, with our current key and secret settings (for our example, we'll use $tweet):

<?php // Consumer key $consumerKey = ''; // Consumer secret $consumerSecret = ''; // Access token $OAuthToken = ''; // Access token secret $OAuthSecret = ''; // Include Twitter OAuth Class include("twitteroauth/twitteroauth.php"); $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret); ?>

At this stage we are practically finished as $tweet is what we can use to send tweets, read tweets, view members, follow members, etc.. Obviously for this tutorial we are just wanting it to Tweet from our account "Hello World". So to send a tweet, you need to tell the class to post a status update and then tell it what you want posting. The example below is only updating the status (but within that array, you can also put other stuff like your longitude and latitude coordinates for a more localised tweet.

<?php // Consumer key $consumerKey = ''; // Consumer secret $consumerSecret = ''; // Access token $OAuthToken = ''; // Access token secret $OAuthSecret = ''; // Include Twitter OAuth Class include("twitteroauth/twitteroauth.php"); $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret); $tweet->post( 'statuses/update', array( 'status' => "Hello World" ) ); ?>

Once you've saved that file and visit it in your browser it should just show a blank page (as we haven't echoed anything onto it), but then if you head over to Twitter, you should publicly see a Tweet from you saying "Hello World".

You can change the message to anything you like, for example:

Tweet Time and Date

$tad = date("jS F Y h:ia",time()); $tweet->post( 'statuses/update', array( 'status' => "Hey, it's currently " . $tad ) );

Tweet Random Number

$tweet->post( 'statuses/update', array( 'status' => rand(1,100) . " is a random number" ) );

And that's that for this part of the Twitter Series. Keep checking back for more updates and tutorials. Want to see a tutorial that will interest you? Then why not contact me and I'll see what I can do for you.

Additional Links
» Twitter OAuth Class (Github)
» Twitter Series Tutorials

Current Directory Structure

+ Root Folder + twitteroauth - OAuth.php - twitteroauth.php - hello.php

 

Tweet ThisShare On FacebookShare On Google ReaderAdd To Del.icio.usShare On DiggShare On RedditShare On LinkedInShare On StumbleUpon

Comments

Post your comment...

Your Name:

Your Email:

Your Comment:

Are You Human?

Tick this box to confirm you're not a bot.

  • © Dale Hay, 2005 - 2016