Tuesday, July 28, 2009

Twitter API: How to create a stream of messages Monitter-like with PHP and jQuery


This tutorial illustrates a very simple way to work with the Twitter API in order to implement a search in the Twitter public timeline and display search results with an animated stream of messages (tweets) similar to Monitter. In this example I used PHP, jQuery and a very useful Twitter Search API for PHP based on the work of David Billingham and actually developed by Ryan Faerman. This implementation is very simple to customize and integrate on your project. The result is something linke this:



You can download the full code here and reuse it for free on your projects.



I suggest you to take also a look at these posts:

- Simple PHP Twitter Search ready to use in your web projects
- Super simple way to work with Twitter API (PHP + CSS)
- Send messages from a PHP page using Twitter API


1. A little introduction

This package contains the following files:




- index.php: page with the search form + search results
- search.php: PHP search
- twitterapi.php: Twitter Search API for PHP
- jquery/jquery-1.3.2.min.js: jQuery framework

How it works? After submission the search form calls an ajax request to the page search.php that returns search results into an array. Each element of the array (every single tweet) appears into the div twitter-results with a nice fade-in effect. I set a delay between each tweet equal 2 seconds (2000 ms).



I suggest you to take a look at Monitter, and download the full code to try this tutorial on your local host. Now, take a look at the code.


2. index.php: HTML code

HTML code is very simple. Copy the following code in the tag <body>:

<div class="twitter_container">
<form id="twittersearch" method="post" action="">
<input name="twitterq" type="text" id="twitterq" />
<button type="submit">Search</button>
</form>
<div id="twitter-results"></div>
</div>

...and add into the tag <head> of the page a link to jQuery:

<script type="text/javascript" src="jquery.js"></script>

The result is a simple search form:





3. search.php
Now copy and paste the following code into search.php:

<?php
include('search.php');
if($_POST['twitterq']){
$twitter_query = $_POST['twitterq'];
$search = new TwitterSearch($twitter_query);
$results = $search->results();

foreach($results as $result){
echo '<div class="twitter_status">';
echo '<img src="'.$result->profile_image_url.'" class="twitter_image">';
$text_n = toLink($result->text);
echo $text_n;
echo '<div class="twitter_small">';
echo '<strong>From:</strong> <a href="http://www.twitter.com/'.$result->from_user.'">'.$result->from_user.'</a>: ';
echo '<strong>at:</strong> '.$result->created_at;
echo '</div>';
echo '</div>';
}
}
?>


$result is the array that contains search results. To display all elements of the array (search results) I used this simple loop:

foreach($results as $result){
...
}

... and to get a specific attribute of the array I used this simple code:

$result->name_of_element

Take a look at this post for info about the search on Twitter.


4. index.php: JavaScript Code

Now take a look at the following JavaScript code that enables an ajax request for the search and display results into the page index.php with a fade in effect and a delay between each tweet equal to 2 seconds:

<script type="text/javascript">
$(document).ready(function(){
var twitterq = '';

function displayTweet(){
var i = 0;
var limit = $("#twitter-results > div").size();
var myInterval = window.setInterval(function () {
var element = $("#twitter-results div:last-child");
$("#twitter-results").prepend(element);
element.fadeIn("slow");
i++;
if(i==limit){
window.setTimeout(function () {
clearInterval(myInterval);
});
}
},2000);
}

$("form#twittersearch").submit(function() {
twitterq = $('#twitterq').attr('value');
$.ajax({
type: "POST",
url: "search.php",
cache: false,
data: "twitterq="+ twitterq,
success: function(html){
$("#twitter-results").html(html);
displayTweet();
}
});
return false;
});
});
</script>

This is a very basic implementation you can modify to get a real-time stream of messages for example calling a new ajax request (to search.php) every time the current array with the search results is totally displayed in the page.

That's all. If you have some suggestion please add a comment. Thanks!
You can download the full code of this tutorial here and reuse it on your projects.



Related posts
- Simple PHP Twitter Search ready to use in your web projects
- Super simple way to work with Twitter API (PHP + CSS)
- Send messages from a PHP page using Twitter API

Bookmark and Share
Digg this

Link Exchange


Hi, you all must be knowing the importance of backlinks and its impact on your blog's traffic. Here I start the link exchange programme for this blog.

If you want to display your link here, just place link to my blog and comment here with your blog rul, I'll check whether my link is place or not, and if placed , will immediately place your link here.

Bookmark and Share
Digg this

The complete list Free Blogging Platforms.


