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.

rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • Yes, you are doing correct `json_decode`. but what you want exactly ? – GBD Nov 12 '12 at 05: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. – Chris Hayes Nov 12 '12 at 05:51

5 Answers5

1

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

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
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>
Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
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

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
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

Community
  • 1
  • 1
Dane Balia
  • 5,271
  • 5
  • 32
  • 57
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.

Joshua Kaiser
  • 1,461
  • 9
  • 17