How to get value from parsed XML - php

This is my XML with name test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:test="http://www.test.com/rss/2.0">
<positionfeed>
<test:import user="accounts#test.com" site="www.test.com" />
<job>
<id>83762455</id>
<employer>Mount Ida College</employer>
<title>Assistant Professor, Dental Hygiene</title>
<description>As a faculty member, you will provide instruction in didactic courses, clinics, and/or labs in the Dental Hygiene Program.</description>
<postingdate>2016-10-21</postingdate>
<joburl>http://url.com</joburl>
<location>Newton, MA</location>
<category>Education</category>
</job>
<job>
<id>83762405</id>
<employer>Veterinary Orthopedic & Sports Medicine Group</employer>
<title>Overnight Recovery Technician</title>
<description>As a faculty member, you will provide instruction in didactic courses, clinics, and/or labs in the Dental Hygiene Program.</description>
<postingdate>2016-10-25</postingdate>
<joburl>http://url.com</joburl>
<location>Annapolis Junction, MD</location>
<category>Installer/Technician</category>
</job>
</positionfeed>
</rss>
This is my PHP page where I'm parsing it:
<?php
$languages = simplexml_load_file("test.xml");
print_r($languages);
foreach ($languages->job as $job) {
echo $job->title;
echo "<br>";
}
?>
I want:
job->id
job->title
and so on
=============================
print_r($languages) gives:
SimpleXMLElement Object ( [#attributes] => Array ( [version] => 2.0 )
[positionfeed] => SimpleXMLElement Object ( [job] => Array ( [0] =>
SimpleXMLElement Object ( [id] => 83762455 [employer] => Mount Ida
College [title] => Assistant Professor, Dental Hygiene [description]
=> As a faculty member, you will provide instruction in didactic courses, clinics, and/or labs in the Dental Hygiene Program.
[postingdate] => 2016-10-21 [joburl] => http://url.com [location] =>
Newton, MA [category] => Education ) [1] => SimpleXMLElement Object (
[id] => 83762405 [employer] => Veterinary Orthopedic & Sports Medicine
Group [title] => Overnight Recovery Technician [description] => As a
faculty member, you will provide instruction in didactic courses,
clinics, and/or labs in the Dental Hygiene Program. [postingdate] =>
2016-10-25 [joburl] => http://url.com [location] => Annapolis
Junction, MD [category] => Installer/Technician ) ) ) )
============================
I tried researching a lot.
If I remove below three lines manually the PHP code works fine and displays values as needed, but I am unable to remove them dynamically.
<rss version="2.0" xmlns:test="http://www.test.com/rss/2.0">
<test:import user="accounts#test.com" site="www.test.com" />
</rss>
So either of the way will work, if anyone can help, either help me to remove these three lines dynamically on load on PHP page else able to get values of the required nodes.
Again as description is long, I want:
job->id
job->title
and so on

Related

php sort multidimensional array on date column

I have an array $dataStoreForTableImport set up in the following way.
$dataStoreForTableImport['title']
$dataStoreForTableImport['content']
$dataStoreForTableImport['date']
$dataStoreForTableImport['link']
$dataStoreForTableImport['username']
$dataStoreForTableImport['website']
It contains data as below
Array
(
[0] => Array
(
[title] => Quote from Tony Blair
[content] => ... from beating it I'm afraid." (Tony Blair, Sky News) He had every opportunity to put religion in its ...
[articledate] => 28/09/2013
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12890951
[Username] => Michael Dray
[website] => The Motley Fool
)
[1] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... I am irritated that he got such an easy ride; Why? Because he is not to your political liking? He was a witness; he was not on trial and he spoke under oath. What did you expect Jay to ask him? I had dealings with a QC a few years ago. He was as ...
[articledate] => 28/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12564154
[Username] => Michael Dray
[website] => The Motley Fool
)
[2] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... If your doubts about Jay's competence/bias were shared I'm sure it would have been debated ad nauseam on Radio 4. Eh - are you serious? I'm a Radio 4 fan - but thats despite its hatred of all things right of centre, not becuase of. ...
[articledate] => 28/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12564167
[Username] => Michael Dray
[website] => The Motley Fool
)
[3] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... Maybe Tony should have brought Cherie with him - remember Rupert Murdoch and the reaction of Wendi Deng to the custard pie incident. IMHO Cherie is every bit as intimidating:-) Wendi Deng did not eject the pie flinger, she intervened when he acted. Use of an angled bat to deflect criticism from ...
[articledate] => 30/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12565346
[Username] => Michael Dray
[website] => The Motley Fool
)
[4] => Array
(
[title] => Re: The Tony Blair Show
[content] => ... What did surprise me was the fact that he had his own personal bodyguards in the hearing with him. Although, given the level of security that allowed that protester to break into the hearing, maybe he had a point! Eh? He is clearly at risk of terror ...
[articledate] => 28/05/2012
[link] => http://boards.fool.co.uk/message.asp?source=isesitlnk0000001&mid=12564500
[Username] => Michael Dray
[website] => The Motley Fool
)
I want to be able to remove rows from this array that if the articleDate is before a given date.
I have tried everything but it does not seem to work. I am not even able to get it to sort correctly by date?
The date comes in the format - February 10, 2007
I have used
$sortDate = date('d/m/Y', strtotime($sortDate));
to format it to the format shown in the array above.
Can anyone please help?
Thanks
Mike
Do the filtering and sorting of rows in the database backend. This will improve the performance of you app. Use a WHERE clause for filtering by date and an ORDER BY clause for ordering by date in you SQL query.