Now-a-days,blogging has been sought a great source of earning some good online income.But still many people wonder from where to start.So,here I am providing the complete list of free blogging platform available over the internet -
  1. Aeonity.com
  2. blog.com
  3. blogates.com
  4. blogetery.com
  5. blogger.com
  6. blogr.com
  7. blogsome.com
  8. blogster.com
  9. bravenet.com/webtools/journal/
  10. clearblogs.com
  11. ehow.com
  12. googlepages.com
  13. hubpages.com
  14. iseekblog.com
  15. myspace.com
  16. pbwiki.com
  17. quazen.com
  18. rticlz.com
  19. sampa.com
  20. squarespace.com
  21. squidoo.com
  22. stikipad.com
  23. thoughts.com
  24. tumblr.com
  25. wikispaces.com
  26. wordpress.com
As per the PCWorld.com,top 5 among the are -
  1. Blogger.com
  2. Wordpress.com
  3. Typepad.com
  4. Tripod.com
  5. Squarespace.com

Bookmark and Share
Digg this

Got Error while uploading Template - bX-2nuf8g


After making the first post here,I thought of changing the Blogger template.I never liked the default templates provided here at Blogger.After finding a suitable template (after 2 hrs. of search), I just tried to upload it to my blog , but I got this error message from Blogger - bX-2nuf8g. I tried one more time but same message.I retried for 5 times more,but in vain.

Then I just checked my other blog by uploading the template there, doubting if there is any problem in that specific blog, but still the same error message - bX-2nuf8g.

Then I searched blogger help for this error message.There I got to know that this message ( and every message starting with "bx-" have no specific meaning.They adviced to delete your cookies and cache and retry. But this also didn't help ,then I used 2nd option available there - Report your Error Message to Blogger.

here's the link for that -
http://groups.google.com/group/blogger-help/web/report-your-bx-code-bug-here

Well let's see what Blogger can help me with.

UPDATE:
Solution - Just change your browser,I tried with Apple Safari and got my work done.

Bookmark and Share
Digg this

At last added a Custom Domain from GoDaddy.com to Blogger!!!


As I am blogging since last 1 year I know the importance of having your own domain.

I changed to custom domain on the 2nd day of starting the blog at Blogger,because if I've done that later,it would have resulted in a great drop in my traffic and low rankings on the search engine result pages.

I choose Godaddy.com for registering a domain,because it's cheapest, safe, and has partenered with Blogger for providing custom domains.But it costs $10/domain per year if you purchased it through Blogger.Thats why I purchased it directly from GoDaddy.com using some promo coupons and got the domain registered for 5 years for $35.75 only.

So you have registered a domain with GoDaddy.com, next -
  1. Login to your account at GoDaddy.com .
  2. Click on "Manage Domains".
  3. Click on your domain where you want to host your Blogger blog.
  4. There will be one option availabe - "Total DNS Control And MX Records".(Note- You will not get this option unless you click on your domain.)
  5. There will be a record prefixed - "www",in front of it there will be edit option(image of a pencil), click it.
  6. In the option "Point to Host" - enter this - "ghs.google.com".Please DO NOT change anything. Click ok.
  7. That's it you have added google's server to your domain.
  8. Now if you want to can check whether everything is fine or not,open Command Prompt(Start>Run>Command).
  9. Type - "ping www.yourdomain.com" and press enter.
  10. If everything is ok you will a response like this -
    Pinging ghs.1.google.com [72.14.207.121] with 32 bytes of data: Reply from 72.14.207.121: bytes=32 time=85ms TTL=247 Reply from 72.14.207.121: bytes=32 time=89ms TTL=247 Reply from 72.14.207.121: bytes=32 time=86ms TTL=247 Reply from 72.14.207.121: bytes=32 time=86ms TTL=247 Ping statistics for 72.14.207.121: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 85ms, Maximum = 89ms, Average = 86ms
  11. Now everything is done add your custom domain to your Blogger blog.
This way I got my blog address changed from "the-webmasters-blog.blogspot.com" to "webmasters-blog.com".(I must rather say "www.webmasters-blog.com",because Blogger can add either "www.webmasters-blog.com" or "webmasters-blog.com" to my blog,and I preferred first.)

Update:
I was completely unaware that with Godaddy domain I've got a Free Godaddy Hosting Account,hence I added it to Blogger.
But now I've decided to Host another Wordpress Blog at the Godaddy Domain - www.webmasters-blog.com and also have one Blogger blog => free-blogger-help.blogspot.com

Bookmark and Share
Digg this

How to add a horizontal navigation bar or menu to Blogger?


We have seen a horizontal menu or a navigation bar on many websites and blogs especially the wordpress blogs.But very few Blogger blogs have a horizontal navigation bar like we have here at our blog.



Here are some simple steps for setting up a Horizontal Navigation Bar -

Step 1.-Dashboard > Layout > Edit HTML. Here find this - /* Header */

Step 2.- Add this code below that -
#menu ul li{
font-size:100%;
list-style-type: none;
display:inline;
padding:0px;
margin:10px;
border:0px solid;
}

