SimpleXML asXML Not Working As Expected

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
johnywhy
New php-forum User
New php-forum User
Posts: 1
Joined: Sun May 06, 2018 11:30 am

Sun May 06, 2018 11:33 am

Help file says:
SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element
http://php.net/manual/en/simplexmlelement.asxml.php
However, the following:

Code: Select all

$url = "https://gunretort.xyz/api.php?action=query&list=search&srsearch=NewTag&srwhat=text&srnamespace=0|3000&format=xml";
$xml = simplexml_load_file($url);
echo $xml->asXML;
returns nothing. Why?
thx
AdoptiveSolution
php-forum Super User
php-forum Super User
Posts: 167
Joined: Wed Jun 15, 2016 8:35 am

Mon May 07, 2018 4:04 am

To see what's returned, use this :

Code: Select all

<?php
echo '<pre>';
print_r( $xml );
echo '</pre>';
?>
Then, after studying the code, use this sample code to display the content :

Code: Select all

<?php
$url = "https://gunretort.xyz/api.php?action=query&list=search&srsearch=NewTag&srwhat=text&srnamespace=0|3000&format=xml";

$xml = simplexml_load_file($url);

echo '<p>Number of hits : <b>' . $xml->query->searchinfo['totalhits'] . '</b></p>';

foreach ($xml->query->search->p as $search) {
	echo '<p>';

	echo 'Title     : ' . $search['title']     . '<br />';
	echo 'Snippet   : ' . $search['snippet']   . '<br />';
	echo 'Page      : ' . $search['pageid']    . '<br />';
	echo 'Size      : ' . $search['size']      . '<br />';
	echo 'Wordcount : ' . $search['wordcount'] . '<br />';

	$datetime = new DateTime( $search['timestamp']);
	echo 'Date/Time : ' . $datetime->format('d M Y') . '/' . $datetime->format('h:i A');

	echo '</p>';
}
?>
Post Reply