Jump to content

Trouble loading urlencoded xml file using simplexml_load_file...


phpFTW

Recommended Posts

Hello fellow developers,

 

Let me start by saying I'm still a PHP noob so please don't go too hard on me if I my code reflects this noobishness...

 

So I'm trying to build a (very) simple RSS reader using jQuery and PHP. When the user clicks on an option from a dropdown, an AJAX call is made and passes the url selected from the list to a PHP script.

 

The PHP script then loads the appropriate xml file from an 'xmlcache' folder. Everything is working just fine for files named as such:

 

somefilename_1231231231.xml

 

The problem is that I need it to be able to load files which have been urlencoded such as:

 

http%3A%2F%2Ffeeds.feedburner.com%2Fajaxian_1235523548

 

In case you're wondering, the numbers at the end are a timestamp which will determine if the reader will load a cached copy of the desired xml file (if it's less than an hour old) or if it needs to get a fresh version (this part of the code is not written yet). Here is a look at my script thus far:

 

<?php

$url = "./xmlcache/" . urlencode($_REQUEST['url']); //access url from AJAX call
echo $url; //outputs -> "./xmlcache/http%3A%2F%2Ffeeds.feedburner.com%2Fajaxian_1235523548.xml", which is the same as the file's name
$curTime = time();
$urlParts = explode('_', $url);
$urlTime = intval($urlParts[1]);
$diff = $curTime - $urlTime;
$urlAge = 60 * 60; //60sec * 60min = 3600sec or 1 hour

if(file_exists($url)){
        if($diff > $urlAge){ //the > will be switched to < to make logical sense once testing is done.
                $xml = simplexml_load_file($url);
               
                //ROOT TAG
                foreach($xml as $key0 => $value){
                        echo "$key0: $value";
                        foreach($value->attributes() as $attributeskey0 => $attributesvalue1){
                        echo "$attributeskey0: $attributesvalue1";
                        }
                        echo "<br />";
                       
                        //1 LEVEL BELOW ROOT TAG
                        foreach($value as $key => $value2){
                                echo "$key: $value2";
                                foreach($value2->attributes() as $attributeskey => $attributesvalue2){
                                        echo "$attributeskey = $attributesvalue2";
                                }
                                echo '<br />';
                               
                                //2 LEVELS BELOW ROOT TAG
                                foreach($value2 as $key2 => $value3){
                                        echo "$key2: $value3";
                                        foreach($value3->attributes() as $attributeskey2 => $attributesvalue3){
                                                echo "$attributeskey2 = $attributesvalue3";
                                        }
                                        echo '<br />';
                                        //3 LEVELS BELOW ROOT TAG
                                        foreach($value3 as $key3 => $value4){
                                                echo "$key3: $value4";
                                                foreach($value4->attributes() as $attributeskey3 => $attributesvalue4){
                                                echo "$attributeskey3 = $attributesvalue4";
                                                }
                                                echo '<br />';
                                                //4 LEVELS BELOW ROOT TAG
                                                foreach($value4 as $key4 => $value5){
                                                        echo "$key4: $value5";
                                                        foreach($value5->attributes() as $attributeskey4 => $attributesvalue5){
                                                                echo "$attributeskey4 = $attributesvalue5";
                                                        }
                                                        echo '<br />';
                                                        //5 LEVELS BELOW ROOT TAG
                                                        foreach($value5 as $key5 => $value6){
                                                                echo "$key5: $value6";
                                                                foreach($value6->attributes() as $attributeskey5 => $attributesvalue6){
                                                                        echo "$attributeskey5 = $attributesvalue6";
                                                                }
                                                                echo '<br />';
                                                        }
                                                }
                                        }
                                }
                                echo '<br />';
                        }
                        echo '<br />';
                }

        }else{
                //need to create the XML file from scratch, do a curl function, or read from a text file
                echo "need a new version";
                //@unlink("$url);
        }
       
}else{
        echo "That file does not exist.<br/>";
}

?>

 

The error message I'm getting is this:

 


 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "./xmlcache/http%3A%2F%2Ffeeds.feedburner.com%2Fajaxian_1235523548.xml" in C:\wamp\www\reader\loadfeed.php on line 13

 

Warning: Invalid argument supplied for foreach() in C:\wamp\www\reader\loadfeed.php on line 16

 


 

The second error is obviously thrown because the file doesn't get loaded.

 

I appreciate any help you gurus have to offer, and if you have any suggestions for the best way to load a fresh version that will be handled by the first else statement, I'm all ears.

 

Thanks guys!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.