This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
php simplexml issue in reading an attribute that has a ‘column - : ’ in its name
I'm in the process of learning PHP, so I'm making a weather app to display an image based on the weather. I'm using the Yahoo Weather API.
My problem is that I'm looking for a way to grab the code of the weather, located next to yweather:condition . I found simpleXML but can't figure out how to grab something that's located in something like the way Yahoo does its xml in the form of
<yweather:condition text="Fair" code="34" temp="37"
So my question is how can I grab the code="34" portion of the Yahoo weather and display it as a variable like $weathercode = 34;?
Thanks for any and all help, and I'm here to give more details if you need them!
Also, I'm not sure if the title of this post is correct, so sorry if it's not!
You can use the SimpleXMLElement::attributes call in a similar way to below.
$string = {Yahoo Weather Feed}
$xml = simplexml_load_string($string);
$weathercode = $xml->channel->item->children('yweather', true)->condition[0]->attributes()->code;
You may need to tinker to get it right, but this is just a guide.
Reference: http://php.net/manual/en/simplexmlelement.attributes.php
You could try the following:
$xml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w=12793465&u=f');
var_dump($xml);
This will load the URL and dump the SimpleXML object.
More on simplexml_load_file here
Related
This question already has answers here:
StaleElementReference Exception in PageFactory
(3 answers)
Closed 3 years ago.
Good afternoon,
I am trying to open several links within a foreach using Facebook WebDriver, but it only opens the first one.
The links are arriving correctly, string, but on the second link it hangs and displays the error below.
It seems to me that selenium drive has a cookie or cache problem.
foreach($subjectsList as $subject)
{
// Get all info subjects each course.
$subjectTitle = $subject->getText();
$subjectLink = trim($subject->getAttribute('href'));
$rawPage = $this->seleniumDriver->get($subjectLink);
} // end Foreach for subjectsLis
Thanks in advance guys, any help is very welcome.
It's not a cookie or a cache problem, it's conceptual.
$rawPage = $this->seleniumDriver->get($subjectLink); performs browser navigation. So, your code will correctly parse out the first link, and navigate to it. But, when it tries the second link, selenium correctly identifies that $subject is essentially a dangling pointer (well, it contains a C pointer which is technically the dangling pointer, but...) to a DOM element in the previous (deallocated) webpage/dom-tree.
To do what you're looking for, first parse out the titles/hrefs to strings, then iterate over the strings.
I'd take a look at the relevant documentation
This question already has answers here:
get json using php
(3 answers)
Closed 9 years ago.
When you're visiting this page:
https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns
...I guess what is called an object, appears with a lot of information. But when I try to do a simple GET call and then print it, like so:
$tweets = $_GET['https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns'];
print_r($tweets);
...It returns nothing... Why is that?
The $_GET super global is populated with the query string of the current request, it's not used to get stuff from the interwebs.
You're looking for file_get_contents():
$url = 'https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns';
$tweets = file_get_contents($url);
print_r($tweets);
If that contains a JSON encoded response you need to additionally use json_decode() to use it.
Do you know how to use $_GET?
Check PHP documentation, you must have made a mistake, or you're using it the wrong way.
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have to create a webservice which goes to a specific URL that returns a XML-file as response and interprets/parses this file in order to save its contents to a MySQL database.
I've heard about the SimpleXML but I'm not sure how to get the websites response into a file whose path is needed in order to parse the document.
Can somebody at least explain me how to reach the goal of downloading the XML and saving it to a file? (best with some PHP code)
I will then (hopefully) find out by myself how to parse it and store its contents.
Here's an example of what my XML will look like (for privacy reasons I can't publish the real URL I'm using...)
Here's a couple of pointers..
To download a file and save it, the easiest way I have found is this:
<?php
file_put_contents('saved.xml', file_get_contents('http://www.xmlfiles.com/examples/simple.xml'));
You can then open the file with the simpleXML library like so:
$xml = simplexml_load_file('saved.xml');
var_dump($xml);
Hope that gives you enough info to get started.
See simpleXML for info on the simpleXML library.
You can download and save the xml to a local file by doing this:
$xmlstring = file_get_contents("http://domain.com/webservice/xmlfile.xml");
file_put_contents("path/localxmlfile.xml", $xmlstring);
To parse the xml file I suggest you to use DOMDocument class in combination with the DOMXPath class to query/search for specific elements.
DOMDocument: http://php.net/manual/de/class.domdocument.php
DOMXPath: http://php.net/manual/de/class.domxpath.php
Hopefully you can find your answer on below link. Seems related topic.
How do you parse and process HTML/XML in PHP?
This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I want to find country, city, latitude and longitude from IP address using php. I am using this url and it is returning data in xml format.
http://www.ipgp.net/api/xml/122.163.6.58
The data is coming like this:
<IpLookup>
<Ip>122.163.6.58</Ip>
<Code>IN</Code>
<Country>India</Country>
<Flag>http://www.ipgp.net/flags/in.png</Flag>
<City>Calcutta</City>
<Region>West Bengal</Region>
<Isp></Isp>
<Lat>22.5697</Lat>
<Lng>88.3697</Lng>
</IpLookup>
Can anybody suggest how to parse and get the result
Use the XML parser included in PHP?
Use simplexml_load_string().
I've been using this personally:
http://ipinfodb.com/
The examples are very clear and concise and the API is very fast.
Good luck.
The use of API in PHP has already described in their website. Why you use but don't read?
http://www.ipgp.net/developer-tools/
I'll suggest you to use xpath this is more easier schema for accessing the attributes.
for example for your current data i have the following:
$file = 'file.xml';
$xml = new SimpleXMLElement($file, NULL, TRUE);
$ipz = $xml->xpath("/IpLookup/Ip");
$country = $xml->xpath("/IpLookup/Country/");
foreach($ipz as $ip)
{
foreach($country as $country)
{
echo $ip.'<br/>';
echo $country.'<br/>';
}
}
this code will return you the ip and the country for your current xml. you can edit it in your own way.