SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member thelumberjack's Avatar
    Join Date
    Sep 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying XML Data Stored in a PHP Variable

    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 Code:
    <?php 

    $url 
    "http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID";
    $username "vlastanovak"
            
    $ch curl_init();     
        
    curl_setopt($chCURLOPT_URL$url); 
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue); 
    curl_setopt($chCURLOPT_POST1); 
    curl_setopt($chCURLOPT_TIMEOUT15); 
    curl_setopt($chCURLOPT_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!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Take a look at the SimpleXML class.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member thelumberjack's Avatar
    Join Date
    Sep 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers.

    I've got it working to some degree:

    PHP Code:
    <?php

    $url 
    "http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID";
    $username "vlastanovak";

    $ch curl_init();

    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_TIMEOUT15);
    curl_setopt($chCURLOPT_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?


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •