Saturday, February 28, 2009

Display Live Cricket Score on your site

I wanted to display live cricket score on my site, so I pushed the tea cup aside and got down in front of my PC googling for something significant, to find an hour later that the previous hour I spent went in no man's land!!! I got an inspiration here too from the blog comments BBC Backstage. Ok, so I went ahead and decided to pull out the live score data from wap.cricinfo.com. I first tried to pull out their page source into a string using PHP built in function file_get_contents() but apparently it didn't work out because cricinfo server is too nasty to respond to your http GET request if you don't provide the user-agent string. So, I formulated my own http request and then it worked out effectively. Below is the PHP code fully commented for clarity. The code is in aqua and comments in yellow.

<?php
$matchid=18307; //enter the matchid here
/*How to get match id? Open wap.cricnfo.com through Opera or FF with XHTML-MP extension. Then click the match that you're interested in. From the address bar, read the matchid.
Ok, so now we've got the match ID lets proceed
*/

$fp = fsockopen("wap.cricinfo.com", 80, $errno, $errstr, 30);
/*the above line is to open a socket connection to their server and bind it to a file stream
*/

if (!$fp) {
echo "$errstr ($errno)
\n";
echo "Data not available!";
} else {

/* and these four lines below are our http request */
$out = "GET /liveScore.do?matchId=$matchid HTTP/1.1\r\n";
$out .= "Host: wap.cricinfo.com\r\n";
$out .= "User-Agent: SonyEricssonK800i/R1AA Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1\r\n";
$out .= "\r\n";
fwrite($fp, $out);

/* the below while loop reads the response from their server and stores it into a string i.e. $page
*/

while (!feof($fp)) {
$page.=fread($fp, 512 );
}
fclose($fp);

/* now we've got the page source, just use explode to pull out only the live score from it
*/

$blah=explode("<tr align=\"center\" bgcolor=\"#85ADE4\">", $page);
$blah=explode("</tr>", $blah[1]);
echo $blah[0];
?>


Now you've got the live score in the variable $blah[0], you can do anything with it. Format it according to your site theme and add wherever you want :) You may also use the PHP GD library to create an image from it. Possibilities are endless...

8 comments:

Unknown said...

t showing scote.....scowing code only on webpage..tell me how to get score..mail me kartheek.bollu@gmail.com

Anonymous said...

Thanks for good info update.

best regards.
http://www.livetv.pk

de martin said...

Hi I get the following error while running the script:

Parse error: syntax error, unexpected $end in /home/wwwwanee/public_html/index.php on line 31

How can we fix it?

Unknown said...

hey

There is an error in the script. The last line i guess. Please can you check it and fix that.

prab97 said...

Hi Sukhpal,
This blog post is 3 years old. You can't expect this script to work directly without any modifications because it depends on the exact format wap.cricinfo.com uses. The web has changed a lot in 3 years, so did cricinfo. And the script broke.

Sunrise said...
This comment has been removed by a blog administrator.
Gilbert Josh said...


Thank you so much for sharing all this wonderful info with the how-to's!!!! It is so appreciated!!! You always have good humor in your posts/blogs. So much fun and easy to read!

Auslogics BoostSpeed Crack

Harry Oscar said...

The Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.
Amazing content you published on your blog post. Thanks for sharing this!

Thank you for every other informative site.
Where else may just I get that type of info written in such an ideal way?

You definitely put a brand new spin on a topic that’s been discussed for many years.
Wonderful stuff, just wonderful!
Sidify Music Converter Crack
Photo Mechanic Crack
Adobe Photoshop CC Crack
ActivePresenter Crack
FL Studio Crack