1

ARRAY: https://api.twitch.tv/kraken/search/games?q=League%20of%20Legends&type=suggest

Greetings! I am trying to get a value within an array. What i'm trying to go for is the large box art of a certain game. So how do I do this? Here is my attempt, but I am getting these errors.

Notice: Trying to get property of non-object in /???/test.php on line 4

Notice: Trying to get property of non-object in /???/test.php on line 4

Notice: Trying to get property of non-object in /???/test.php on line 4

<?php
$game = urlencode($_GET['game']); // This is "League of Legends" in the URL
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/search/games?q={$game}&type=suggest'), true);
echo "IMG: ". $json_array->games[0]->box->large; // line 4
12
  • 2
    what is the code in your line 4 ? Commented Jun 17, 2015 at 0:57
  • yep since PHP is stating the issue is on line 4 and you've only shown 3 lines we have to ask if you can show line 4 in your file :) I also suggest that you sanitize and ensure that the value of $_GET['game'] is proper. You can do that in many different ways depending on the content that's required for that variable... For instance it it's a string validate that it is a string, etc. Commented Jun 17, 2015 at 1:02
  • 4
    You've passed true as the 2nd arg to json_decode which forces it to create an associative array instead of an object. You want something like $json_array['games'][0]['box']['large']. Or simply don't pass a 2nd arg Commented Jun 17, 2015 at 1:03
  • @JosephCrawford $_GET values are always strings. I'd also say passing it through urlencode() is safe enough Commented Jun 17, 2015 at 1:07
  • 3
    You should use double quotes in the file_get_contents function, otherwise the $game variable is not going to be replaced. Commented Jun 17, 2015 at 1:27

2 Answers 2

1

You should first get your content. You wasn't get content before. Below line returns $json_array correctly.

$json_array=json_decode(file_get_contents('https://api.twitch.tv/kraken/search/games?q='.$game.'&type=suggest'), true);

Then edit your second line like below

echo "IMG: ". $json_array['games'][0]['box']['large'];

So your final and working code like this:

$game = urlencode($_GET['game']); // This is "League of Legends" in the URL
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/search/games?q='.$game.'&type=suggest'), true);

echo "IMG: ". $json_array['games'][0]['box']['large'];

It absolutely works, I tried and get

IMG: http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg

as output

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

Comments

0

This error simply says that $json_array is an array not an object. You have to access object element with -> and array elements with []. If $json_array is array then you have to access it's elements with

$json_array['games']

And if $json_array is object then you have to access it's elements with

  $json_array->games

Comments

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.