Tips And Resources For Software Consultants

Twitter API Programming Tutorial With PHP – Intro

<img src="" alt="picture-15" title=&uuot;picture-15" width="400" height="125" class="alignnone size-full wp-image-85" />

I have recently started using Twitter and completely fell in love with it. Being a programming, this naturally made me want to develop applications for interfacing with it. So, I scoured the internet (checking Twitter’s website as well) to find good *simple tutorials for doing this. After searching for a while, I couldn’t really find any. Ok, I lied, I found a few but they were terrible. I hate tutorials that assume the reader knows more than they actually do.

So, I am taking it upon myself to write a series of comprehensive tutorials for interfacing with Twitter. I will first do them all in PHP and then maybe in some other languages. I may even do some in Objective-C to be implemented on the iPhone. These would be written of course at 😉 .

Let’s just jump right in. Twitter offers a few ways to interface with their web services, which are all documented on their API Wiki. The documentation is great, assuming you know the code to get connected and make the calls. So, let’s skip all of the nerdy low level stuff and write an application.

Today, I will be teaching you how to simply connect to Twitter and update your status. This will be pretty straight forward and require very little PHP code. So, grab some coffee, open up your favorite PHP editor (notepad?).

Ok, so let’s start by wrapping our code into an easy to call function. We don’t want to have to copy and paste our Twitter interface code every time we need it in a project. Wouldn’t it be nice if we could just call it like this

<code class=’php’>updateTwitter("Just Rockin Out")</code>

So let’s begin by declaring a function called updateTwitter. Type the following code</p>

