4

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.

Community
  • 1
  • 1
Steven M
  • 103
  • 1
  • 2
  • 7
  • var_dump($json_information) - what do you get? – sagi Jul 15 '11 at 15:27
  • it should be `if ($exist !== FALSE)`, to be 'correct'. strpos can return 0 if what you're looking for is right at the start of the string, which would otherwise be interpreted as false. – Marc B Jul 15 '11 at 15:40
  • I am working inconjunction with another person. He is the one that gave me the code for the exists and this sit eh way that he wanted to do it. If you want a sample of the code returned, enter: http://api.justin.tv/api/stream/list.json?channel=beastyqt The name is never first according to the API so I doubt that will be an issue. – Steven M Jul 15 '11 at 15:44
  • @Sagi: Here is a pastie to the information that is returned. http://pastie.org/2218457 The only thing that I printed out myself was the live link to a stream. – Steven M Jul 15 '11 at 15:58

2 Answers2

5

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.
timdev
  • 61,857
  • 6
  • 82
  • 92
2

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']
sagi
  • 5,619
  • 1
  • 30
  • 31
  • I have made the change as you suggested, but it did not work. I have updated the code in the original to reflect this. – Steven M Jul 15 '11 at 16:25
  • What about `$image[count]` in your code. See the typo there? – timdev Jul 15 '11 at 16:29
  • Seriously, you need to look at my answer and start debugging for yourself. Look at my answer for a suggestion about how to get started. This isn't rocket surgery, but it seems like you're scared to think it through and debug. – timdev Jul 15 '11 at 16:32
  • timdev. Have you considered I only have the code base of this particular class in front of me? Quite literally I am looking at just one particular file. Also, In my actual editor which you cannot see, that typo did not exist. Please be a lot more polite since you do not have access to the actual screen in front of me and cannot see that. Besides that, I also was not given access to any of the other resources so I cannot insert the code myself to test it. I have to rely on someone else getting the code from me and while this is not the most productive way, it is how I have to do it anyhow. – Steven M Jul 15 '11 at 16:49
  • @Steven M - I did consider you have only this code in front of you. There's plenty you can do to debug it. I gave you hints in my answer. Sagi said (correctly) that you weren't looking in the right place for the image url. You just said "but it did not work", which isnt helpful. As you pointed out, we can't see your screen: **in what way** did it not work? Are you getting an error message? Is json_decode returning something that looks like an array? Have you tried turning error reporting to E_ALL? Didn't mean to be rude, but you're asking for help and giving us nothing to go on. – timdev Jul 15 '11 at 16:56
  • The way in which it did not work was that the url still did not display even though I am looking at the correct place for it now. So while that was fixed there may be something else behind the API's and that is what I am looking into now at their site. The hope was to get the url so that it can be listed as an image reference. So thank you for your help regardless, but I do tend to take others saying check for errors yourself as an attack. Also, on my current computer I don't have any PHP error checking as I am using notepad and as stated before don't have access to the code base. – Steven M Jul 15 '11 at 17:03
  • @StevenM let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1517/discussion-between-timdev-and-steven-m) – timdev Jul 15 '11 at 17:08