PHP not parsing XML in expected format? - php

Im trying to pull this data into PHP, ultimately get it into Javascript so I can make some graphs.
When I download the data using cURL from my mac terminal, I open it in xCode and it looks exactly as expected. No issues accessing the website for data:
curl "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/‌​XmlView.aspx?data=yieldyear&year=2015" > test.xml
open test.xml
When I try to pull into PHP, the xml looks very different. For example, the d:BC_1MONTH tag just isn't present:
$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$xml = simplexml_load_file($url);
print_r($xml);
How do i use php to get XML data in the same format as it is on the website and with cURL download?

print_r() doesn't work as expected with SimpleXMLElement objects. You should do this instead: echo $xml->asXML()
When I did that with your code above I saw the element you were asking about.

Related

PHP won't receieve POST XML data

I'm building a service backend that is being sent a "delivery report" after successfully sending a SMS to a user.
The report itself is XML POSTed to our "endpoint" with the content-type application/xml.
I'm using Postman to make sure that everything is working correctly. I've made a test using regular JSON and can return the data without issues, however, no matter what I try with XML I basically get no indication that anything is being sent to the server.
(Test with JSON)
(Test with XML)
Here's my simple PHP script:
<?php
header('Content-Type: application/xml; charset=utf-8');
print_r(json_decode(file_get_contents("php://input"), true));
print_r($HTTP_RAW_POST_DATA);
?>
I feel like I've tried everything. Been looking at past issues posted to SO and other places, it simply won't work for me. I'm hoping for some answers that at least points me in the right direction here.
Cheers.
Your trying to json_decode XML data. You should use something like SimpleXML.
Instead of...
print_r(json_decode(file_get_contents("php://input"), true));
You should use ...
$xml = new SimpleXMLElement(file_get_contents("php://input"));
echo $xml->asXML();
You should be able to get the information by (for example)...
echo (string)$xml->id;
json_decode can't read XML, seems like you're trying to parse XML with json_decode. if you want to output the received XML, use echo (or if it's for debugging purposes, use var_dump, eg var_dump(file_get_contents("php://input"));), or if you want to parse the XML, use DOMDocument.

Zoopla API - Output Data (XML)

I'm attempting to use the Zoopla API (http://developer.zoopla.com/docs/read/Property_listings) to output specific data.
I have tested the API using a simple echo after the "file_get_contents() method, which shows the data. Example code shown below (API Key Removed)
$url = "http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=CF11&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$zoopla = file_get_contents($url);
echo $zoopla;
What Im trying to code is a loop that will allow me to add html tags so that I can style them. I've done similar for a RSS feed but can't figure out a way for this XML.
I have also tried an alternative approach using simplexml_load_file()
$xml = simplexml_load_file($url);
$agent_address = $xml->agent_address->agent_address[1]->agent_address;
echo $agent_address;
Any help would be greatly appreciated!
I found the answer to my own question!
Basically the $URL is a string and not a file "simplexml_load_file()"
So first, we need to get the xml file as a string and then parse the file. Code as followed! Works like a treat!
$zoopla = file_get_contents('http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=CF64&api_key=xxxxxxxxxxxxxxxxx');
$properties = simplexml_load_string($zoopla);
echo $properties->listing[2]->agent_phone;

PHP RSS parser - - not working

Hello I'm working on parsing this feed and cannot seem to get it to work
<?php
$url = "http://insite.unthsc.edu/dailynews/category/campus-news/feed/";
$xml = simplexml_load_file($url);
print_r($xml);
?>
I get this currently when I output pre
I've done this before for other feeds and xml files but cannot for the life of me figure out why it is not working this time.
Thanks
The problem is right in the error message: you need to authenticate before loading the XML resource.
Open the link in an Incognito window, or
curl http://insite.unthsc.edu/dailynews/category/campus-news/feed/
and you'll see what PHP sees.

get data from xml url using php

I am trying to get data from xml url using php but its not working for me. Please check what is wrong in my code.
$result_xml = simplexml_load_file(
"https://maps.googleapis.com/maps/api/geocode/xml?address=UB67JJ"
);
echo $result_xml->GeocodeResponse->result->formatted_address;
I want to get "formatted_address" from xml file.
Don't know where the GeocodeResponse comes from, but it is not in the XML response.
It should just be
echo $result_xml->result->formatted_address;
See https://developers.google.com/maps/documentation/geocoding/
If i remember right, the $result_xml already referes to the outer xml-container, so try:
echo $result_xml->result->formatted_address;

Php and XML connection(SimpleXML)

So im trying to make a connection with steam api and my site.Steam api returns format in XML, so i have created a connection via PHP to my server using simplexml.it is something like this:
$xml=simplexml_load_file("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxx&steamids=xxx&format=xml");
print $xml->response->players->player->steamid;
And the xml file code is :
<response><players><player><steamid>xxxx</steamid></response></players></player>
So in return I get nothing while i should get this number: xxxx
Also I can get whole document with print_r($xml); but taking only some tags seems not to work
Any tips?
Thanks in advance

Categories