<code class=’php’>function updateTwitter($status){ 
    // Twitter login information 
    $username = "TwitterUsername"; 
    $password = "TwitterPassword";</code>

Our function begins with a declaration of a username and password. This will be your Twitter login information. Every Twitter API call requires that you authenticate yourself. Make sure you update the code to include your username and password/

Next, we will add the following code to initialize the variables needed to make our Twitter API call. Continue by adding the following code.

<code class=’php’>// The url of the update function 
    $url = ‘’; 
    // Arguments we are posting to Twitter 
    $postargs = ‘status=’.urlencode($status); 
    // Will store the response we get from Twitter 
    // Initialize CURL 
    $ch = curl_init($url);</code>

One thing I want to point out is the URL. Notice the update.xml at the end of it. This is telling the Twitter API we want to call the update function and we expect to receive xml back. You could also change it to be update.json if you want to receive json data back.

The next variable postargs is simply the arguments we will pass to the update function. Since these arguments get appended to the URL, they need to be urlencoded. The responseInfo array will contain the return data from the cURL request to Twitter. Finally, we just initialize a new cURL session. cURL is just a protocol for transferring data. You can read up on it on Wikipedia if you feel so inclined.

Next, we need to tell cURL to do a POST rather than a GET and pass it our argument string

<code class=’php’>// Tell CURL we are doing a POST 
    curl_setopt ($ch, CURLOPT_POST, true); 
    // Give CURL the arguments in the POST 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);</code>

The next part is where the magic happens. Here is the next bit of code (I’ll explain it below)…

<code class=’php’>// Set the username and password in the CURL call 
    curl_setopt($ch, CURLOPT_USERPWD, $username.’:’.$password); 
    // Set some cur flags (not too important) 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt($ch, CURLOPT_NOBODY, 0); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // execute the CURL call 
    $response = curl_exec($ch); 
    // Get information about the response 
    // Close the CURL connection curl_close($ch);</code>

Wow! that looks like a lot of nerdy code. Good thing you only have to write it once and don’t have to understand it (unless you want to). The first line sets the username and password fields in our cURL call. When we first connect with the Twitter API, it will prompt for a username and password. cURL will automatically feed the username and password to the API. The next few lines are not super important. If you one of those people that get hung up on that sort of thing, you can read about them here. We’re almost done, I promise… Finally, we make the cURL call itself by calling curl_exec. This will return a response from Twitter which will contains some XML if your call completed successfully. The next line, gets the http response (makes sure you were able to connect to Twitter). If it is anything other than 200 (HTTP OK), it means your cURL request never even reached Twitter. Here is the last bit of code

<code class=’php’>// Make sure we received a response from Twitter 
        // Display the response from Twitter 
        echo $response; 
        // Something went wrong 
        echo "Error: " . $responseInfo[‘http_code’]; 

All this code really does is makes sure we got a 200 code (successfully reached Twitter). If so, it prints out the XML that Twitter returned to us. Now you have a handy-dandy function you can call whenever we want to update your Twitter status from your website. Simply type

<code class=’php’>updateTwitter("Just finished a sweet tutorial on")</code>

and like magic, your Twitter status will be updated. This has many different uses as you can imagine. Join me next time when I will be putting this code into a PHP class as well as implementing the rest of the Twitter API functions. We will then be able to use this Twitter class in a variety of PHP applications. So , be sure to subscribe to my RSS feed and feel free to ask me any questions in the comments section of this post. You can also download the source code of this tutorial here… (insert clever tag line here (iCodeBlog’s is happy iCoding, I need a new one)).

Binary Game – iPhone App Review

Just posted an app review of B1nary Game over at FreshApps. Check it out!

New iPhone Game Programming Tutorial Post On iCodeBlog

Well, it had taken me a while to slot in the time, but I finally finished part 2 of the iPhone Game Programming series iTennis on . The game is shaping up to be pretty cool and I am really enjoying teaching about game programming.

I think that game programming is perfect for teaching people how to program a certain language. This is because games generally encompasses many aspects of programming (interface, logic, math, animation, AI, etc…). The only problem is, they take forever to code/teach. Here are the links for the iPhone Game Programming Tutorials Series:

Be sure to check them out. Let me know what you think. Also, hit me up if you have tutorial suggestions.

Blogging Practices – Frequency and Size of Posts

If you have ever had your own blog, you may have at one point been faced with a question. How often should I write a blog post and how long should each post be?

I have been thinking about this since I started blogging. It is a common thought that you must write a post every single day in order to increase/maintain traffic. But, is this really true? For busy people with families and lives (that aren’t blogging) might have a tough time writing a new content-rich post every single day.

So, would readers rather see less frequent but “better” blog posts or more frequent “weaker” blog posts? Or maybe even a combo of the two?

For example, what if between killer programming tutorials, I wrote short posts about my dog or response to popular articles. Would this benefit my readers or turn them off?

I don’t have a real solution to this but plan to employ some of these strategies while writing this blog. Hopefully, I will be able to draw some conclusions and share them. If you happen to stumble on this post on your journey through the interwebz, please feel free to comment and shed some light on this for me.

The Focus Of This Blog

The purpose of this blog will be primarily to share my programming knowledge through coding tutorials.

I will be writing tutorials based on the programming language that I am currently excited about. These will be anywhere from objective-c to ruby on rails to .NET. Whatever strikes my fancy.

Along the road, I will also be sharing my art (photo and drawing) as well as anything else I find interesting.

So just sit back and enjoy the magic of code!

iPhone App: Buzzword Submitted to the App Store

I have just submitted an iPhone app that I have been developing in my free time. The app is called Buzzword. It’s basically a game similar to the game catchphrase. So wherever you go, you and your friends can bust out a game of Buzzword…

Here are some screenshots:</p>



Once approved, Buzzword will only cost a buck. I’m not kidding, it will probably be the best dollar you have spent and will spend in your entire life. Stay tuned, I’ll be giving out promo codes to people at random…

How to Connect Facebook To Twitter

With the growing popularity of both Twitter and Facebook, one could obviously see a need to link them together. Whether you primarily use Twitter or Facebook, this tutorial will show you step by step, how to update your Facebook status by posting to your Twitter account.

So, how can you update your Facebook status with a text message? Well, Twitter offers this great functionality that allows you to post a “Tweet” via text message. If you combine this with the Twitter app that Facebook offers, you can now update your Facebook and Twitter status simultaneously with one text message (no internet required).

What you will need:

  • Twitter Account
  • Facebook Account
  • Cell Phone Capable of Sending Text Messages

Signing Up For Twitter

The first step is to sign up for a Twitter account if you don’t already have one. Head on over to


Setting Up Text Message Updates On Twitter

You first need to tell Twitter that you want to post from your phone. Start by clicking on Settings in the top right corner of your Twitter page.


Now, select the Devices tab


Enter in your phone number with a “+” in front of it, followed by the country code. So for US phone numbers, it’s +1… Look at the image below for an example.</p>


Twitter will then send your phone a text message containing a unique code. Once you get it, enter it in the box. Now you are able to update your Twitter status by sending a text message to 40404. Whatever you send to this number will be posted to Twitter.

Connecting Facebook to Twitter

Now, you simply need to install the Twitter application on your Facebook account. Log in to Facebook and go to Twitter’s application. You can find it by clicking here or searching for “Twitter” in the search bar. Once on Twitter’s page click “Go to Application”


It will ask you if you want to allow access to the Twitter application on Facebook. Click “Allow”


Now it will ask you to enter your Twitter user name and password. Enter it in and click “Log In”


Now, click on “Allow Twitter To Update Your Facebook Status”


The next screen will confirm. Click “Allow” and you are good to go!</p>

There you have it! It sounds a little complicated, but trust me, updating your Facebook status via text message is very handy. You don’t even have to look at your Twitter account every again (although you should because Twitter is rad). If you have any questions or comments, feel free to leave them in the comments section of this post.



The ultimate party game has arrived in the App Store! If you are a fan of the game Catch-Phrase, then you will love Buzzword.

In this fast-paced game, do whatever it takes to get your team to guess the word or phrase. Give them clues – talk, use your hands, jump around. Once they guess, pass it quick. You don’t want to be caught with the game at the buzz. </p>

Game Controls:

Start/Stop – Starts and stops the timer

**Category **– Selects a category

Team1 – Awards a point to team 1

Team 2 – Awards a point to team 2

**Next **– Selects the next word to describe</p>

Game Rules:

To play the game, players need to split up into two teams. You should sit in a circle alternating every other person as a member on your team.

First, the teams will need to decide on a category in which the words will come from. This is done by pressing the “Category” button. Next, pick a team to begin the game with the first word.

The game starts by pressing the “start/stop” button to start the timer. The player holding the iPhone/iPod will try to describe the word on the screen. They can say and do whatever they want without saying any of the words on the screen, saying “rhymes with”, or saying “starts with”. All of the members on their team will then try to guess the word or phrase the player is trying to describe.

Once the word has been guessed, the iPhone/iPod is passed to the next player on the opposite team. This player will press the “Next” button to receive a new word to describe.

The game is played until the buzzer goes off. At this point, the team NOT holding the iPhone/iPod gets a point. This is done by pressing either the “Team 1” or “Team 2” buttons. The first team to 7 wins!</p>

If you have any questions, suggestions, or bug reports, feel free to comment on this page or write me on Twitter (

Download Buzzword In The App Store