Archive for March, 2010
Free Ticket to the jQuery Conference in San Francisco
Tuesday, March 30th, 2010
On April 24-25, I’ll be attending the jQuery conference in San Francisco, and I hope many of you will as well! With speakers like John Resig, Steve Souders, Rey Bango, and other highly respected members from the jQuery team, it’s sure to be a fantastic event! Now, if you haven’t purchased your ticket just yet, the team has given me one ticket to give away for free.
“The San Francisco Bay Area conference is the second of four events planned by the jQuery Project in 2010. The first was the jQuery14 event, and additional conferences are being planned in Europe and on the East Coast for later this year.”
Hosted at Microsoft
“The San Francisco Bay Area conference will be held at the Microsoft Silicon Valley Research Center in Mountain View, California. This venue is the largest that the project has worked with to date (Harvard Law School in ’07, the MIT Stata Center in ’08 and Microsoft New England Research Center in ’09) and we expect to sell out very quickly.”
How to Enter
The winner will receive one complimentary ticket to attend the jQuery conference. He or she will still be responsible for accommodations, and/or travel expenses, per usual. If you’re in or near the Bay area of San Francisco, and would LOVE to attend, leave a comment of no more than three sentences telling us why.

If you aren’t selected to win the free ticket, admission is only $199, which is quite cheap when you consider the level of knowledge that you’ll gain as a result of attending.
I’ll be attending, and will hope to see you there! Will you be going?
learn, php, rss, tutorials
PHP Tutorials | No Comments »
Animus Tell a Friend
Tuesday, March 30th, 2010
This script let's visitors to your web site quickly and easily recommend your site to their friends. It has features that allow you to send a thank you e-mail when it is used, and can be configured to send you an e-mail just to let you know that it was used. It has built-in protection to prevent itself from being repurposed by spammers to send their e-mails as well. rss
Free PHP Scripts | No Comments »
arnab manja
Tuesday, March 30th, 2010
Perkongsian informasi, merintis reformasi aktif dunia penternakan arnab Malaysia rss
Free PHP Scripts | No Comments »
Form Champ Easy Form Builder and Processor with Captcha
Tuesday, March 30th, 2010
Easily create, build and generate forms that send emails with an online form wizard interface. When done just copy paste the code to your website, blog, facebook or myspace page and the form will be ready and working. Free membership has unlimited form submissions, captcha protection, ability to add file upload files and attachments to forms, success page, error, page, validation and more! Suitable to create contact forms, feedback forms, email forms, event registration forms, online polls, and any kind of php and html forms. Free to use! rss
Free PHP Scripts | No Comments »
Mi-Dia Blog
Tuesday, March 30th, 2010
Mi-Dia Blog is a purpose built and open source Blogging and CMS script written in the dynamic php language. Designed with a ballance of simplicity and ability, this software is a great tool aimed at everyone interested in blogging and content publishing. Features Multiple Databases Support Simply share split information into two databases, but it still works great with just one. Multiple Authors The owner of the blog can be the sole poster, assign additional authors ( Staff ) or even allow all registered users to post. Commenting Visitors can comment on posts by registering for a User Account. Full User Registration A full user registration system that allows users to maintain their accounts and post comments. Content Sharing Sharing your work around the web is easy with built in support for publishing your content to popular social networking and bookmarking websites including Twitter, Digg and Stumble Upon. Validity Verified and Compatible XHTML, CSS and RSS ensures your blog works perfect in all browsers. Templates Customize your Blog with Templates and even allow user's to view the content in their own preferred template. Pages Set your own additional static pages through the Staff Panel Security Your content and blog is protected by many systems, including upper level permissions and dual log in to access the non public area's. Control With the functional and secure Staff Panel, you can control everything on your blog through well designed pages. rss
Free PHP Scripts | No Comments »
Diving into the Twitter API
Tuesday, March 30th, 2010
Twitter’s astonishing growth is rivaled only by its intuitive, developer friendly API. In this second part of the series, we are going to learn more about Twitter’s API and how to work with it.
Too much abstraction is never a good thing.
In this Web 2.0 era, web applications which have an easy to use, intuitive API have a distinct advantage as it lets developers exploit and build for the platform and thus capture more users. As we move towards the social web and mashups, a good API is not a nice addition anymore: it is downright necessary. And remember, too much abstraction is never a good thing. While there are a number of API kits out there to simplify working with the API in question, wouldn’t it be cool to know what is actually going on under the hood? Wouldn’t it be exciting to deconstruct the actual voodoo going on between the kit and the API? Yeah, I thought so!
Before we Begin
For each functionality, I am going to show you how to achieve it in two ways: the first using a server side language, PHP in this case, and the second using only a client side language, JavaScript. Each implementation will function separately and achieve the required functionality. Feel free to mix and match these solutions to create a hybrid. The JavaScript examples will use JSON as the format for the payload while for the PHP example I’ve chosen XML.
Twitter API Basics
The first thing you need to know about the Twitter API is that it is RESTful. To cut the jargon, it means you access appropriate URLs using GET or POST requests to modify, request or manipulate the data exposed by the API.
There are three separate Twitter APIs actually.
- The normal REST based API
- The Search API
- The Stream API
Each of these APIs have their own distinct set of functionality, quirks, advantages and disadvantages.
REST API
The REST methods constitute the core of the Twitter API, and are written by the devs at Twitter itself. It allows other developers to access and manipulate all of Twitter’s main data. You’d use this API to do all the usual stuff you’d want to do with Twitter including retrieving statuses, updating statuses, showing a user’s timeline, sending direct messages and so on.
Search API
The search API is actually the brainchild of the guys over at Summize Inc, a company which Twitter acquired for its API. The search API lets you look beyond you and your followers. You need this API if you are looking to view trending topics and so on.
Stream API
Finally we have the stream API. This API lets developers sample huge amounts of real time data. Since this API is only available to approved users, we aren’t going to go over this today.
Authentication and all that Boohockey
Public data can be freely accessed without an API key. When requesting private data and/or user specific data, Twitter requires authentication. You can authenticate with Twitter using either of two methods.
Basic Authentication
This is the default method of authentication Twitter initially launched with and still uses. In this method, you pass the user name and password as Bse64 encoded strings in the header of the HTTP request. A sample GET request would look like so.
GET /somefolder/index.html HTTP/1.1 Host: net.tutsplus.com Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Looks a tiny bit complicated doesn’t it? Let me explain. The first two lines are part of a standard GET request. The third line is where all of magic happens. We make sure the server knows we are using basic auth to authenticate with it, and then pass in the base 64 encoded string of username:password as the value. The server can then decode the credentials, check them and provide access if everything matches.
Twitter supporting basic auth is probably one of the reasons it got so big since it let a multitude of developers develop for the platform since the barrier of entry is relatively low. Basic auth is sufficient to retrieve all parts of the API.
OAuth
Twitter started supporting OAuth in the second quarter of 2009. Users using OAuth enabled application need not divulge their passwords to the program and can also delegate control to the program with multiple levels of access. However, we’ll not look at Twitter’s OAuth implementation today. OAuth and implementing its workflow is a rather complex topic and requires an article of its own. I’d rather skip over OAuth for this article rather than throw out some half baked explanations and writing code based on those explanations.
Caution
“Twitter only lets you make a predefined number of calls to its API.”
Before we delve in to the coding part, I need to make one thing absolutely clear: Twitter only lets you make a predefined number of calls to its API. The REST API’s limit is 150 for an hour, while the search API’s limit is undisclosed as of now. For authenticated calls, the call is deducted from the authenticating user’s limit, while for unauthenticated calls it is deducted from the calling IP’s quota.
Remember, when developing an application, make sure you either cache the information or respect the limit and stay within it. If you think the preset limit is insufficient for your application, you could always apply for white listing.
The Most Common Tasks
Instead of looking at a number of methods you probably won’t ever use, I’ve decided to show you how to do the three most common tasks.
- Retrieving your status
- Updating your status
- Searching Twitter
I think this is a proper subset of the API which directly covers what the API is most used for without spreading itself too thin by covering a ton of methods. With that out of the way, let’s get started!
Retrieving your Status
The first functionality we are going to look at is displaying a user’s status. This is the most popular use of the API if a web developer wants to display a given user’s status without resorting to a Twitter badge.
PHP Method #1: Direct Retrieval
<?php
$response = new SimpleXMLElement('http://twitter.com/users/show/userid.xml',NULL,TRUE);
echo $response->status->text.' - '.date("g:i: A D, F jS Y",strtotime($response->status->created_at));
?>
The first method utilizes PHP. It’s a relatively simple method where we just grab the user specific XML file, and then parse it to display the current status.
We first convert the XML file into an object so we can easily traverse through it using the SimpleXML extension which comes standard with PHP. Now that the object has been created, we just find the status node, and print out the status text and creation time.
Remember, this only displays the current status of the user. If you want a list of recent updates, see below.
PHP Method #2: cURL
cURL is a command line tool and comes enabled on most hosted servers. To quote Wikipedia:
In computing, cURL functions as a command-line tool for transferring files with URL syntax.
It is a library designed to allow users to connect to different types of servers using different protocols. Using cURL is the method of choice for all the Twitter libraries out there and we’ll be using the same.
<?php
$username = 'xxx';
$password = 'yyy';
$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "http://twitter.com/statuses/user_timeline.xml");
curl_setopt($curlhandle, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curlhandle);
curl_close($curlhandle);
$xmlobj = new SimpleXMLElement($response);
foreach ($xmlobj->status as $status)
{
echo $status->text.'<br /> by'.$status->user->screen_name.' at '
.date("g:i: A D, F jS Y",strtotime($status->created_at)).'<br /> <br /> ' ;
}
?>
Let me explain. First, we assign our username and password to separate variables to be used later. I then set the URL to the value mentioned above. Since the method requires authentication, we send in our username and password.
Next, we execute our call and store the response in a seperate variable. We then convert it into a valid XML object and then parse the data, printing out the status and when it was posted.
I’ve formatted the data and time to look the way I want. If you want fancy dates and times like 3 hours ago or 12 days ago, you either need to look for a snippet or code one yourselves. Twitter only returns normally formatted data and time.
You’ll receive an output, similar to this:
I am stupid. Didn't use an IDE and missed a comma wasting 110 minutes. Remind me not to try debugging when I am sleepy or tired. at 6:01: PM Fri, February 14th 2010 Flood of traffic. :O I must have budgeted for this many visitors. Argh! Hope nothing breaks. at 8:51: PM Thu, February 13th 2010
JavaScript
Using JavaScript to display your status is the most elegant way to go forward since you can load it asynchronously after the page has loaded, which means, even if Twitter is downm or being tardy, your site functions the same.
We are going to use jQuery’s ajax method to do all our dirty work today.
$.ajax({
url : "http://twitter.com/statuses/user_timeline/userid.json?callback=?",
dataType : "json",
timeout:15000,
success : function(data)
{
$("#data").html("Data successfully obtained! <br />");
for (i=0; i<data.length; i++)
{
$("#data").append("<p>" + data[i].text) +"</p>";
$("#data").append("<p>" + data[i].created_at +"</p>");
}
},
error : function()
{
alert("Failure!");
},
});
Again, a big chunk of code but, broken down, it’s simple really. We use jQuery’s lowest level AJAX function instead of the getJSON method, since the low level call seems to be more versatile.
First up, we define the URL and the datatype. We also add a callback function to the URL to circumvent the cross domain restriction on most browsers. Without this callback, our script wouldn’t run; it’d just return an error and quit.
I’ve chosen not to authenticate, because we are specifying an ID in the URL, and hence don’t need authentication – that and because basic auth is not really safe for sensitive information. You don’t want to send out your password over an insecure line.
Finally, the success function which is called when no errors are encountered. We just parse the returned JSON object and print out the text and the creation time. #data is just a container where we put all our data.
A Quick Note
This code is the template for all your methods which access data. Very minimal change is required to modify it to work with other API methods.
For the PHP example, all you’d need to do is change URL value to point to a new method, and you should be mostly done. If the method requires parameters, you just add them directly to the URL itself. Simple as that.
Same with the JavaScript example. All you’d need to do is change the URL that the methods request, and you should be done. Be sure to fetch only public data with the JavaScript method.
Remember, this code is the base for all your other methods which retrieve data. This includes methods getting your direct messages, timelines, friends, followers and mentioned tweets. Just change the url, add in a parameter as needed, and you’re all set! Easy, no?
Updating your Status
With any Twitter application you are creating, letting users update their status through it is a no-brainer. Remember, previously applications using basic auth were able to use a custom source string for all tweets sent from their application. Now, implementing OAuth is the only way you get a custom string. In short, if you want all tweets sent from your application to have a link back to your app, use OAuth. With that out of the way, let’s see the code.
PHP
<?php
$username = 'xxx';
$password = 'yyy';
$status= 'Testing out cURL with the Twitter API';
$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
curl_setopt($curlhandle, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlhandle, CURLOPT_POST, 1);
curl_setopt($curlhandle, CURLOPT_POSTFIELDS, "status=$status");
$response = curl_exec($curlhandle);
curl_close($curlhandle);
?>
We can use cURL just as easily to post to Twitter. The code is almost the same as before except we change the url to the appropriate one. Also, we make sure posting is enabled, and set the fields to be posted. The API method requires a status parameters, and so we set the value of the status variable to this parameter.
Do remember to make sure the text to be posted is UTF-8 encoded. Otherwise, we’d run into needless errors.
We now save the response to be used for later. In this example, I’ve chosen to do nothing. But in a practical application you’d definitely want to display a success/error message and/or display the response.
JavaScript
Posting a status update to Twitter using only JavaScript seems to be impossible right now since there is no way to pass the user’s id and password. With Flickr, those credentials are passed on as part of the POST request itself. With Twitter, this data needs to be sent in the HTTP headers, and there doesn’t seem to be a way of doing that.
Even disregarding the fact that you can’t send credentials over to the service, there is still the problem of not being able to make cross domain POST requests with the XMLHttp object. These two points make API methods which require a POST request a strict no-no with JavaScript.
In case you are interested, a successful post nets you this response.
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<status>
<created_at>Fri Aug 14 21:31:53 +0000 2009</created_at>
<id>3316091255</id>
<text>Testing out cURL with the Twitter API</text>
<source><a href="http://apiwiki.twitter.com/" rel="nofollow">API</a></source>
<truncated>false</truncated>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<favorited>false</favorited>
<in_reply_to_screen_name></in_reply_to_screen_name>
<user>
<id>18118645</id>
<name>Tony / Siddharth</name>
<screen_name>lordtottuu</screen_name>
<location>India</location>
<description>Gamer, developer, web designer, writer, geek. </description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/300532536/NVAGSoNIpS_o5XxbmB4pl-boGheY6JBASaiLiieGIAO6JTitHnRMNJa6ktCPRMQW_normal.jpg</profile_image_url>
<url>http://ssiddharth.com</url>
<protected>false</protected>
</user>
</status>
A Quick Note
The code techniques discussed above consist of the base for all your data which sends data to Twitter. This includes methods which lets you follow/unfollow someone, send direct messages, creating favorites, blocking people, and so on and so forth.
The only thing you’d need to do is change out these URLs, see whether they require extra parameters, and add them as needed. Nothing else required.
Searching Twitter
Letting users search through Twitter for information is potentially an important need of an application. For this purpose, we can use the search API to query Twitter.
PHP
<?php
$searchstring = "Envato";
$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "http://search.twitter.com/search.json?q=$searchstring");
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curlhandle);
curl_close($curlhandle);
$json = json_decode($response);
foreach ($json->results as $result)
{
echo $result->text;
}
?>
The above code lets you search Twitter for tweets which reference Envato. As usual, we change the URL to point to the correct API method and proceed. Since this method is only available in either JSON or ATOM formats, I’ve chosen to go with JSON.
I’ve used PHP’s built-in json_decode function to convert it into an object so we could parse the response easily. In this example, I’ve only printed out the tweets themselves. In your applications, you’d probably want to display more. A sample response is below.
[text] => @nsethi check out http://www.envato.com/ if you want some sick tuts...I'm gonna blog about it later. [to_user_id] => 1273919 [to_user] => nsethi [from_user] => thinklime [id] => 3315720513 [from_user_id] => 33020944 [iso_language_code] => en source] => Tweetie [profile_image_url] => http://s3.amazonaws.com/twitter_production/profile_images/201032569/idTwitter_normal.jpg [created_at] => Fri, 14 Aug 2009 21:10:42 +0000
As you can see, a lot of info about the user and the tweet itself is available. Feel free to mix and match.
JavaScript
$.ajax({
url : "http://search.twitter.com/search.json?q=somestring&callback=?",
dataType : "json",
timeout:15000,
success : function(data)
{
// parse data here
},
error : function()
{
alert("Failure!");
},
});
As usual, we use the ajax method to ping Twitter’s search service. Just as in the previous example, we’ve included a callback function to get over the cross domain restriction.
A sample response, plucked directly from Twitter, looks like so.
{"results":[
{"text":"@twitterapi http:\/\/tinyurl.com\/ctrefg",
"to_user_id":396524,
"to_user":"TwitterAPI",
"from_user":"jkoum",
"id":1478555574,
"from_user_id":1833773,
... truncated ...],
"since_id":0,
"max_id":1480307926,
"refresh_url":"?since_id=1480307926&q=%40twitterapi",
"results_per_page":15,
"next_page":"?page=2&max_id=1480307926&q=%40twitterapi",
"completed_in":0.031704,
"page":1,
"query":"%40twitterapi"}
}
As a side note, the search method can be used in a variety of ways really.
- ?q=Envato – Searches for tweets containing the string Envato
- ?q=#Envato – Searches for hashtags of Envato
- ?phrase=Envato+marketplace – Searches for tweets containing the phrase Envato marketplace
- ?q=from:NETTUTS – Returns all tweets from NETTUTS
- ?q=to:NETTUTS – Returns for all tweets to NETTUTS
Why Other Methods Aren’t Covered
I’ve decided to limit myself to showing how to read from and write to a service using its API. This way, I can focus only on those specific methods. And also because most methods build on these basics. If you want to return a friend’s timeline, you’d just change the URL and parse through the response with minimal fuss.
The same occurs if you want to send a direct message to another user or mention another user. The basics of posting is still there. You just need to add in a parameter or two to make it all happen. It doesn’t make sense to cover all that when I can just explain the basics a lot more clearly.
Conclusion
I hope you’ve learned more about how to work with Twitter’s API today. As always, let me know, via the comments, if you need any help!
learn, php, rss, tutorials
PHP Tutorials | No Comments »
OSX Process
Monday, March 29th, 2010
It uses the PHP CLI program to run a PHP script in parallel.
The class can kill the started process at any moment and check if the parallel script is still running.
classes, rss
Classes | No Comments »
Export Spreadsheets
Monday, March 29th, 2010
It performs a query to a given MySQL table and creates an Excel spreadsheet file with the PEAR Spreadsheet_Excel_Writer package from the table data.
The class can add multiple worksheets from results of querying one or more MySQL tables.
Several parameters may be used to configure details like from which table fields map to spreadsheet columns, field sorting, etc..
classes, rss
Classes | No Comments »
[Free] New Free Antivirus Rescue Disk: Recover from a Malware Infection
Monday, March 29th, 2010
A new bootable rescue disk has been added to the Free Antivirus Rescue CDs and DVDs page. If your computer system is compromised by viruses or some other type of malware, booting from a rescue disk may be the only way to clean the infection, since some malware protect their files while Windows is running. The AVG Rescue CD can be burned onto a CD or, in spite of its name, even set up on a USB thumb drive to scan and disinfect your computer.
rss
Uncategorized | No Comments »
Summer of WordPress 2010: Act II
Monday, March 29th, 2010
Scene: A college classroom
Professor: So. Out of the 20 students in the class, half wrote WordPress Summer of Code proposals good enough to receive an A. How many of you are planning to apply for the program?
Jack, a student: I am. They opened applications today.
Sophie, a student: I am. And that sentence was grammatically terrible.
Jack: Shut up.
Chris, a student: I’m not applying.
Jack (to Chris): Chicken?
Sophie: You’re such a jerk! Maybe he has a job lined up or something, did you ever think of that?
Professor: Whoa -
Chris: Actually, I’m going backpacking in Australia with my Dad. No internet for about half the time, and when I emailed the people at WordPress they said I should probably wait until next year to apply and make sure I’d be able to be online through the whole summer.
Professor: Fair enough. The application period opens today at 19:00 UTC and goes through April 9th, so let’s hear from the people who are applying.
Jack: I’m submitting mine today.
Sophie: That’s just stupid.
Andrea, a teacher’s assistant: Hey, that’s not necessary.
Jack: Yeah! The early bird gets the worm, or hadn’t you heard?
Sophie: What I heard was that the WordPress mentors are holding open IRC chats this week to talk to prospective students and give them feedback on proposals and ideas, and that talking directly to the mentors ups your chances of being selected. But I guess you don’t think you need the people who are actually choosing the students to know your name because your proposal is so brilliant?
Jack’s jaw drops.
Jack: Where did you hear that? It wasn’t on the GSoC mailing list.
Sophie: I joined the wp-hackers list and asked all the core contributors for feedback on my idea, and then I emailed 3 potential mentors to see what they thought of it personally. By the time applications are due, I’ll have revised it based on community and mentor feedback, and enough people will know who I am — and that I’m full of initiative — that my chances of being accepted will be much better.
Jack: You think you’re all Felicia Day with your MW2 level 70, but you’re just a computer nerd.
Sophie: Um, duh. We’re in an advanced computer programming class. We’re all computer nerds.
Professor: Now, now. Sophie’s correct; talking to community members and mentors will improve her chances. But, Jack, there’s no reason you can’t join the IRC chats and the mailing list to get your name out there, too, even if you submit your application today. Most proposals get tweaked a bit after the students are chosen anyway.
Sophie: Plus, Felicia Day is awesome! And she uses WordPress, so ha!
End Act II.
Here’s the deal. The application period opens today. Early applications will likely get a bit more attention up front, but it’s also important that your ideas and approach are vetted by the community and the mentors. If you haven’t already, you should join the wp-hackers mailing list and send your proposal to the list for feedback. We’ll also be doing a few IRC chats during the application period to give students a chance to talk directly with the mentors. Note that not every mentor will attend all three chats, so if you want to talk to a specific person, you might want to email them. Please arrive on time to the chats, as they will be scheduled for an hour, and will have to accommodate multiple students. IRC chats will be held at irc.freenode.net in room #wordpress-gsoc.
- Wednesday, March March 31 at 20:30 UTC (4:30pm eastern)
- Saturday, April 3 at 21:30 UTC (5:30pm eastern)
- Wednesday, April 7 at 20:30 UTC (4:30pm eastern)
This chat room will remain open during the application period, and various mentors and community members may be there and able to answer questions, but the scheduled chats are the only official times at which they are scheduled to do so.
Oh, and if you want to help publicize the WordPress summer of code, grab a flyer and post it somewhere on a bulletin board at your local college campus. Professors, don’t forget to encourage your brightest students to apply!

