Hey, guys.
I’m currently trying to pull data from the Playstation 3’s PSN API and I’m not entirely sure what I’m doing.
Here is my code, saved as trophycount.php.
<?php
$url = "http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID";
$username = "vlastanovak";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POSTFIELDS, "sPSNID=$username");
$data = curl_exec($ch);
curl_close ($ch);
echo $data;
?>
Here it is online. If you view the source you will see that the XML is being returned.
http://thelumberjack.com.au/trophycount.php
How can I display specific data from the XML feed?
Also, when I change the echo $data line to return $data, the source becomes blank?
Thanks in advance!
Take a look at the [FPHP]SimpleXML[/FPHP] class.
Cheers.
I’ve got it working to some degree:
<?php
$url = "http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID";
$username = "vlastanovak";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POSTFIELDS, "sPSNID=$username");
$data = curl_exec($ch);
curl_close ($ch);
echo $data;
$psninfo = new SimpleXMLElement($data);
echo $psninfo->ID;
?>
<ul>
<li>PSN: <?php echo $psninfo->ID; ?></li>
<li>Total Trophies: <?php echo $psninfo->Trophies->Total; ?></li>
<li>Platinum: <?php echo $psninfo->Trophies->Platinum; ?></li>
<li>Gold: <?php echo $psninfo->Trophies->Gold; ?></li>
<li>Silver: <?php echo $psninfo->Trophies->Silver; ?></li>
<li>Bronze: <?php echo $psninfo->Trophies->Bronze; ?></li>
</ul>
However, it won’t work unless I echo $data first (line 17). When I change it to return $data it fails to echo the contents of the unordered list. What am I doing wrong here?