loading xml file from url problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edzillion
Forum Newbie
Posts: 12
Joined: Mon Jan 28, 2008 10:06 am

loading xml file from url problem

Post by edzillion »

my setup is this
I have a php file that displays prices, it calls another php file that gets the xml file from a URL:

pricefeed.php:

Code: Select all

<?php
 
    $fileUrl = "http://someurl.com/prices.xml";
    $AgetHeaders = @get_headers($fileUrl);
 
    if (preg_match("|200|", $AgetHeaders[0])) {
        // file exists
        echo"file exists";
        readfile($fileUrl);
    } 
    else 
    {
        // file doesn't exist
        echo"file doesn't exist";
        return false;
    }
?>
 
If I run this when the url is unavailable I get "file doesn't exist", but I can't figure out how to call this file without a parsing error if the file is unavailable.

The calling line:

Code: Select all

 
$xml = simplexml_load_file('http://localhost/scripts/pricefeed.php');

Gives the error:
Warning: simplexml_load_file() [function.simplexml-load-file]: http://localhost/scripts/pricefeed.php:1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\prices.php on line 17
If I get rid of the "file exists" echoes from the pricefeed.php file I get
Document is empty in C:\xampp\htdocs\prices.php on line 17
Any ideas?
TIA
Ed
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: loading xml file from url problem

Post by anto91 »

Code: Select all

 
$xml = simplexml_load_file('http://localhost/scripts/pricefeed.php');[
 
TO

Code: Select all

 
$xml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'scripts'.DIRECTORY_SEPARATOR.'pricefeed.php');
 
edzillion
Forum Newbie
Posts: 12
Joined: Mon Jan 28, 2008 10:06 am

Re: loading xml file from url problem

Post by edzillion »

Im not sure exactly what you are suggesting here, but I should probably mention that my script works fine if the website hosting the xml file is up.
I just want some redundancy if the site I am getting the xml prices from goes down.

Thanks
Ed
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: loading xml file from url problem

Post by Zoxive »

Code: Select all

Warning: simplexml_load_file() [function.simplexml-load-file]: http://localhost/scripts/pricefeed.php:1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\prices.php on line 17
This error is suggesting the file pricefeed.php is not a valid xml document. Because it could not find the opening tag.

Code: Select all

try {
   $xml = new SimpleXMLElement($str);
} catch (Exception $e) {
   // handle the error
   echo $e->getMessage();
}
edzillion
Forum Newbie
Posts: 12
Joined: Mon Jan 28, 2008 10:06 am

Re: loading xml file from url problem

Post by edzillion »

using your try catch suggestion.
If I try to load the file:

Code: Select all

   try {
$xml = simplexml_load_file($feedStr);
The exception isn't caught.
Warning: simplexml_load_file() [function.simplexml-load-file]: scripts/pricefeed.php:18: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\wpgoldprices.php on line 18
Warning: simplexml_load_file() [function.simplexml-load-file]: ?> in C:\xampp\htdocs\wpgoldprices.php on line 18
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in C:\xampp\htdocs\wpgoldprices.php on line 18
If I use simpleXMLElement I catch the exception

Code: Select all

   
try {
$xml = new SimpleXMLElement($feedStr);
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\wpgoldprices.php on line 17
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: scripts/pricefeed.php in C:\xampp\htdocs\wpgoldprices.php on line 17
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in C:\xampp\htdocs\wpgoldprices.php on line 17
Caught exception: String could not be parsed as XML
Any Ideas?
Ed
Post Reply