Reputation: 31
I'm trying to access stock Quotes through Google Finance
by doing so:
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=VSE:APG1L');
$json = str_replace("\n", "", $quote);
$data = substr($json, 4, strlen($json) -5);
print_r($data);
$json_output = json_decode($data, true);
print_r($json_output);
echo "\n".$json_output['l'];
json_decode
suppose to give me an normal array with keys and values, but it doesn't.
Upvotes: 3
Views: 6900
Reputation: 21763
Another solution is to "manually" replace all instances of €
by \u20AC
(which is the unicode character point for the Euro sign).
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=VSE:APG1L');
$quote = str_replace(chr(128), '\u20AC', $quote);
$json = substr($quote, 6, -3); // remove unnecessary '// [ … ]'
$json_output = json_decode($json, true);
print_r($json_output);
Upvotes: 0
Reputation: 28911
If you look at json_last_error()
after attempting json_decode()
you'll see that you are getting:
JSON_ERROR_UTF8 Malformed UTF-8 characters, possibly incorrectly encoded
Try this:
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=VSE:APG1L');
$json = substr($quote, 4, -5);
$json_output = json_decode($json, true, JSON_UNESCAPED_UNICODE);
print_r($json_output);
Note that JSON_UNESCAPED_UNICODE
is only available as of php 5.4
Another option would be to do...
$quote = utf8_decode($quote);
...after you fetch it. This will convert the euro symbol into a ?
character. Might not be what you want, but at least you get json_decode to return an array to you.
Update: See here for more information:
PHP decoding and encoding json with unicode characters
Upvotes: 2