-1

I'm trying to encode a json string but it keeps returning null. I've tried a few suggestion here on st

$url = "https://www.google.com/finance?output=json&start=0&num=200&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28change_today_percent%20%3E%3D%20-101%29%20%26%20%28change_today_percent%20%3C%3D%20-3%29%20%26%20%28volume%20%3E%3D%20150001%29%20%26%20%28volume%20%3C%3D%20313940000%29%20%26%20%28last_price%20%3E%3D%2010%29%20%26%20%28last_price%20%3C%3D%20229301%29]&restype=company&ei=T5mTVKG5IYT1igKEoYHQCQ";

$obj = file_get_contents($url);
$obj = json_decode($obj);
var_dump($obj);
9
  • 1
    What does $obj contain before you run json_decode on it? Commented Dec 19, 2014 at 18:14
  • 2
    It looks like if you want to encode a string you have to use json_encode not json_decode. Commented Dec 19, 2014 at 18:15
  • 2
    @Verhaeren I'd assume he wants to take a string (that he gets form that URL) and decode it into an object / array. Commented Dec 19, 2014 at 18:16
  • @AlpineCoder I'm pointing the lack of consistency in the title of the question. Commented Dec 19, 2014 at 18:17
  • 2
    This might be helpful: PHP reading invalid json with json_decode(). It specifically references google/finance. "The JSON feeds from Google always seem to be plagued with problems...." Commented Dec 19, 2014 at 20:22

3 Answers 3

2
+50

JSON doesn't support \x escapes, so it's invalid JSON by definition, hence why json_decode() returns null. Decoding the valid JSON \u0022 works fine.

$url = "https://www.google.com/finance?output=json&start=0&num=200&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28change_today_percent%20%3E%3D%20-101%29%20%26%20%28change_today_percent%20%3C%3D%20-3%29%20%26%20%28volume%20%3E%3D%20150001%29%20%26%20%28volume%20%3C%3D%20313940000%29%20%26%20%28last_price%20%3E%3D%2010%29%20%26%20%28last_price%20%3C%3D%20229301%29]&restype=company&ei=T5mTVKG5IYT1igKEoYHQCQ";

$obj = file_get_contents($url);
$obj = trim($obj);
$obj = str_replace('\x', '\u00', $obj);
$obj = json_decode($obj,true);
var_dump($obj);
Sign up to request clarification or add additional context in comments.

2 Comments

The solution is indeed to convert the \x notation to \u00 notation. trim() isn't necessary. Works like a charm!
@ Jasper N. Brouwer Yes its not needed. I have added it because the json data have whitespaces.
0

Try this code

<?php
function convert($string) {
    return preg_replace_callback('#\\\\x([[:xdigit:]]{2})#ism', function($matches) {
        return htmlentities(chr(hexdec($matches[1])));
    }, $string);
}
$url = "https://www.google.com/finance?output=json&start=0&num=200&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28change_today_percent%20%3E%3D%20-101%29%20%26%20%28change_today_percent%20%3C%3D%20-3%29%20%26%20%28volume%20%3E%3D%20150001%29%20%26%20%28volume%20%3C%3D%20313940000%29%20%26%20%28last_price%20%3E%3D%2010%29%20%26%20%28last_price%20%3C%3D%20229301%29]&restype=company&ei=T5mTVKG5IYT1igKEoYHQCQ";

$obj = file_get_contents($url);
$obj = convert($obj);
$obj = json_decode($obj);
echo '<pre>'; print_r($obj); echo '</pre>';
?>

Comments

0

As recommended here (and pointed out by @showdev already, thanks!) you should use their RSS API and parse that as XML instead, because Google does not deem it necessary to honour web standards (here, they fail at JSON; elsewhere, they fail at simple things like SMTP and especially IMAP).

JSON has a very detailed specification, and it is very easy to adhere to in generators. Making JSON parsing strict is recommended for security reasons – especially with external input. So please use Google’s RSS variant.

1 Comment

Did you even try using the rss option argument? I would suggest you do before just adding an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.