3

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.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
pixelblock
  • 31
  • 1
  • 4

2 Answers2

2

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);

See: http://3v4l.org/jkInl

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

Community
  • 1
  • 1
jszobody
  • 28,495
  • 6
  • 61
  • 72
  • `utf8_decode` doesn't change `€` into `\u20AC`, but into an `?`; this is probably not what the OP wants. – Marcel Korpel Apr 04 '13 at 12:48
  • @MarcelKorpel updated answer, now preserves the € (assuming php 5.4) – jszobody Apr 04 '13 at 13:02
  • I'm still working on it, so the OP doesn't need al least PHP 5.4. BTW, for me, running PHP 5.4.13, I still get a `JSON_ERROR_UTF8`. – Marcel Korpel Apr 04 '13 at 13:11
  • Did you look at http://3v4l.org/jkInl? 5.4+ is looking beautiful. No JSON errors. – jszobody Apr 04 '13 at 13:18
  • 1
    I've seen it. Where did you find about `JSON_UNESCAPED_UNICODE`? As far as I can see, it is only an option for `json_encode`. – Marcel Korpel Apr 04 '13 at 13:22
  • It was another SO thread that mentioned JSON_UNESCAPED_UNICODE in a json_decode, I'll try to find it again. – jszobody Apr 04 '13 at 13:31
  • thanx jszobody major problem is solved, of course it leads to the other problem which is already discussed. I will try find proper soliution for currency charachters, it would be a big improvement on my site to display stocks denominated in it's market currency – pixelblock Apr 04 '13 at 13:54
  • @pixelblock excellent, glad that helped. I just added a link to another SO discussion that provides further information about json encoding and unicode characters. – jszobody Apr 04 '13 at 14:31
0

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);
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • That only fixes a symptom, not the problem. – 000 Apr 04 '13 at 13:33
  • @JoeFrambach: The problem *is* the JSON containing the Euro sign. `utf8_decode` converts the Euro sign into a `?`, so that's also not a solution. – Marcel Korpel Apr 04 '13 at 13:38
  • Are there any other characters that may appear, with the same problems? – 000 Apr 04 '13 at 13:55
  • thanks Marcel Korpel , good approach, but it creates array within array ,that unecessery beast multidimensional array ;) http://3v4l.org/I8XBp – pixelblock Apr 04 '13 at 14:49
  • @pixelblock: That's just how Google sends its string, but you can of course remove the `// [ … ]` using `substr`. See my updated answer. – Marcel Korpel Apr 04 '13 at 17:27
  • @JoeFrambach: I agree that there can be other characters (e.g. other currency signs) that can be sent, but we can only replace them this way with unicode character points, there is no function that does this for you. The actual problem lies at Google, that sends these malformed JSON-formatted strings. – Marcel Korpel Apr 04 '13 at 19:07