1

As you can see I've checked some other answers here on Stackoverflow but I only get blank results. Here's what I've got so far:

$city = simplexml_load_file('http://api.openweathermap.org/data/2.5/weather?q=Tijuana&mode=xml&appid=12fcd235af3a27a895aecb26bc957055');

echo $city['current']['city']['name'];
echo $city->current->city;

Any help is greatly appreciated.

4
  • 1
    Try this echo (string)$city->city["name"]; simplexml_load_string or file ignore root element, which is current in your case Commented Aug 26, 2017 at 7:09
  • One last question: Sahil, I can get the result for the first pocket but not the second... for example: <?php $base_clima = simplexml_load_file('api.openweathermap.org/data/2.5/…); // print_r($city); $humedad = (string)$base_clima->humidity["value"]; $wind_v = (string)$base_clima->wind["speed"]['value']; ?> <p><?='Humidity: '.$humedad.'%'?></p> <p><?='Wind speed: '. $wind_v .'Km/h'?></p> Commented Aug 26, 2017 at 7:24
  • I dont understand which second pocket ? Commented Aug 26, 2017 at 7:26
  • Sorry... Object. If I want to print the first part of the array inside of the wind objet I get a blank result. wind["speed"]->speed["value"] Commented Aug 26, 2017 at 7:32

1 Answer 1

1

In your code change this:

$base_clima->wind["speed"]['value'];

This:

$base_clima->wind->speed['value'];

Sample print of an object:

[wind] => SimpleXMLElement Object
         (
            [speed] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [value] => 1.95
                            [name] => Light breeze
                        )

                )
         )

For accessing the elements of an object You can use something like this $object->element1->element2 but in simplexml_load_string/file @attributes can be accessed as an array so you can use it like this.

Try this, Hope this one will be helpful.

$city = simplexml_load_file('http://api.openweathermap.org/data/2.5/weather?q=Tijuana&mode=xml&appid=12fcd235af3a27a895aecb26bc957055');
echo $humedad = (string)$city->humidity["value"]; 
echo $wind_v = (string)$city->wind->speed['value'];
Sign up to request clarification or add additional context in comments.

1 Comment

You are a Genus! Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.