Пример #1
0
/**
 * Convert a string of xml to an array
 * @author Nate White
 * @param string $xml
 * @return mixed array if success; else null
 */
function xmlstring2array($xml)
{
    //$ReElements = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
    $ReElements = '/<(\\w+)\\s*([^\\/>]*)\\s*(?:\\/>|>(.*?)<(\\/\\s*\\1\\s*)>)/s';
    $ReAttributes = '/(\\w+)=(?:"|\')([^"\']*)(:?"|\')/';
    preg_match_all($ReElements, $xml, $elements);
    foreach ($elements[1] as $ie => $xx) {
        $xmlary[$ie]["name"] = $elements[1][$ie];
        if ($attributes = trim($elements[2][$ie])) {
            preg_match_all($ReAttributes, $attributes, $att);
            foreach ($att[1] as $ia => $xx) {
                // all the attributes for current element are added here
                $xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
            }
        }
        // if $attributes
        // get text if it's combined with sub elements
        $cdend = strpos($elements[3][$ie], "<");
        if ($cdend > 0) {
            $xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);
        }
        // if cdend
        if (preg_match($ReElements, $elements[3][$ie])) {
            $xmlary[$ie]["elements"] = xmlstring2array($elements[3][$ie]);
        } else {
            if (isset($elements[3][$ie])) {
                $xmlary[$ie]["text"] = $elements[3][$ie];
            }
        }
    }
    if (!empty($xmlary)) {
        return $xmlary;
    }
}
Пример #2
0
 /**
  * Получение параметров погоды из сервиса.
  * http://api.openweathermap.org
  * CITYID = 498817
  * APIID = a61d94e24a6ea5c6adca71cd69256c39 (juicylevel, epictrain)
  */
 private function getWeatherFromService()
 {
     try {
         $serviceUrl = "http://api.openweathermap.org/data/2.5/weather?id=498817&APPID=a61d94e24a6ea5c6adca71cd69256c39&lang=ru&mode=xml&units=metric";
         $response = sendRequest($serviceUrl);
         $serviceData = xmlstring2array($response);
         $weather = array('temperature' => $serviceData['temperature']['@attributes']['value'], 'humidity' => $serviceData['humidity']['@attributes']['value'], 'pressure' => round($serviceData['pressure']['@attributes']['value'] * 0.75006375541921), 'wind_speed' => $serviceData['wind']['speed']['@attributes']['value'], 'wind_direction' => Settings::getInstance()->getWindDirection($serviceData['wind']['direction']['@attributes']['code'])['code'], 'weather_condition' => Settings::getInstance()->getWeatherCondition(substr($serviceData['weather']['@attributes']['icon'], 0, -1))['code']);
     } catch (Exception $exception) {
         $weather = array();
     }
     return $weather;
 }