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...
<?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...
No comments :
Post a Comment