Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2007
    Posts
    72

    Want to pull data from a URL and display it using PHP/HTML

    Alright, so I want to build a stats page for Killing Floor. Basically I want to pull data from a URL then using PHP I want to display this stats on a page formatting with HTML.

    This is what I've tried so far, but it gives me errors.

    Code:
    <?php
    
    //Passing the XML
    $data = file_get_contents('steamcommunity.com/id/lblanchardiii/statsfeed/1250/?xml=1&schema=1');
    $stats = simplexml_load_file($data);
    //-----------------------
    //Passing a filename
    $stats = simplexml_load_file('steamcommunity.com/id/lblanchardiii/statsfeed/1250/?xml=1&schema=1', null, true);
    ?>
    This is the error I am getting

    Code:
    Warning: file_get_contents(steamcommunity.com/id/lblanchardiii/statsfeed/1250/?xml=1&schema=1) [function.file-get-contents]: failed to open stream: No such file or directory in /home2/tdnclan/public_html/kf-stats/index.php on line 4
    
    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "" in /home2/tdnclan/public_html/kf-stats/index.php on line 5
    
    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "steamcommunity.com/id/lblanchardiii/statsfeed/1250/?xml=1&schema=1" in /home2/tdnclan/public_html/kf-stats/index.php on line 8
    I was following the example outlined here; http://www.phpfreaks.com/tutorial/handling-xml-data

    However that doesn't seem to work. I then just saved the XML file to the web server, then used the direct file name in the code above instead of the URL.

    Code:
    <?php
    
    //Passing the XML
    $data = file_get_contents('1250.xml');
    $stats = simplexml_load_file($data);
    //-----------------------
    //Passing a filename
    $stats = simplexml_load_file('1250.xml', null, true);
    ?>
    Still getting errors:
    Code:
    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8" standalone="yes"?><statsfeed><steamID64>76561198035683889</steamID64><stats><ite in /home2/tdnclan/public_html/kf-stats/index.php on line 5

    Just realized I am using simplexml_load_file() in the code above, even though the tutorial said to use SimpleXMLElement(), but since that wasn't working I assumed I needed to use something to pull a file remotely since the file wasn't stored physically. Still not working as this is the error I am getting, with the file stored locally.

    Code:
    <?php
    
    //Passing the XML
    $data = file_get_contents('1250.xml');
    $stats = SimpleXMLElement($data);
    //-----------------------
    //Passing a filename
    $stats = SimpleXMLElement('1250.xml', null, true);
    ?>
    Code:
    Fatal error: Call to undefined function SimpleXMLElement() in /home2/tdnclan/public_html/kf-stats/index.php on line 5
    Not sure what I am doing wrong and Googling it doesn't seem to help me much; but I'm thinking it's because I don't really know what I need to search.

  2. #2
    Join Date
    Nov 2004
    Posts
    71
    for "file_get_contents" you need to supply the protocol like 'http://' so it knows where to get the data from.

    As for the other example you used, looks like you don't have the required XML functions installed on your server.

  3. #3
    Join Date
    Aug 2007
    Posts
    72
    Gonna test out that http:// thing, but as for the required XML functions... can you be a little more specific on that?

    I'm running CentOS 5.8 w/ WHM/cPanel...
    Apache version 2.2.23
    PHP version 5.3.18
    MySQL version 5.1.65-cll
    Architecture x86_64
    Perl version 5.8.8
    Kernel version 2.6.18-308.11.1.el5

    Going to do some research on it now.

  4. #4
    Join Date
    Aug 2007
    Posts
    72
    Thinking this is what I need to compile apache with; http://www.php.net/manual/en/ref.xmlrpc.php

    Doing that as I type this.

  5. #5
    Join Date
    Aug 2001
    Posts
    5,597
    Try
    PHP Code:
    $stats = new SimpleXMLElement('1250.xml'nulltrue); 

  6. #6
    Join Date
    Aug 2007
    Posts
    72
    Quote Originally Posted by zoid View Post
    Try
    PHP Code:
    $stats = new SimpleXMLElement('1250.xml'nulltrue); 
    Still get the same error:

    Code:
    Fatal error: Call to undefined function SimpleXMLElement() in /home2/tdnclan/public_html/kf-stats/index.php on line 5

  7. #7
    Join Date
    Aug 2001
    Posts
    5,597
    What PHP version do you use? It should be enabled by default http://www.php.net/manual/en/simplexml.installation.php unless you disabled it

  8. #8
    Join Date
    Aug 2007
    Posts
    72
    5.3.18 and as far as I know I haven't disabled it. I will check that real quick though.

  9. #9
    Join Date
    Aug 2007
    Posts
    72
    Apparently it should be enabled.

    Code:
    a:40:{i:0;s:4:"Core";i:1;s:4:"date";i:2;s:4:"ereg";i:3;s:6:"libxml";i:4;s:7 :"openssl";i:5;s:4:"pcre";i:6;s:7:"sqlite3";i:7;s:4:"zlib";i:8;s:6:"bcmath" ;i:9;s:8:"calendar";i:10;s:5:"ctype";i:11;s:4:"curl";i:12;s:3:"dom";i:13;s: 4:"hash";i:14;s:6:"filter";i:15;s:3:"ftp";i:16;s:2:"gd";i:17;s:3:"SPL";i:18 ;s:5:"iconv";i:19;s:7:"session";i:20;s:4:"json";i:21;s:8:"mbstring";i:22;s: 5:"mysql";i:23;s:6:"mysqli";i:24;s:5:"posix";i:25;s:10:"Reflection";i:26;s: 8:"standard";i:27;s:4:"imap";i:28;s:9:"SimpleXML";i:29;s:7:"sockets";i:30;s :4:"exif";i:31;s:9:"tokenizer";i:32;s:3:"xml";i:33;s:9:"xmlreader";i:34;s:9 :"xmlwriter";i:35;s:8:"cgi-fcgi";i:36;s:3:"PDO";i:37;s:10:"pdo_sqlite";i:38 ;s:6:"SQLite";i:39;s:17:"Zend Guard Loader";}

  10. #10
    Join Date
    Aug 2007
    Posts
    72
    Oh wait a minute, I used this code here and it's reporting a blank page; which I think is what it should do right?

    Code:
    <?php
    
    //Passing the XML
    $data = file_get_contents('1250.xml');
    $stats = new SimpleXMLElement($data);
    //-----------------------
    //Passing a filename
    $stats = new SimpleXMLElement('1250.xml', null, true);
    ?>

  11. #11
    Join Date
    Aug 2001
    Posts
    5,597
    A blank page would be fine. Do you get a valid object back?

  12. #12
    Join Date
    Aug 2007
    Posts
    72
    Quote Originally Posted by zoid View Post
    A blank page would be fine. Do you get a valid object back?
    What do you mean by a valid object? Nothing is displaying but a blank page, no errors are reporting.

  13. #13
    Join Date
    Aug 2007
    Posts
    72
    Okay, I think this is not going to work and I'm going to have to take a different approach. Maybe this isn't an XML file like I originally thought it was. Let me explain.

    This is the rest of the code I've since placed under the code I posted above.

    Code:
    <?php
    // load SimpleXML
    $stats = new SimpleXMLElement('1250.xml', null, true);
    echo <<<EOF
    <table>
            <tr>
                    <th>Kills</th>
            </tr>
    EOF;
    foreach($stats as $stats) // loop through our stats
    {
            echo <<<EOF
            <tr>
                    <td>{$stats->kills}</td>
    
            </tr>
    
    EOF;
    }
    echo '</table>';
    ?>
    Was hoping, for testing purposes, that it would display "Kills" with the number of kills under but it doesn't. All it displays is just the text "Kills" and nothing else. Then I got to looking at the data I am trying to pull and it's not laid out like the XML document in the tutorial I'm reading online. Here is a snippet of the data;

    Code:
    <statsfeed>
    <steamID64>76561198035683889</steamID64>
    <stats>
    <item>
    <APIName>kills</APIName>
    <value>35232</value>
    <displayName>Kills</displayName>
    </item>
    <item>
    <APIName>damagehealed</APIName>
    <value>19944</value>
    <displayName>Damage Healed</displayName>
    </item>
    As you can see, there is no way to for me to specify the kills value, since it's wrapped with a value tag, but ALL of the values in the document are wrapped with a value tag; thus the only way to figure out what value is what is looking at what's between the APIName values, then directly below that it will display the value for that value.

    Back to the drawing board now.

  14. #14
    Join Date
    Aug 2007
    Posts
    72
    Getting somewhere now... Seems I just needed to think about what I was doing first.

    Code:
    <?php
    
    //Passing the XML
    $data = file_get_contents('1250.xml');
    $stats = new SimpleXMLElement($data);
    //-----------------------
    //Passing a filename
    $stats = new SimpleXMLElement('1250.xml', null, true);
    ?>
    
    <?php
    // load SimpleXML
    $stats = new SimpleXMLElement('1250.xml', null, true);
    echo <<<EOF
    <table>
            <tr>
                    <th>Stat</th>
    				<th>value</th>
    				<th>Display Name</th>
            </tr>
    EOF;
    foreach($stats as $stats) // loop through our stats
    {
            echo <<<EOF
            <tr>
                    <td>{$stats->item->APIName}</td>
    				<td>{$stats->item->value}</td>
    				<td>{$stats->item->displayname}</td>
            </tr>
    
    EOF;
    }
    echo '</table>';
    ?>
    This gives me data on my page now!!! Still not in a good way though. For reference this is the data I am pulling:
    http://steamcommunity.com/id/lblanch...xml=1&schema=1

    This is what shows when the above code is pulls the data from above (but a physical file instead of the URL)

    Code:
    Stat	value	Display Name
    kills	35232	
    winwestlondonnormal	1
    Not really worried about the formatting at this point, but as you can see... it pull two different sets of data.

    the top half of the xml file
    Code:
    <statsfeed>
    <steamID64>76561198035683889</steamID64>
    <stats>
    <item>
    <APIName>kills</APIName>
    <value>35232</value>
    <displayName>Kills</displayName>
    </item>
    Towards the middle/lower part of the file:
    Code:
    </stats>
    <achievements>
    <item>
    <APIName>winwestlondonnormal</APIName>
    <value>1</value>
    <displayName>
    So I kind of see why it's pulling both sets of data. The "item" isn't in the same formatting as the "item" on the rest of the page above, it's almost as if the "item" has now replaced the "stats" within' the formatting, so the data is pulling that now? I don't really get it, but I sort of get it.

  15. #15
    Join Date
    Aug 2007
    Posts
    72
    Okay, I think I am going to take a different approach this. Instead of the way I'm doing it now (printing the data), I'm going to try to map the data so I can use it that way. Now where do I start on that? Google to the rescue.

Similar Threads

  1. php break up data by months to display?
    By lexington in forum Programming Discussion
    Replies: 14
    Last Post: 05-05-2009, 03:11 AM
  2. (PHP) Script won't display any data
    By calidude in forum Programming Discussion
    Replies: 21
    Last Post: 08-08-2008, 12:50 AM
  3. Display text when a link is clicked (javascript/php/html)
    By liquidgoat in forum Programming Discussion
    Replies: 1
    Last Post: 10-22-2005, 03:05 AM
  4. Display data in 3 columns in PHP
    By Mondeo in forum Programming Discussion
    Replies: 12
    Last Post: 09-08-2004, 10:42 PM
  5. Replies: 8
    Last Post: 07-27-2004, 06:26 AM

Posting Permissions

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