3

After googling, implementing code upon code, I still cannot get ANYTHING to display. Nothing. Not one thing.

I have a URL spitting out JSON:

 {"videos":[{"video":{"name":"Sanyo Zio","youtube":"FxxLDr--R5A","post_date":"2010-10-08 01:00:00",...

Here's the code I'm using to access the page:

 $url = file_get_contents("http://[website]/json/test.json");

 $arr = json_decode($url,true);

Now here's a short list of what I've tried to access ANY data from the page:

1:

 print_r($arr);

2:

 foreach($arr['videos']['video'] as $item) {
   echo "Name: ". $item[0] ."<br>"; 
 }

3:

 $obj = $arr[0];
 echo $obj;

4:

 foreach($arr as $a){
   echo "Name: ".$a['videos']['video']['name']."<br />";
 }

Clearly I'm missing something, but I just haven't been able to figure out what I'm doing wrong! Is my encoding not correct? Here's how I'm encoding the JSON to begin with:

 $arr = array('videos' => array());
 foreach($vid as $items){
  $arr['videos'][] = array('video' => array(
    'name' => $items['videoName'], 'youtube' => $items['youtubeID'], 'post_date' => $items['productionTimestamp'], 'description' => $items['videoDesc'], 'link' => $single_linker_values['deeplink'], 'image' => $image));
 }
 echo json_encode($arr);

Any ideas/suggestions?


Update - Apparently the server is locked down, but being inhouse I don't notice :) Obviously the webpage does! Thanks for the help!

8
  • The URL? Have you tried var_dump()? Commented Jan 7, 2013 at 15:53
  • So what does print_r($arr); show? What's the problem here? Commented Jan 7, 2013 at 15:55
  • 2
    var_dump($url); before you try to parse anything, to see if you actually have a response at all. Commented Jan 7, 2013 at 15:57
  • print_r($arr); doesn't show anything, which leads me to believe the issue is either with the retrieval or the encoding. Commented Jan 7, 2013 at 16:04
  • Did you check your PHP file encoding? Commented Jan 7, 2013 at 16:09

1 Answer 1

1

From PHP Manual

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit

Since you are not specifying a recursion limit, chances are that either your JSON is invalid or nothing is being retrieved from your URL.

Three things to try:

  • determine if there was a json_decode error

    print_r(json_last_error()); // call after json_decode

  • check that data is being returned

    print_r($url);

  • see if data will decode as an object

    $obj = json_decode($url);

    print_r($obj);

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

5 Comments

print_r(json_last_error()); spits out a 0. print_r($url); spits out nothing.
So there is the problem, $url is empty meaning file_get_contents("http://[website]/json/test.json"); is not retrieving anything. Can you provide the full URL and we can check?
I've added the URL to my original question.
Your URL is returning a 204 No-Content response
That would explain it. Server locked down. Guess I'll find another way. Thanks!

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.