That is how XML is displayed in the browser when the header being sent is Content-type: text/html
. The demo example you link to that shows formatted XML uses some special formatting to get it looking like that. You need to set the header to be text/xml like header('Content-type: text/xml');
and then the output should display formatted.
header('Content-type: text/xml');
echo $op;
You can also output your content like so:
echo '<pre>';
echo htmlentities($op);
echo '</pre>';
The above explains why XML shows up unformatted in the browser and demonstrates how to fix that. The OP's main problem is that his XML is malformed due to that stray string at the the end of the output. The following deals with that:
$r = 'http://query.yahooapis.com/v1/public/yql';
$p = "q=select * from contentanalysis.analyze where text='Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration'";
$c = curl_init($r);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $p);
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$op = curl_exec ($c);
curl_close ($c);
if (!($xml = strstr($op, '<?xml'))) {
$xml = null;
}
header('Content-type: text/xml');
echo $xml;