#menu li a{
color:#dfffed;
}

#menu li a:visited {
color: #57E964;
}

#menu li a:hover {
color: #F88017;
background: #ffff66;
}
You should edit this code after step 6,so that you could know how does it looks like.

(Edit margin values for placement,and various html color values below for respective colours.)

Step 3.- Find this -
b:section class='header' id='header' maxwidgets='1' showaddelement='no'

Step 4.- Change -
  1. showaddelement='no' to showaddelement='yes'
  2. maxwidgets='1' to maxwidgets='4'
Save template and go to Page Elements.

Step 5.- In the Header , Add Page Element > Add HTML .

Step 6.- Leave the title Blank and add the following code in text field -
<div id='menu'>
<ul>
<li><a href="Your URL">Home</a></li>
<li><a href="Your URL">Advertise</a></li>
<li><a href="Your URL">About Me</a></li>
<li><a href="mailto:YOUR EMAIL ADDRESS">Contact</a></li>
</ul></div>
Add your desired values in above code marked red. Save it.

Now you can edit the code in Step 2.

Bookmark and Share
Digg this

Submit your blog to High PageRank Directories.


As many of you know that submitting your blogs to different directories will help you in increasing traffic.

How does it help ?
- By submitting your blog to directories and getting listed there will increase exposure of your blog to Search Engines. Also it increases your PageRank which determines your Search Engine Result Position (SERP),thus getting you more traffic.

Where to Submit your Blogs ?
- Here is the complete list of oneway directories with high Pagerank.
  1. dmoz.org - PR9
  2. 2rss.com - PR7
  3. kids.yahoo.com/submissions/suggest_site - PR7
  4. ExactSeek.com - PR7
  5. Jayde.com - PR6
  6. SuperPages.ca - PR6
  7. Patient.co.uk - PR6
  8. BestCatalog.net - PR6
  9. Gimpsy.com - PR6
  10. Bizweb.com - PR6
  11. Haaba.com - PR6
  12. Canadopedia.com - PR6
  13. Web-Beacon.com - PR6
  14. ClearLeadInc.com - PR6
  15. aigam.com - PR6
  16. BooksMusicVideo.com - PR6
  17. Chiff.com - PR6
  18. WebWorldIndex.com - PR6
  19. IllumiRate.com - PR6
  20. Americasbest.com - PR6
  21. SoMuch.com - PR6
  22. Re-Quest.net - PR6
  23. PsychNet-UK.com - PR6
  24. Clickey.com - PR6
  25. ProudlyCanadian.net - PR6
  26. turnpike.net/directory.html - PR5
  27. Earthstation9.com - PR5
  28. Re-quest.net - PR5
  29. MavicaNet.com - PR5
  30. Greenstalk.com - PR5
  31. Smallerbizz.com - PR5
  32. Emfind.com - PR5
  33. Hoppa.com - PR5
  34. prolinkdirectory.com - PR5
  35. AllTheBizz.com - PR5
  36. Click4Choice.com - PR5
  37. Websavvy.cc - PR5
  38. iMarvel.com - PR5
  39. CyberWebSearch.com - PR5
  40. DC2NET.com - PR5
  41. cantufind.com - PR5
  42. Josh.nu - PR5
  43. verajane.com - PR5
  44. thewebsite***istant.com - PR5
  45. Webs-Best-Directory.com - PR5
  46. MyAllFish.com - PR5
  47. Yeandi.com - PR5
  48. Bontonic.com - PR5
  49. AllFreeThings.comR5
  50. fitnesspros.net - PR5
  51. SmartLinks.org - PR5
  52. charolabap.com - PR5
  53. money-talk.org/directory - PR5
  54. cgbee.com - PR5
  55. findsome.info - PR5
  56. wordforums.net - PR5
  57. xoron.com - PR5
  58. csdir.org - PR4
  59. findingblog.com - PR4
  60. linkaddurl.com - PR4
It may take a long time before you can see the results (usually 2-3 months).

Bookmark and Share
Digg this