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;
Related
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.
I have a problem with file_get_html(), i don't understand why it doesn't work can you help me? my code
$html = file_get_html('https://www.airbnb.fr/');
if ($html) {
echo "good";
}
Have a good day!
I think, server just blocks your request, you will not be able to fetch data from it, using simple HTTP requests.
You can try using curl, proxies, or both (there are ready to use solutions for this, like: AngryCurl, or RollingCurl)
It doesnt work because you have to include the simple_dom_html class to make it work. You can find the code on their official page:
http://simplehtmldom.sourceforge.net/
Then you can simply get the HTML and output it like this:
// Dump contents (without tags) from HTML
echo file_get_html('http://www.google.com/')->outertext;
or if you want to save the result in a variable
// Dump contents (without tags) from HTML
$html = file_get_html('http://www.google.com/')->outertext;
More info: http://simplehtmldom.sourceforge.net/
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;
I am trying to read this url from my php script:
http://test.cellpay.com.np:8181/cellcom?FN=123&fromMobile=01670746301&phoneNumber=01670746301&PIN=123456&amount=0
please load the url first, check the response and tell me what to do.
I tried the following:
file_get_contents
simplexml_load_string
simplexml_load_file
CURL
failed in all cases.
I've saved the xml and upload it to
http://vtopup.tk/1.xml
I can read this, but not the original one.
Please help me. I am tired of searching and applying...
Thanks.
Tested and it's working.
<?php
$url = 'http://test.cellpay.com.np:8181/cellcom?FN=ATW&fromMobile=testuser&phoneNumber=01670746301&PIN=z2ag&amount=0';
$xml = simplexml_load_file($url) or die("feed not loading");
var_dump($xml);
!!!!!!!!!Finally solved!!!!!!!!!
Thats all about php versions
I was trying from PHP 5.4.27, doesn't work....
Finally tried from PHP 5.6.3 and it works. so simply and magically.
Thanks to Veenex and file_get_contents returns empty string
for all effort.
Thank you guys, thank you very much.
I have some RSS that I am trying to parse from this URL:
http://weather.yahooapis.com/forecastrss?w=12797541
But when I try to parse it using PHP doing this:
$yahoo_response = new SimpleXMLElement($yahoo_url , 0, true);
echo $yahoo_response->rss->channel->item->title;
echo $yahoo_response->rss->channel->item->description;
Nothing gets outputed. Does anyone know what I am doing wrong here? I just need the current forecast bit.
Thanks,
Alex
The root element is <rss>, which is represented by the SimpleXmlElement you loaded into $yahoo_response.
echo $yahoo_response->getName(); // rss
You are trying to do <rss><rss> when you should do:
echo $yahoo_response->channel->item->title;
echo $yahoo_response->channel->item->description;
I've found that RSS feeds are best parsed using a library.
I've had much success with magpieRSS.
But your immediate problem is that you are passing a URL to simplexml, instead of the xml itself.
try $xml = file_get_contents($yahoo_url); first.