Skip to content

Instantly share code, notes, and snippets.

@Ibochkarev
Created September 4, 2015 06:38
Show Gist options
  • Save Ibochkarev/70f5b17ad1bc67b573ad to your computer and use it in GitHub Desktop.
Save Ibochkarev/70f5b17ad1bc67b573ad to your computer and use it in GitHub Desktop.
Openweathermap weather city
<?php
//q = Name city
$curl = curl_init("http://api.openweathermap.org/data/2.5/weather?q=calp&units=metric&mode=xml&lang=ru");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
$buffer = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($buffer);
$city = $xml->city[name];
$temperarure = $xml->temperature[value];
$weather_id = $xml->weather[number];
$weather_icon = $xml->weather[icon];
$weather_type = $xml->weather[value];
$temperarure = round($temperarure);
if ($temperarure>0) {$temperarure='+'.$temperarure;}
echo "<strong>$city</strong>";
echo ("&nbsp; <img src=\"/assets/images/weather/$weather_icon.svg\" height=\"35\" alt=\"$weather_id\" title=\"$weather_type\" >&nbsp;<span>$temperarure<sup>o</sup>C</span>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment