Response time of file_get_contents and valid xml - php

I'm trying to read and store an RSS feed in my database using this method.
<?php
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
$xml = simplexml_load_string($homepage,'SimpleXMLElement', LIBXML_NOCDATA);
echo '<pre>';
print_r('$xml');
?>
But:
1. How can I check if `$homepage` contains a valid XML file or not?
2. I'm want to know how much time its taken to call if the url is valid XML file
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
using try and catch exceptions..

Try something like this
$start = microtime(true);
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
$end = microtime(true);
$duration = $end - $start;
try {
libxml_use_internal_errors() ;
$xml = new SimpleXMLElement($homepage, LIBXML_NOCDATA);
} catch (Exception $ex) {
// error parsing XML
throw $ex;
}
Edit: You can even combine the file_get_contents() call and SimpleXMLElement creation into one line using
$xml = new SimpleXMLElement('http://www.forbes.com/news/index.xml',
LIBXML_NOCDATA, true);
though any timing you wrap around that line will include HTTP retrieval and parsing

Below code will just works fine. Try it,
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
$xml = simplexml_load_string($homepage,'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
echo "<pre>";
print_r($xml);
echo "</pre>";
Thanks.

Related

Cannot parse XML from a URL using file_get_contents() in PHP

I'm trying to pull information from the following URL (https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL) using PHP, but for some reason I keep getting no information back.
After searching around a bit, I ended up with
<?php
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=1&stationString=KORL';
$xml = json_decode(file_get_contents($url));
echo $xml;
?>
EDIT: above code is obviously wrong... my updated (and still wrong) code is below.
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL';
$xml = simplexml_load_file($url) or die("feed not loading");
$string_data = $xml;
$xmlstr = simplexml_load_string($string_data);
$data = (string) $xmlstr->data->METAR->raw_text;
echo $data;
The information I need to get from this is <raw_text>.
Any help is greatly appreciated here!
No need to simplexml_load_string() when you simplexml_load_file(). Simply load the XML and access it like you used to:
<?php
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL';
$xml = simplexml_load_file($url) or die("feed not loading");
$data = (string) $xml->data->METAR->raw_text;
echo $data;
will output:
KORL 191653Z 08019G24KT 10SM SCT039 BKN085 29/18 A3018 RMK AO2 RAB17E26 SLP222 P0000 T02890183

PHP Domdocument use saveXML instead of save

I am creating an XML file in PHP like this...
$myXML = new DOMDocument();
$myXML ->formatOutput = true;
$data = $myXML ->createElement('data');
$data->nodeValue = 'mydata';
$final->appendChild($data);
$myXML ->save('/mypath/myfile.xml');
This works, but how can I convert this to use saveXML() instead? I have tried like this but I get nothing
$myXML->saveXML();
Where am I going wrong?
I see two things:
$final is not declared. Change it.
In case saveXML() is called, the output has to be assigned to a variable or printed
Here goes the working code:
<?php
$myXML = new DOMDocument();
$myXML ->formatOutput = true;
$data = $myXML ->createElement('data');
$data->nodeValue = 'mydata';
$myXML->appendChild($data);
echo $myXML ->saveXML();
?>
Output:
<?xml version="1.0"?>
<data>mydata</data>

How to read this XML with that has HTML tags with PHP?

I have been working several times with php and XML but this kind of XML has Html tags in the beginning and in the end:
Link To XML
there is no direct link to the xml file so I have to use file_get_contents().
Im using this php code:
$url = "https://www.tandildiario.com/suscripcion.php?section=4";
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
foreach ($feed->channel->item as $item) {
.....
I try different thing ..most of the errors are like this:
Warning: simplexml_load_string(): Entity: line 14: parser error : Entity 'oacute' not defined in D:\reader.php on line 37
Since the original XML is incorrect (it contains unescaped HTML in the description-tags), you can fix it before trying to parse it. Add the CDATA-attributes yourself:
$url = "https://www.tandildiario.com/suscripcion.php?section=4";
$xml = file_get_contents($url);
// Add the CDATA tags for the description
$xml = str_replace('<description>', '<description><![CDATA[', $xml);
$xml = str_replace('</description>', ']]></description>', $xml);
$feed = simplexml_load_string($xml);
You could decode the HTML entities prior to loading the XML.
$url = "https://www.tandildiario.com/suscripcion.php?section=5";
$xml = file_get_contents($url);
$feed = simplexml_load_string(html_entity_decode($xml, null, "UTF-8"));
foreach ( $feed->channel->item as $item ) {
echo $item->asXML();
}

How to load a xml file in php so that i can use xpath on it?

I have a problem with php,
If I implement this code below then nothing will be happen.
$filename = "/opt/olat/olatdata/bcroot/course/85235053647606/runstructure.xml";
if (file_exists($filename)) {
$xml = simplexml_load_file($filename, 'SimpleXMLElement', LIBXML_NOCDATA);
// $xpath = new DOMXPath($filename);
}
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$res = $xpath->query('/org.olat.course.Structure/rootNode/children/org.olat.course.nodes.STCourseNode/shortTitle');
foreach ($res as $entry) {
echo "{$entry->nodeValue}<br/>";
}
If I change the contents of $xml in the content with the content of the $filename
$xml = '<org.olat.course.Structure><rootNode class="org.olat.course.nodes.STCourseNode"> ... ';
then it works, so i think that there is something wrong with loading methode of the xml file,
I've also tried to load the xml file as a Domdocument but it won't work neither.
And in both cases, it does work if I collect xml data via xml
for example this works
echo $Course_name = $xml->rootNode->longTitle;
loadXML takes a string as input, not the return value of simplexml_load_file. Just use file_get_contents to get the (full) contents of a file as string

parsing a DOMNodeList in PHP

A web service return Xml of format
<string>
<NewDataSet>
<DealBlotter>
<CustomerReference>161403239</CustomerReference>
<Symbol>EUR/USD</Symbol>
<BuySell>S</BuySell>
<ContractValue>-100000</ContractValue>
<Price>1.35070</Price>
<CounterValue>-135070</CounterValue>
<TradeDate>2011-01-20 22:05:21.690</TradeDate>
<ConfirmationNumber>78967117</ConfirmationNumber>
<Status>C</Status>
<lTID>111913820</lTID>
</DealBlotter>
</NewDataSet>
</string>
Now i am using curl to access this and then -
$xml = simplexml_load_string($result);
$dom = new DOMDOcument();
// Load your XML as a string
$dom->loadXML($xml);
// Create new XPath object
$xpath = new DOMXpath($dom);
$res = $xpath->query("/NewDataSet/DealBlotter");
foreach($res as $node)
{
print "i went inside foreach";
$custref = ($node->getElementsByTagName("CustomerReference")->item(0)->nodeValue);
print $custref;
$ccy = ($node->getElementsByTagName("Symbol")->item(0)->nodeValue);
print $ccy;
$type = ($node->getElementsByTagName("BuySell")->item(0)->nodeValue);
$lots = ($node->getElementsByTagName("ContractValue")->item(0)->nodeValue);
$price = ($node->getElementsByTagName("Price")->item(0)->nodeValue);
$confnumber = ($node->getElementsByTagName("ConfirmationNumber")->item(0)->nodeValue);
$status = ($node->getElementsByTagName("Status")->item(0)->nodeValue);
$ltid = ($node->getElementsByTagName("lTID")->item(0)->nodeValue);
$time = ($node->getElementsByTagName("TradeDate")->item(0)->nodeValue);
}
But nothing is getting printed. except the dummy statement.
using $res = $xpath->query("/string/NewDataSet/DealBlotter"); did not help. Also a print_r($res); gives output as DOMNodeList obect.
Doing this also does not print anything
$objDOM = new DOMDocument();
$objDOM->load($result);
$note = $objDOM->getElementsByTagName("DealBlotter");
foreach( $note as $value )
{
print "hello";
$tasks = $value->getElementsByTagName("Symbol");
$task = (string)$tasks->item(0)->nodeValue;
$details = $value->getElementsByTagName("Status");
$detail = (string)$details->item(0)->nodeValue;
print "$task :: $detail <br>";
}
There are a few problems.
With how you're loading the xml. Get rid of the simplexml line. It's not needed, and is messing things up. Instead just do $dom->loadXml($result);. There's no reason to load SimpleXML first if you're going to pass it directly into DomDocument.
With your query, the / operator is the direct decendent operator. So it means directly next to. So your first tag should be the root. So either add the root onto it:
$res = $xpath->query("/string/NewDataSet/DealBlotter");
Or make the leading slash into // which selects any matching decendent:
$res = $xpath->query("//NewDataSet/DealBlotter");
And finally, doing a var_dump on $res isn't going to tell you much. Instead, I like to do var_dump($res->length) since it'll tell you how many matches it has rather than that it's a domnodelist (which you already know)...

Categories