PHP Array traversing foreach loop[?

Array
(
[0] => LinkedIn Corporation
[1] => www.linkedin.com
[2] => 2029 Stierlin Ct
Mountain View, CA 94043-4655
United States map
[3] => +1.650.687.3600
[4] => Software & Internet, E-commerce and Internet Businesses
Software & Internet, Data Analytics, Management and Storage
Business Services, HR and Recruiting Services
[5] => 1K - 10K
[6] => > $1B
[7] => Publicly Traded - NASDAQ : LNKD
)
By the way foreach is not a loop but a language construct.
Traverse like this.
foreach($yourarr as $k=>$v)
{
echo "The Key:$k and The Value:$v<br>";
}

XPATH - Return whole object under one node when inner node namespaces differ

Another XPath question. I have an API to query which contains multiple nested namespaces.
<feed xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom" xmlns:pamedia="http://paimages.co.uk/pamedia.htm">
<title>
Image / video search results
</title>
<subtitle>
Images / video found containing the search string provided
</subtitle>
<pamedia:found>
2228
</pamedia:found>
<pamedia:perpage>
100
</pamedia:perpage>
<pamedia:page>
1
</pamedia:page>
<opensearch:totalResults>
2228
</opensearch:totalResults>
<opensearch:itemsPerpage>
100
</opensearch:itemsPerpage>
<opensearch:startIndex>
1
</opensearch:startIndex>
<id>
http://images.pressassociation.com/cgi/search_api/?state=search&q=miley
</id>
<link rel="self" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley"></link>
<link rel="next" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley&offset=2"></link>
<link rel="last" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley&offset=23"></link>
<updated>
2013-11-21T09:13:21Z
</updated>
<link rel="self" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley">
<updated>
2013-11-21T09:13:21Z
</updated>
<name>
Press Association Images
</name>
<email>
redacted
</email>
</link>
<entry>
<pamedia:media-type>
image/jpeg
</pamedia:media-type>
<pamedia:event_date>
2013-11-14
</pamedia:event_date>
<pamedia:urn>
18209006
</pamedia:urn>
<pamedia:domain>
2
</pamedia:domain>
<pamedia:domain_prefix>
PA
</pamedia:domain_prefix>
<link type="application/vnd.iptc.g2.newsitem+xml" href="http://images.pressassociation.com/meta/2.18209006.xml"></link>
<link rel="related" href="http://images.pressassociation.com/meta/2.18209006.html" type="text/html"></link>
<link rel="related" href="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg" type="image/jpeg"></link>
<media:thumbnail width="127" medium="image" height="190" url="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg" type="image/jpeg"></media:thumbnail>
<media:content expression="sample" medium="image" width="511" height="767" url="http://images.pressassociation.com/image/preview/2.18209006.jpg" type="image/jpeg"></media:content>
<media:copyright>
Associated Press
</media:copyright>
<media:content expression="full" medium="photo" width="6041" height="4024" url="http://images.pressassociation.com/image/2.18209006.jpg" type="image/jpeg"></media:content>
<updated>
2013-11-15T10:56:44Z
</updated>
<summary type="html">
Fans wait for singer Miley Cyrus before the Bambi 2013 media awards in Berlin, Germany, Thursday, Nov. 14, 2013. (AP Photo/Gero Breloer)
</summary>
<rights type="html">
UK picture buyers only BRO110
</rights>
<id>
http://images.pressassociation.com/meta/2.18209006.xml
</id>
<title type="html">
2013 Bambi Media Awards - Berlin
</title>
<category term="E"></category>
<author>
<name>
Gero Breloer/AP
</name>
</author>
</entry>
<entry>
<pamedia:media-type>
image/jpeg
</pamedia:media-type>
<pamedia:event_date>
2013-11-14
</pamedia:event_date>
<pamedia:urn>
18207923
</pamedia:urn>
<pamedia:domain>
2
</pamedia:domain>
<pamedia:domain_prefix>
PA
</pamedia:domain_prefix>
<link type="application/vnd.iptc.g2.newsitem+xml" href="http://images.pressassociation.com/meta/2.18207923.xml"></link>
<link rel="related" href="http://images.pressassociation.com/meta/2.18207923.html" type="text/html"></link>
<link rel="related" href="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18207923.jpg" type="image/jpeg"></link>
<media:thumbnail width="127" medium="image" height="198" url="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18207923.jpg" type="image/jpeg"></media:thumbnail>
<media:content expression="sample" medium="image" width="511" height="796" url="http://images.pressassociation.com/image/preview/2.18207923.jpg" type="image/jpeg"></media:content>
<media:copyright>
Associated Press
</media:copyright>
<media:content expression="full" medium="photo" width="2801" height="1796" url="http://images.pressassociation.com/image/2.18207923.jpg" type="image/jpeg"></media:content>
<updated>
2013-11-15T10:56:44Z
</updated>
<summary type="html">
Singer Miley Cyrus arrives for the Bambi 2013 media awards in Berlin, Germany, Thursday, Nov. 14, 2013. (AP Photo/Gero Breloer)
</summary>
<rights type="html">
UK picture buyers only BRO105
</rights>
<id>
http://images.pressassociation.com/meta/2.18207923.xml
</id>
<title type="html">
2013 Bambi Media Awards - Berlin
</title>
<category term="E"></category>
<author>
<name>
Gero Breloer/AP
</name>
</author>
</entry>
In order to get the media: namespace nodes and the rest of the entry nodes I am using this query: '/namespace:feed/namespace:entry | namespace:entry/media:*' However doing this returns the media nodes as seperate array items:
rray
(
[0] => SimpleXMLElement Object
(
[link] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://images.pressassociation.com/meta/2.18209006.xml
[type] => application/vnd.iptc.g2.newsitem+xml
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://images.pressassociation.com/meta/2.18209006.html
[rel] => related
[type] => text/html
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg
[rel] => related
[type] => image/jpeg
)
)
)
[updated] => 2013-11-15T10:56:44Z
[summary] => Fans wait for singer Miley Cyrus before the Bambi 2013 media awards in Berlin, Germany, Thursday, Nov. 14, 2013. (AP Photo/Gero Breloer)
[rights] => UK picture buyers only BRO110
[id] => http://images.pressassociation.com/meta/2.18209006.xml
[title] => 2013 Bambi Media Awards - Berlin
[category] => SimpleXMLElement Object
(
[#attributes] => Array
(
[term] => E
)
)
[author] => SimpleXMLElement Object
(
[name] => Gero Breloer/AP
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg
[medium] => image
[type] => image/jpeg
[width] => 127
[height] => 190
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://images.pressassociation.com/image/preview/2.18209006.jpg
[medium] => image
[type] => image/jpeg
[expression] => sample
[width] => 511
[height] => 767
)
)
[3] => SimpleXMLElement Object
(
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://images.pressassociation.com/image/2.18209006.jpg
[medium] => photo
[type] => image/jpeg
[expression] => full
[width] => 6041
[height] => 4024
)
)
I however require [1] [2] [3] & [4] to be nested withing the entry node [0] for extracting values. If anyone could help with this issue I would be very grateful. If possible I would like to be able to return it all with one XPath call.
To get entry nodes use only the first part of your expression:
/atom:feed/atom:entry
To get all entry nodes that have a media descendant node, you could use this expression:
/atom:feed/atom:entry[.//media:*]
After you get the entry nodes, you wanted use another expression and provide the context in the second argument of query()/evaluate().
.//media:*
Be aware that you have to register and use your own namespaces prefixes on the SimpleXml/DOMXpath instance.
I suggest using DOMXpath::evaluate() and not SimpleXmlElement::xpath(). Unlike xpath(), evaluate can return scalar values directly. Expressions like string(.//media:copyright) only work with evaluate(), not with xpath() or DOMXpath::query().
Here is a small example using DOM+Xpath to query an Atom feed with MediaRss:
$dom = new DOMDocument();
$dom->load('feed.xml');
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
foreach ($xpath->evaluate('/atom:feed/atom:entry[.//media:thumbnail]', NULL, FALSE) as $entry) {
$href = $xpath->evaluate('string(atom:link[#rel = "related" and #type="text/html"]/#href)', $entry, FALSE);
$src = $xpath->evaluate('string(.//media:thumbnail/#url)', $entry, FALSE);
printf('<img src="%s"/>', $href, $src);
}

Retrieving BIO information in LastFM API and PHP

I'm trying to look up the artist BIO information with the LASTFM api but I'm having trouble.
I'm using PHP and a CURL request that's returning the result as XML.
If I use the url that gets sent by CURL and just load it normally in the browser it show's everything as desired. However, the result returned by CURL in the php script is missing certain properties of the XML.
For example: artist->bio->summary is there but with no content.
You can replicate what I'm doing ill copy the basic concepts here:
open this in you browser:
http://ws.audioscrobbler.com/2.0/artist/cher/info.xml
copy and paste this into a PHP file and open:
$url = 'http://ws.audioscrobbler.com/2.0/artist/cher/info.xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$retData = curl_exec($ch);
curl_close($ch);
echo '<pre>';
echo $retData;
echo '</pre>';
You'll see that the returned results are similar but with missing content in the curl request. I've also tried doing this a simplexml_load_file() but the same happens. What am I missing? Cheers for any help!
Try to replace
echo $retData;
with
echo htmlspecialchars($retData);
and you'll see that everything is there. You're printing raw XML in an HTML context, and some of the tags in the XML are interpreted as HTML tags.
The url is wrong but that could be a typo ;p. The reason its content is missing is the <![CDATA[ tag, strip it before parsing and you should be good to go.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/2.0/artist/cher/info.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$retData = curl_exec($ch);
$retData = str_replace(array('<![CDATA[',']]>'),'',$retData);
$xml = simplexml_load_string($retData);
$lastFM = json_decode(json_encode($xml),TRUE);
print_r($lastFM);
/*Array
(
[name] => Cher
[mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818
[url] => http://www.last.fm/music/Cher
[image] => Array
(
[0] => http://userserve-ak.last.fm/serve/34/63530405.png
[1] => http://userserve-ak.last.fm/serve/64/63530405.png
[2] => http://userserve-ak.last.fm/serve/126/63530405.png
[3] => http://userserve-ak.last.fm/serve/252/63530405.png
[4] => http://userserve-ak.last.fm/serve/500/63530405/Cher.png
)
[streamable] => 1
[stats] => Array
(
[listeners] => 768175
[playcount] => 7895613
)
[similar] => Array
(
[artist] => Array
(
[0] => Array
(
[name] => Sonny & Cher
[url] => http://www.last.fm/music/Sonny+&+Cher
[image] => Array
(
[0] => http://userserve-ak.last.fm/serve/34/71168880.png
[1] => http://userserve-ak.last.fm/serve/64/71168880.png
[2] => http://userserve-ak.last.fm/serve/126/71168880.png
[3] => http://userserve-ak.last.fm/serve/252/71168880.png
[4] => http://userserve-ak.last.fm/serve/500/71168880/Sonny++Cher.png
)
)
[1] => Array
(
[name] => Cyndi Lauper
[url] => http://www.last.fm/music/Cyndi+Lauper
[image] => Array
(
[0] => http://userserve-ak.last.fm/serve/34/76056286.png
[1] => http://userserve-ak.last.fm/serve/64/76056286.png
[2] => http://userserve-ak.last.fm/serve/126/76056286.png
[3] => http://userserve-ak.last.fm/serve/252/76056286.png
[4] => http://userserve-ak.last.fm/serve/_/76056286/Cyndi+Lauper++for+Uniqlo.png
)
)
[2] => Array
(
[name] => Madonna
[url] => http://www.last.fm/music/Madonna
[image] => Array
(
[0] => http://userserve-ak.last.fm/serve/34/78270650.png
[1] => http://userserve-ak.last.fm/serve/64/78270650.png
[2] => http://userserve-ak.last.fm/serve/126/78270650.png
[3] => http://userserve-ak.last.fm/serve/252/78270650.png
[4] => http://userserve-ak.last.fm/serve/_/78270650/Madonna+Shes+a+bad+girl.png
)
)
[3] => Array
(
[name] => Kylie Minogue
[url] => http://www.last.fm/music/Kylie+Minogue
[image] => Array
(
[0] => http://userserve-ak.last.fm/serve/34/78217522.png
[1] => http://userserve-ak.last.fm/serve/64/78217522.png
[2] => http://userserve-ak.last.fm/serve/126/78217522.png
[3] => http://userserve-ak.last.fm/serve/252/78217522.png
[4] => http://userserve-ak.last.fm/serve/500/78217522/Kylie+Minogue+KylieBestOf.png
)
)
[4] => Array
(
[name] => Tina Turner
[url] => http://www.last.fm/music/Tina+Turner
[image] => Array
(
[0] => http://userserve-ak.last.fm/serve/34/74998404.jpg
[1] => http://userserve-ak.last.fm/serve/64/74998404.jpg
[2] => http://userserve-ak.last.fm/serve/126/74998404.jpg
[3] => http://userserve-ak.last.fm/serve/252/74998404.jpg
[4] => http://userserve-ak.last.fm/serve/_/74998404/Tina+Turner+Queen+3.jpg
)
)
)
)
[tags] => Array
(
[tag] => Array
(
[0] => Array
(
[name] => pop
[url] => http://www.last.fm/tag/pop
)
[1] => Array
(
[name] => female vocalists
[url] => http://www.last.fm/tag/female%20vocalists
)
[2] => Array
(
[name] => 80s
[url] => http://www.last.fm/tag/80s
)
[3] => Array
(
[name] => dance
[url] => http://www.last.fm/tag/dance
)
[4] => Array
(
[name] => rock
[url] => http://www.last.fm/tag/rock
)
)
)
[bio] => Array
(
[published] => Tue, 1 May 2012 12:51:56 +0000
[summary] => Cher (born Cherilyn Sarkisian on May 20, 1946) is an American singer, actress, songwriter, author and entertainer. Among her many career accomplishments in music, television and film, she has won an Academy Award, a Grammy Award, an Emmy Award and three Golden Globe Awards among others. Referred to as the Goddess of Pop, Cher first rose to prominence in 1965 as one half of the pop/rock duo Sonny & Cher.
[content] => Cher (born Cherilyn Sarkisian on May 20, 1946) is an American singer, actress, songwriter, author and entertainer. Among her many career accomplishments in music, television and film, she has won an Academy Award, a Grammy Award, an Emmy Award and three Golden Globe Awards among others.
Referred to as the Goddess of Pop, Cher first rose to prominence in 1965 as one half of the pop/rock duo Sonny & Cher. She also established herself as a solo recording artist, releasing 25 albums, contributing to numerous compilations, and tallying 34 Billboard Top 40 entries over her career, both solo and with Sonny. These include eighteen Top 10 singles and five number one singles (four solo). Cher has had 16 top ten hits in the UK between 1965 and 2003, four of which reached number one (two solo, one with Sonny, one as part of a charity single).
In a career surpassing 40 years, Cher has been described as an enduring pop icon and one of the most popular female artists in music history. Since her debut in 1964, Cher has sold as a solo artist over 180 million records worldwide and an estimated 70 million singles and with duo Sonny and Cher over 80 million records becoming one of the biggest-selling artists of all time.
She became a television star in the 1970s and a film actress in the 1980s. In 1987, she won the Academy Award for Best Actress for her role in the romantic comedy Moonstruck.
During 2007, in the seventh volume of Chrome Hearts, Cher once again confirmed that she is working on her twenty-sixth studio album. No recordings have taken place as of yet, and no release date had been scheduled.
It is rumoured that Cher will collaborate with Lady Gaga in a song for her next album Born This Way. This album is expected to be released in Spring 2011.
On February 7, 2008 Cher, at 61, announced that she had reached a deal to perform 200 shows over three years live at the Colosseum at Caesar's Palace in Las Vegas. Her new show, entitled Cher at the Colosseum, debuted on May 6, 2008. The show reportedly includes 18 dancers, 4 aerialists, and multiple costumes designed by Bob Mackie. Choreography will be directed by Doriana Sanchez who also worked with Cher on her past three major tours. Her show will perform four nights a week for a month, and a second leg of the show will continue in August. Cher will share the stage on a rotating basis with contemporaries Bette Midler (whose The Showgirl Must Go On opened on February 20, 2008) and Elton John (whose The Red Piano which opened in 2004, will continues its run of about 50 shows a year).
Cher made her musical debut and first film role in a decade in , starring alongside Christina Aguilera, who also made her musical and film debut. The film was released in November 2010.
User-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.
)
)
*/

Get a query from url in php

I don't know how to do this in php. I am trying to get geocode data from openstreetmap.org but I need to do this in php.
In Javascript (and I have this working), it's:
<script type="text/javascript"
src="http://nominatim.openstreetmap.org/reverse?format=xml&lat=43.642018&lon=-79.374671">
</script>
I have that url but I don't know how to execute it in php.
I have tried:
<?php
$url = "http://nominatim.openstreetmap.org/reverse?format=xml&lat=43.642018&lon=-79.374671";
$response = file_get_contents($url);
?>
But this doesn't work. I tried the same with curl. Any ideas or is this type of query not possible in php?
Use this;
$xml = simplexml_load_file("http://nominatim.openstreetmap.org/reverse?format=xml&lat=43.642018&lon=-79.374671");
print_r($xml);
which will return you with an array of the values, see below;
SimpleXMLElement Object
(
[#attributes] => Array
(
[timestamp] => Thu, 22 Mar 12 18:31:11 +0000
[attribution] => Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.
[querystring] => format=xml&lat=43.642018&lon=-79.374671
)
[result] => 1, Yonge Street, Corktown, Toronto, Ontario, M5B2H1, Canada
[addressparts] => SimpleXMLElement Object
(
[house_number] => 1
[road] => Yonge Street
[suburb] => Corktown
[city] => Toronto
[county] => Toronto
[state] => Ontario
[postcode] => M5B2H1
[country] => Canada
[country_code] => ca
)
)
Then manipulate the data however you see fit.
It does return
<?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Thu, 22 Mar 12 18:07:13 +0000' attribution='Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.' querystring='format=xml&lat=100&Lon=100'>
<error>Unable to geocode</error></reversegeocode>
That just means that there is no address for that particular coordinate.. This however has
http://nominatim.openstreetmap.org/reverse?format=xml&lat=52.5487429714954&lon=100
Read API if you need more details

Categories