Cannot parse XML from a URL using file_get_contents() in PHP - 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

Related

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();
}

Passing xml to url

Im new to xml and i just want to pass an object to url and parse it to array . dont know what to do.
Here's my code:
$xml = "<user><name>".$name."</name><balance>".$balance."</balance><address>".$address."</address><mobile>".$mobile."</mobile></user>";
$simple = new SimpleXMLElement($xml);
$simple2 = $simple->asXML();
$sample = simplexml_load_string($simple2);
$url = $GLOBALS['urlPath'].'webportal_sqlanywhere/xml.php?id='.$(my xml);
$path_file = file_get_contents($url);
echo $path_file;

Get Json Value within XML - PHP

Say I have the following php:
$Url = sprintf( "http://www.wowhead.com/item=%u?xml", $EntryId );
$Xml = file_get_contents( $Url );
//echo htmlentities($Xml, ENT_COMPAT, 'UTF-8');
$Xml = simplexml_load_string( $Xml);
and lets say this is the xml:
http://www.wowhead.com/item=31065?xml
I know if T want say DisplayID I can do:
$DisplayId = $Xml->item->icon["displayId"];
However I want to get the values within the <json> part of the file.
<json>
<![CDATA[
"armor":464,"classs":4,"displayid":117596,"id":31064,"level":146,"name":"3Hood of Absolution","reqclass":16,"reqlevel":70,"slot":1,"slotbak":1,"source":[5],"sourcemore": [{"n":"Tydormu","t":1,"ti":23381}],"specs":[258],"subclass":1
]]>
</json>
I want to get the slotbak value, but I'm unsure how to do it. I did echo htmlentities($Xml, ENT_COMPAT, 'UTF-8'); to ensure I'm getting the xml file fine, and I am. But when I use json_decode or json_encode it tends to just either return { } { } { } or simply the object and not the value.
Does anyone know how I can do this?
You need to remove the <![CDATA[ and ]]> for json_decode to work.
Try this:
$json = $Xml->item->json;
$cleanedJson = str_ireplace(array('<![CDATA[', ']]>'), array('{', '}'), $json);
$jsonObject = json_decode($cleanedJson);

Response time of file_get_contents and valid xml

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.

Categories