0

Here's what I have at the moment:

<?php
    $string = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=##################################&steamids=76561198040884950");
    $json=json_decode($string);
?>

<script type="text/javascript">
        alert("<?php echo $json; ?>");
</script>

All I'm trying to do at this stage is receive the JSON information. I'm quite new to jQuery and PHP so not sure where I'm going wrong.

2
  • Yes, you are doing correct json_decode. but what you want exactly ? Commented Nov 12, 2012 at 5:47
  • Publicly posting your Steam API key is a violation of the Steam API terms of use. You should edit your post to remove it ASAP. Commented Nov 12, 2012 at 5:51

5 Answers 5

1

By using json_decode you're converting the JSON from a string (which can be universally understood and parsed) to a PHP object or array, which will not print out the way you wish. You should avoid converting it and simply do something like this:

<?php
    $string = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=...&steamids=76561198040884950");
?>

<script type="text/javascript">
        var json = <?php echo $string; ?>;
        // do things with the JSON; parse it into an object, etc
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

check this out http://api.jquery.com/jQuery.parseJSON/ ,something like this

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

Comments

1

When using json_decode through PHP, your output will be returned in an array;

When returning json information from external files to jquery scripts you must ensure to use the proper syntax. Codex

Comments

1

Secondly I'm not sure if you know, but in using file_get_contents, you need your php.ini file to have the following flag:

allow_url_fopen = 1

If you don't want to, there are alternatives: Check it

Comments

1

Try using <?php print_r($json); ?> instead of echo. It's doing what you want it to. echo just isn't any good at handling arrays.

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.