0

I am very new to both php and xml. What I am trying to do in php is read in xml from a call to a url, and then parse the xml. (I can get this to work in the example below when $urlip = 'localfile.xml' but not when I put in a url. Ive checked the url by going to it with my browser, and I can see the xml. I also did a show source, copied it and then pasted the xml into the localfile and that works fine.
What am I doing wrong in trying to get the xml from the url?

Thank you

The error being returned is: Error loading XML Start tag expected, ‘<' not found

Here is my code snip it:

$urlip="test.xml";# for debugging since I cannot read from the url yet!  not sure why....
if (($xml = file_get_contents($urlip))===false) {
   echo "error fetching XML\n";
} else {
    libxml_use_internal_errors(true);
    $data = simplexml_load_string($xml,null,LIBXML_NOCDATA);
    if (!$data) {
        echo "Error loading XML\n";
        foreach(libxml_get_errors() as $error) {
            echo "\t", $error->message;
        }
    } else {
        foreach ($data as $item) {
            $type = $item->TAB_TYPE;
            $number=$item->ALT_ID;
            $title = $item->SHORT_DESCR;
            $searchlink = $item->ID;
            $rsite=$item->CATEGORY;
            echo "type $type, number $number, title $title, search link $searchlink, site $rsite\n";
        }
    }
}
Kat L
  • 1
  • 1- You can print the result of file_get_contents action and see if the XML is loaded. Depeding on PHP configuration this function can't read remote files, an alternative ca be use curl to crawl the XML. Here an example how to use CURL in php http://davidwalsh.name/curl-download – Dayron Gallardo Jul 17 '14 at 20:56
  • `Error loading XML Start tag expected, ‘<' not found` .. did you completely verify the syntax in the xml file.. the error points to a missing `<` – sunbabaphu Jul 17 '14 at 20:56
  • That error means there is a problem with your xml and not your php – Michal Jul 17 '14 at 20:56
  • is url_fopen enabled? without that, file_get_contents cannot fetch remote urls. or the url returns something OTHER than xml which you blindly try to stuff into simplexml. have you tried a `var_dump($xml)` after file_get_contents to see what you DID receive? – Marc B Jul 17 '14 at 20:57
  • You need to share the URL which makes you problems. Hiding it won't explain the issue you have. If you need some guidance on how to trouble-shoot your issue with the remote URL, there is some debugging information available here: http://stackoverflow.com/a/24711469/367456 – hakre Jul 21 '14 at 18:51

1 Answers1

0

Most likely situation from what it looks like:

  1. Your function queries the remote URL and returns you an empty string, which passes the condition of your 'if' statement.

  2. After that - you try to pass the empty string into XML, but it cannot, so it gives you an error.

Your steps to solve it:

  1. configure php to open remote urls as comments to your question state - url_fopen
  2. use another way to get content from the URL - cURL library works well
Anatoliy Kim
  • 768
  • 4
  • 13