Page 1 of 1

loading xml file from url problem

Posted: Thu Mar 13, 2008 9:06 am
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

Re: loading xml file from url problem

Posted: Thu Mar 13, 2008 9:11 am
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');
 

Re: loading xml file from url problem

Posted: Thu Mar 13, 2008 10:29 am
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

Re: loading xml file from url problem

Posted: Thu Mar 13, 2008 10:43 am
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();
}

Re: loading xml file from url problem

Posted: Fri Mar 14, 2008 6:22 am
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