Thursday, August 25, 2011

Google Finance API Sample PHP Code

/* Turn off notices for empty string values */
error_reporting(0);

/* Get data from google */
$data = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NSE:BIRLAPOWER');

/* Strip first and last characters */
$data = substr($data,7,strlen($data)-7-4);

/* Split data into name:value records */
$records = explode(',', $data);

for($i = 0; $i < count($records); $i++) {
    /* split record into record name ($pair[0]) and record value ($pair[1]) */
    $pair = explode(": ", $records[$i]);
    /* do the output to be sure you got it right */
    echo 'Record name: ' . $pair[0] . ', Record value: ' . $pair[1] . '
';
}
?>

No comments: