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 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.

$matchid=18307; //enter the matchid here
/*How to get match id? Open 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("", 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)
echo "Data not available!";
} else {

/* and these four lines below are our http request */
$out = "GET /$matchid HTTP/1.1\r\n";
$out .= "Host:\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 );

/* 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...


Unknown said...

t showing scote.....scowing code only on webpage..tell me how to get score..mail me

Anonymous said...

Thanks for good info update.

best regards.

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...


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 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.