1

Here is my PHP/JSON code:

$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$json=str_replace('},
]',"}
]",$json);
$decoded= json_decode($json);
$data=$decoded->matches[0];
foreach ($data as $value) {
print_r($value->team1->logo_url);
}

Now I have the following problem

Notice: Trying to get property of non-object

and

Notice: Undefined property: stdClass::$team1

I just want use foreach loop and then show my results in HTML.

Why I am getting the 2 mentioned problems and how can I show the correct results?

4
  • Why are you doing str_replace on the result? Commented Jul 29, 2015 at 23:34
  • Have you tried var_dump($decoded) ? Commented Jul 29, 2015 at 23:36
  • actually json works fine just foreach dont work i think i made mistake in foreach loop matches[0]gone be 1,2,3,4,5,6,7,8 Commented Jul 29, 2015 at 23:43
  • If you have narrowed down the problem to a certain part of your code, please edit the question to make clear what the problem is. Commented Jul 30, 2015 at 0:37

2 Answers 2

1
  1. Ok so the url your are using return VALID JSON, no need to change any of it!
  2. I suggest using arrays, it has always appeared simpler to me
  3. Do you want the logo_url from team or image_url from league? I will show both in my implementation.

So here is some corrected code

$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded= json_decode($json,true); // True turns it into an array
$data = $decoded['matches'];
foreach ($data as $value) {
    //I am not sure which one you want!!!
    echo $value['league']['image_url'] . "<br>";
    echo $value['team1']['logo_url'] . "<br>";
    echo $value['team2']['logo_url'] . "<br>";
}

*EDIT To show wanted implementation by questions author...

$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded= json_decode($json,true); // True turns it into an array
$data = $decoded['matches'];
foreach ($data as $value) {
    echo "
        <img src=\"http://dailydota2.com/{$value['team1']['logo_url']}\">
        <img src=\"http://dailydota2.com/{$value['team2']['logo_url']}\">
    ";
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks...works fine now can i show my results in html? when i trying to do this <img src="http://dailydota2.com/<?php echo $value['team1']['logo_url'] ?>"> <img src="http://dailydota2.com/<?php echo $value['team2']['logo_url'] ?>">its show me just last result how can i show all results in html?
@jackson I added the implementation that you need!
1

I have checked your code and have some notes and hopefully a solution:

1- You are trying to get non existing key from JSON data, that is the message telling you.

2- I am still not sure what do you get from the JSON API. But regarding to dailydota2 documentation there is nothing called image_url under team1. I guess you are looking for logo_url or something like that.

3- Do not change the format of JSON as you do in your code, therefore delete following line:

$json=str_replace('}, ]',"} ]",$json);

Just leave the main JSON output from API as default.

4- When you try to get specific key from the decoded JSON/Array just use following way:

$data = $decoded->{'matches'};

in stead of

$data=$decoded->matches[0];

Reference: http://php.net/manual/en/function.json-decode.php

5- And finally your foreach loop is working but needs the correct key:

foreach ($data as $value) {
    print_r($value->team1->logo_url);
}

When all these step is done, it should works.

Here is your final corrected code:

$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded = json_decode($json);
$data = $decoded->{'matches'};
foreach ($data as $value) {
    print_r($value->team1->logo_url);
    echo '<img src="http://dailydota2.com/' . $value->team1->logo_url . '">';
}

It returns following output, and I do not get any errors.

/images/logos/teams/cdecgaming.png/images/logos/teams/teamempire.png
/images/logos/teams/ehome.png/images/logos/teams/ehome.png
/images/logos/teams/fnatic.png/images/logos/teams/cloud9.png
/images/logos/teams/teamissecret.png/images/logos/teams/teamissecret.png
/images/logos/teams/natusvincere.png/images/logos/teams/fnatic.png

Again I really do not know which information you want to get from the API but here you have a base of working code that you can work further with to get the required data from the right KEYs.

5 Comments

thanks yeah i mean logo_url sorry now can i show my results in html? when i trying to do this <img src="http://dailydota2.com/<?php echo $value->team1->logo_url ?>"> <img src="http://dailydota2.com/<?php echo $value->team1->logo_url ?>"> its just show me last result how i can show all results?
inside the foreach put this line: { echo '<img src="http://dailydota2.com/' . $value->team1->logo_url . '">'; } it will show all images and you can mark //print_r etc...
thank you so much for help :) have good day sir and sorry i can't plus your rep because i need 15 rep in my self or somthing like that
You are welcome if you have been happy for my answer please check it as answer.
@jackson please check this as answer

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.