Jump to content


Photo

How to get XML with PHP from armory


  • Please log in to reply
122 replies to this topic

#121 Furizaa

Furizaa

    Somewhat usefull

  • Members
  • 44 posts

Posted 15 April 2011 - 06:06 PM

Seems like they have some sort of Cross Site Policy set. Surprisingly this works:

Simple Realm Status with JQuery & JSONP - Forums - World of Warcraft

Can someone with access to the US forums ask what we can expect for the future? If they plan to allow X-Site-Ajax requests to fetch data from the API, i ditch my current PHP scripts and write this stuff from scratch in JavaScript.

#122 BinaryMuse

BinaryMuse

    Glass Joe

  • Members
  • 1 posts

Posted 15 April 2011 - 11:05 PM

Seems like they have some sort of Cross Site Policy set. Surprisingly this works:

Simple Realm Status with JQuery & JSONP - Forums - World of Warcraft

Can someone with access to the US forums ask what we can expect for the future? If they plan to allow X-Site-Ajax requests to fetch data from the API, i ditch my current PHP scripts and write this stuff from scratch in JavaScript.


This is specifically what JSONP was designed to do: allow loading JSON data from other domains. See JSONP - Wikipedia, the free encyclopedia.

#123 zork

zork

    Don Flamenco

  • Members
  • 477 posts

Posted 29 April 2011 - 01:48 PM

I set up a small testfile in PHP that gets data from the new WoW Armory API via CURL.

The result is delivered in JSON and then converted into a PHP array.

<?php
[COLOR="YellowGreen"]
  //function and variables[/COLOR]

  function json_code($json) {
    return json_decode($json, true);
  }
[COLOR="YellowGreen"]
  //html output[/COLOR]
  function p($output) {
    $eol = "\n";
    echo $output.$eol;
  }

[COLOR="YellowGreen"]  //html header stuff[/COLOR]
  header('Content-Type: text/html; charset=utf-8');
[COLOR="YellowGreen"]
  //html output[/COLOR]
  p('<html>');
  p('<head>');
  p('  <title>WoW Armory API: Realm Status Test</title>');
  p('</head>');
  p('<body>');
  p('  <h1><a href="test1.php">WoW Armory API: Realm Status Test</a></h1>');

[COLOR="YellowGreen"]  //url to ask for[/COLOR]
  $url = 'http://us.battle.net/api/wow/realm/status?realm=Medivh&realm=Blackrock';

[COLOR="YellowGreen"]  //curl init[/COLOR]
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_POST, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  $json = curl_exec($ch);
  curl_close ($ch);


  $json_orig = $json; [COLOR="YellowGreen"]//temp[/COLOR]
[COLOR="YellowGreen"]  //$json = utf8_encode($json); //in case utf8 encoding is needed[/COLOR]
  $arrayed_json = json_code($json);[COLOR="YellowGreen"] //generate php array from json response[/COLOR]

[COLOR="YellowGreen"]  //Output[/COLOR]
  p('  <h3>Output</h3>');
[COLOR="YellowGreen"]  //print the json code[/COLOR]
  print_r($json_orig);

  p('');

  p('  <hr>');

[COLOR="YellowGreen"]  //print the php array[/COLOR]
  print_r($arrayed_json);

  p('');
  p('  <hr>');

[COLOR="YellowGreen"]  //print result in loop[/COLOR]
  $realms = $arrayed_json['realms'];

  for ($i=0;$i<count($realms);$i++) {
    p('  <h3>Realm: '.$realms[$i]['name'].'</h3>');
    p('  <p>Type: '.$realms[$i]['type'].'</p>');
    p('  <p>Status: '.$realms[$i]['status'].'</p>');
    p('  <hr>');
  }

  p('</body>');
  p('</html>');

?>

Once the data is retrieved and converted into an PHP array it can be extracted easily.

Result of the code above:
<html>
<head>
  <title>WoW Armory API: Realm Status Test</title>
</head>
<body>
  <h1><a href="test1.php">WoW Armory API: Realm Status Test</a></h1>
  <h3>Output</h3>
{
  "realms":[
    {
      "type":"pvp",
      "population":"high",
      "queue":false,
      "status":true,
      "name":"Blackrock",
      "slug":"blackrock"
    },
    {
      "type":"pve",
      "population":"medium",
      "queue":false,
      "status":true,
      "name":"Medivh",
      "slug":"medivh"
    }
  ]
}
  <hr>
Array
(
    [realms] => Array
        (
            [0] => Array
                (
                    [type] => pvp
                    [population] => high
                    [queue] => 
                    [status] => 1
                    [name] => Blackrock
                    [slug] => blackrock
                )

            [1] => Array
                (
                    [type] => pve
                    [population] => medium
                    [queue] => 
                    [status] => 1
                    [name] => Medivh
                    [slug] => medivh
                )

        )

)

  <hr>

  <h3>Realm: Blackrock</h3>
  <p>Type: pvp</p>
  <p>Status: 1</p>
  <hr>
  <h3>Realm: Medivh</h3>
  <p>Type: pve</p>

  <p>Status: 1</p>
  <hr>
</body>
</html>

| Simple is beautiful.
| Blog | Roth UI | Roth UI FAQ | GoogleCode | Zork | Guild | zorker.de

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users