3

I have a very simple bit of code

$pc1 = $_POST['post_code1'];
$pc2 = $_POST['post_code2'];
$url = "http://maps.google.com/maps/nav?q=from:".$pc1."%20to:".$pc2;
$url_data = file_get_contents($url);
$json_data = json_decode($url_data);
var_dump($json_data);

$url_data is full of juicy json stuff but $json_data returns NULL. Does anyone have an idea why?

5
  • 4
    Are you sure it's full of purely valid JSON? Commented Apr 25, 2012 at 15:45
  • Does the URL pass validation? jsonlint.com Commented Apr 25, 2012 at 15:51
  • How deep is it? PHP doc: "NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit." The recursion limit is 512 by default. Commented Apr 25, 2012 at 15:52
  • var_dump($url_data)? Maybe fopen wrappers is disabled? Commented Apr 25, 2012 at 15:53
  • no I got my provider to enable fopen() in php.ini Commented Apr 25, 2012 at 15:58

1 Answer 1

3

I found the following worked after find a number of people with similar problems

$json_data = json_decode(utf8_encode($url_data),true);

source

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct, because file_get_contents does so byte-for-byte and not character-for-character.

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.