I'm currently trying to parse the MapQuest Traffic API, but when I try to display an incident, nothing appears, and if I do "if empty" in php, it returns empty.
Here's the code:
<?php
$mysongs = simplexml_load_file("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox=$_GET[a], $_GET[b], $_GET[c], $_GET[d]&filters=construction,incidents&inFormat=kvp&outFormat=xml");
echo $mysongs->Incidents[0]->Incident[0]->fullDesc;
?>
The parameters I'm passing: ?a=33.352532499999995&b=-118.2324383&c=34.352532499999995&d=-117.2324383.
Thanks in advance!
Here simplexml_load_file not loading all your xml data so, i created a xml file with name test.xml and then loaded data from test.xml. Now you can print data what you need.
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$xml_feed_url = 'http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd|luuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox='.$a.','.$b.','.$c.','.$d.'&filters=construction,incidents&inFormat=kvp&outFormat=xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$xml2 = new SimpleXMLElement($xml);
$xml2->asXML("test.xml");
$xml2->asXML();
$mysongs = simplexml_load_file("test.xml");
print_r($mysongs);
?>
Related
I want to parse the following XML file.
What I have so far is:
$xml = new SimpleXMLElement('http://smarkets.s3.amazonaws.com/oddsfeed.xml', LIBXML_NOCDATA, true);
foreach ($xml->odds->event as $item) {
echo (string)$item->market;
}
But this does not work. Can you help me?
You can try with php CURL:
$ch = curl_init();
$url = 'http://smarkets.s3.amazonaws.com/oddsfeed.xml';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
print_r($xml);
I have no idea which information you want to extract so here is an example how to get the attributes 'id' and 'slug' from all market nodes.
Just add compress.zlib:// to your url to get the xml, for PHP 4.3.0 and up
<?php
$xml = simplexml_load_file('compress.zlib://http://smarkets.s3.amazonaws.com/oddsfeed.xml') or die("Error: Cannot create object");
foreach ($xml->event as $item) {
echo $item->market['id'] . "<br>" . $item->market['slug'] . "<br><br>";
}
?>
I'm a php and javascript newbie so don't be mad.
Ok so i want to get xml file from another server and then extract only one data(number) and put it in javascript variable so i can calculate with it.
I have this code that gets xml file fine, and i get $result variable with data that i need, but i can't save that data in javascript variable.
The reason i need this number in javascript is because all of calculations are coded in javascript.
this is php code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://www.pbz.hr/Downloads/PBZteclist.xml');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
echo($xml);
curl_close($ch);
$xmlObject = new SimpleXMLElement($xml);
$node = $xmlObject->children();
$result = $node[0]->Currency[12]->BuyRateForeign;
echo("<br />");
echo("<br />");
echo $result;
?>
I need data from EUR7,480000
That number 7,480000 is all i need to extract and save in javascript variable.
Maybe the way i'm doing this isn't right, but i have tryied everything. Please help me.
I would suggest grabbing the value using an xpath, in case the order changes up.
If you want to use as a string, wrap it in quotes. If you are using it in calculations you might need to strip out the commas etc...
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://www.pbz.hr/Downloads/PBZteclist.xml');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
//echo($xml);
$sxe = new SimpleXMLElement($xml);
$result = $sxe->xpath("//Currency[#Code='978']");
$BRF = $result[0]->BuyRateForeign;
?>
<script>
var bfr_string = '<?= $BRF?>'; //string
var bfr_number = <?= str_replace(',','.',$BRF)?>; //number
</script>
Assuming you don't intend to do this with Ajax:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.pbz.hr/Downloads/PBZteclist.xml');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
echo($xml);
curl_close($ch);
$xmlObject = new SimpleXMLElement($xml);
$node = $xmlObject->children();
$result = $node[0]->Currency[12]->BuyRateForeign;
?>
<script type="text/javascript">
var rate = '<?php echo $result; ?>';
</script>
If you want the data to be set in javascript on page load, you can use php to echo a javascript string to initiate a variable.
In PHP:
<script>
<?php echo "var data = ".$result; ?>
</script>
If you are getting the data asynchronously you will have to use javascript AJAX to call a PHP page that outputs your data in some kind of format (JSON), or just a single string value.
I am trying to use CURL to grab an XML file associated with this URL, then i am trying to parse the xml file using DOMxPath.
There are no output errors at this point it is just not displaying anything, i tried to catch some errors but i was unable to figure it out, any direction would be amazing.
<?php
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
function tideTime() {
$ch = curl_init("http://tidesandcurrents.noaa.gov/noaatidepredictions/NOAATidesFacade.jsp?datatype=XML&Stationid=8721138");
$fp = fopen("8721138.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$dom = new DOMDocument();
#$dom->loadHTML($ch);
$domx = new DOMXPath($dom);
$entries = $domx->evaluate("//time");
$arr = array();
foreach ($entries as $entry) {
$tide = $entry->nodeValue;
}
echo $tide;
}
?>
Youre trying to load the curl resource handle as the DOM which it is not. the curl functions either output directly or output to string.
$ch = curl_init("http://tidesandcurrents.noaa.gov/noaatidepredictions/NOAATidesFacade.jsp?datatype=XML&Stationid=8721138");
$fp = fopen("8721138.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
$dom = new DomDocument();
$dom->loadHTML($data);
// the rest of the code
it seems you try to catch some unavailable xpath, make sure you have ("//time"); in the xml file, are you sure that you grab is a xml file ? or you just put into xml ?
if we look at that page, it seems xml generated by javascript, look at the http://tidesandcurrents.noaa.gov/noaatidepredictions/NOAATidesFacade.jsp?datatype=XML&Stationid=8721138&text=datafiles%2F8721138%2F09122011%2F877%2F&imagename=images/8721138/09122011/877/8721138_2011-12-10.gif&bdate=20111209&timelength=daily&timeZone=2&dataUnits=1&interval=&edate=20111210&StationName=Ponce Inlet, Halifax River&Stationid_=8721138&state=FL&primary=Subordinate&datum=MLLW&timeUnits=2&ReferenceStationName=GOVERNMENT CUT, MIAMI HARBOR ENTRANCE&HeightOffsetLow=*1.00&HeightOffsetHigh=* 1.18&TimeOffsetLow=33&TimeOffsetHigh=5&pageview=dayly&print_download=true&Threshold=&thresholdvalue=
may be you can grab that
I'm trying to parse an xml file by starting with simplexml_load_file to load the contents. The file comes from a wordpress using an xml feed generated by a .php file.
The problem is it never can load the xml file..I'm not sure what I can do to make this work. Here is the code
<?php
$url = "http://marshallmashup.usc.edu/feed.php";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
$rss = simplexml_load_string($result);
if( ! $rss = simplexml_load_file($url,NULL, LIBXML_NOERROR | LIBXML_NOWARNING) )
{
echo 'unable to load XML file';
}
else
{
echo 'XML file loaded successfully';
}
?>
First of all after this line:
$result = curl_exec($ch);
you should add this one:
$result = utf8_encode($result);
Said that, you'll have no problems with the function simplexml_load_string($result); which will correctly create a DOM based on the string you give to the function and that is the feed gotten from the php page. You can see the result using var_dump($rss); after the statement $rss = simplexml_load_string($result);.
I managed to get this code so far:
<?php
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
foreach ($results->resultset_news->result as $theresult) {
echo ''.$theresult->title.'<br/>';
echo $theresult->abstract.'<br/>';
echo '<small><i>'.$theresult->dispurl.'</i></small><br/>';
echo '<br/><br/>';
}
So how exactly do i output actual XML rather than HTML?
Tried this:
echo $results->asXML();
?