I am trying to write an API for a mobile application. I would like to return the data in XML. I am using the PHP library built for Wolfram Alpha to query their data, and want my API to act as a go between for the mobile application and Wolfram Alpha. Whenever I try to print an XML document after using the Wolfram Alpha PHP library, I get an error from Chrome stating:
error on line 7 at column 6: XML declaration allowed only at the start of the document
I get nothing in IE, and in Firefox I get:
XML Parsing Error: XML or text declaration not at start of entity
Location: http://myfitnessplate.com/api/food-search/name/thisissomethingnotfood/key/HWGCF-YASLT-HOSEC-OZWJH-HETKJ
Line Number 7, Column 1:
<?xml version="1.0"?>
^
With no other data. I opened up the main file for the Wolfram Alpha PHP library (WolframAlphaEngine.php) and noticed that it is using SimpleXML to query Wolfram Alpha in the following code
$xml = simplexml_load_file( $url );
I get the XML error whenever I use the library. If I don’t use the library, the XML outputs just fine. Is it possible that the “simplexml_load_file” is causing my XML to break for some reason?
My code that doesn’t work when using the library that uses simplexml_load_file (this does work when not using the library):
$xml = new SimpleXMLElement('<xml/>');
header("Content-Type:text/xml");
echo $xml->asXML();
I strongly suspect that there are seven lines of output somewhere before you echo out your XML. You need to silence this output, because it is causing your xml to become invalid, which is causing your browsers to go crazy.
Possibly it is error messages from your included library. Possibly there is leading or trailing whitespace in your php files. The only way to know is to see the raw output before the browser starts parsing it.