0

I'm looking to display Json from a url (http://pastebin.com/raw.php?i=MSyA8CBZ). Then, I want the "steamid" to be displayed. Currently, I am using this code, but it's not working:

<?php 
$json = file_get_contents('http://pastebin.com/raw.php?i=MSyA8CBZ');
$obj = json_decode($json);
echo $obj->response->players->steamid; 
?>

I get the error: Fatal error: Cannot use object of type stdClass as array in /home/u434538987/public_html/testjson.php on line 4 What is wrong with my code?

3
  • 1
    Doesn't the error tell you what is wrong? Commented Aug 16, 2014 at 20:48
  • 1
    players is an array, so it should be $obj->response->players[0]->steamid; Commented Aug 16, 2014 at 20:50
  • @putvande Yup! That fixed it. Thanks a million! Commented Aug 16, 2014 at 20:51

1 Answer 1

2

As mentioned in the comment, players is an array, is you could have seen if you had done print_r($obj) or even saw what the URL you load produced. So you should do :

$obj->response->players[0]->steamid;
Sign up to request clarification or add additional context in comments.

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.