Steven M
Steven M

Reputation: 103

PHP json_decode not working properly

I am trying to use PHP's json_decode function to get a particular value from a json object. Sample code below:

foreach ($streams as &$i) {
        $chan = "http://api.justin.tv/api/stream/list.json?channel=" . $i;
        $json = file_get_contents($chan);   //Turns the gathered file information into a string for searching purposes.
        echo $json . " End of json variable.<br>";
        $exist = strpos($json, 'name');     // Search the file/json object for the name attribute
        if($exist) {                        //  Check to see if a name existed and if so add it to the live streams and get the image.
            echo " <a href=\"http://justin.tv/" . $i . "\">" . $i . "</a> <br>";
            $liveStreams[$count] = $i;
            $json_information = json_decode($json,true);
            $image[$count] = $json_information[0]['channel']['image_url_large'];
            echo "Image link should appear: " . $image[count];
            $count++;
        }
    }   

So What I am trying to do with this is first and foremost gather which streams are active from a list provided earlier in the code. Second, if the stream is live, display a link to a page for it to be viewed(currently the justin.tv stream itself). What currently works is only the live streams will appear with links to them. What I need is to figure out why after decoding I cannot access the image_url_large variable. This is going to be a thumbnail view of the stream eventually.

I have looked at various places for what should have worked and even on stackoverflow I saw the following thread:

json decode in php

I tried doing it like nickf's answer and it still isn't working. Any help would be greatly appreciated as well as staying within the array style instead of going into objects.

Upvotes: 4

Views: 18782

Answers (2)

timdev
timdev

Reputation: 62874

Aside from the silly use of strpos(), which you seem to claim was someone else's idea, it seems like you just need to carefully debug.

Do something like this:

$data = json_decode($json,true);
echo "<PRE>";
var_dump($data); die();

Now you can see the data structure the API is giving you.

Look at the structure of the array. Notice, for instance, that $data['image_url_large'] does not exist. There is, however, $data[0]['channel']['image_url_large']!

Notice also that instead of the silly strpos() call, which will give false positives if the string "name" exists anywhere in the json-string, you could do something like:

$exists = ! empty($data[0]['name']);

EDIT Here's some code that will hopefully help you on your way:

    <?php 
//if you don't do this, you're flying blind.
ini_set('display_errors',1);
error_reporting(E_ALL);

//list.json is a copy of the data from the URL you posted.
$json = file_get_contents('./list.json');   

//decode the data
$data = json_decode($json,true);

//uncomment this if you're not sure of what the json's structure is.
#echo "<PRE>";var_dump($data);die();

//check for the existence of a "name" key in the first item.
$exist = ! empty($data[0]['name']);

echo "Exist?:";

if ($exist) { 
    echo " yes\n";
}else{
    echo " no\n";
}

//output the image url:
echo $data[0]['channel']['image_url_large'];

//say goodbye
die("\n\nAll done.\n");

OUTPUT:

$ php test.php 
Exist?: yes
http://static-cdn.jtvnw.net/jtv_user_pictures/beastyqt-profile_image-c5b72ccf47b74ed2-300x300.jpeg

All done.

Upvotes: 5

sagi
sagi

Reputation: 5737

Use var_dump() to examine the returned json object. From your example, it looks like you need something like:

$json_information[0]['channel']['image_url_large']

Upvotes: 2

Related Questions