Zoopla API - Output Data (XML) - php

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;

Related

file_get_html() not working with airbnb

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/

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 script to generate url for php api

i need some help as i am completely novice with php..
I have made a simple script to get an XML tracklist (from an API), then decode what i need and make a ul list with the tracks..
I also need to pull some artist images from a last.fm php script i found recently..
The scripts api is using http adress to get the artist and then bring up the images i found it here https://gist.github.com/iwek/5109952 and the syntax of request is : http://example.com/lastfm.php?artist=Adele
My php code for tracklist is :
foreach ($mysongs as $item):
$x = explode(" - ",$item->track);
$artist = $x[0];
$title = $x[1];
echo "<ul>$artist
$song</ul>\n";
endforeach;
Now i need to put inside the foreach the php code to generate the api call url and also include the image that this script outputs.. I am really stuck here guy's !
If anyone has any idea please do tell !
PS: keep in mind that i am really novice with php..
Thank you for your answers
$url = "http://www.example.com/lasfm.php";
$artist="Adele";
echo "$url?artist=$artist";

removing elements from xml with php

I am building an iphone app which allows people to update an xml file by sending a POST to a php script. After they send the post to the php script and the xml is updated, I would like the user to be able to cancel the update to the XML. How can I delete just one element from the XML and rewrite the XML file to the same location on the server (in other words, just the one element is now gone, everything else is the same)? To add an element I used code that looks like the following:
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xml = new SimpleXMLElement($xmlStr);
$bartenders = $xml->xpath('//Bartenders');
$new_bartender = $bartenders[$newBar_ID]->addChild('Bartender');
$new_bartender->fname = $newfname;
$new_bartender->lname = $newlname;
$new_bartender->imageURL = $newimageURL;
$new_bartender->shift = $newShift;
print_r($bartenders);
$xml->asXML('Bars.xml');
When I send the POST method to the php script, I have the element's attribute to identify which is to be deleted.
Thanks for your help.
Possible duplicate of THIS.
From your code snippet I don't really know what data is given. You could try to do either
unset($bartenders[$newBar_ID]);
or
$bartenders->removeChild($bartenders[$newBar_ID]);
I guess... I wrote these based on code snippets google turned up, never tested them. Feel free to give me feedback if it works or not.

user servicesidekick API using PHP

I want to use the servicesidekick API using php, as per their documentation at here: http://www.servicesidekick.com/help/api
They are suggesting to use curl for this. I am suppose to get xml file in response.
Below is my code:
exec('command',$data);
when I print the $data then its giving each line of xml in each element of Array. But i want it to be accessed as the XML file.
You can use the SimpleXML functions to make things easy. Here is a very basic example that prints out some comma-deimited fields:
<?php
$token = "123xxx";
$account = "myaccount";
$url = "https://{$token}:{$token}#{$account}.servicesidekick.com/jobs.xml";
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->children() as $job) {
echo "{$job->id},{$job->name},{$job->{"job-number"}},{$job->{"balance-remaining"}} \n";
}
?>
It's pretty easy to get access to all of the fields. This example just demonstrates jobs, but all of the entities can be called the same way. Good luck!
Please read the docs of the PHP curl bindings.
There is no function curl() which does lots of magic like you apparently assume in your code.

Categories