Your IP : 216.73.216.53


Current Path : /home/ahrcaeuege/www/modules/mod_jayahoo_weather/
Upload File :
Current File : /home/ahrcaeuege/www/modules/mod_jayahoo_weather/yql.php

<?php
/**
 * ------------------------------------------------------------------------
 * JA Yahoo Weather
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2018 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - GNU/GPL, http://www.gnu.org/licenses/gpl.html
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites: http://www.joomlart.com - http://www.joomlancers.com
 * ------------------------------------------------------------------------
 */
class jaYQLWeather
{
	protected $endpoint = 'https://query.yahooapis.com/v1/public/yql';

	public function __construct() {

	}

    function buildBaseString($baseURI, $method, $params)
    {
        $r = array();
        ksort($params);
        foreach($params as $key => $value) {
            $r[] = "$key=" . rawurlencode($value);
        }
        return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
    }
    function buildAuthorizationHeader($oauth)
    {
        $r = 'Authorization: OAuth ';
        $values = array();
        foreach($oauth as $key=>$value) {
            $values[] = "$key=\"" . rawurlencode($value) . "\"";
        }
        $r .= implode(', ', $values);
        return $r;
    }
    function getData($key=[], $search)
    {
        $url = 'https://weather-ydn-yql.media.yahoo.com/forecastrss';
        $app_id = $key['appid'];
        $consumer_key = $key['consumer_key'];
        $consumer_secret = $key['consumer_secret'];
        
        $query = array(
            'format' => 'json',
        );
        $query = array_merge($query,$search);

        $oauth = array(
            'oauth_consumer_key' => $consumer_key,
            'oauth_nonce' => uniqid(mt_rand(1, 1000)),
            'oauth_signature_method' => 'HMAC-SHA1',
            'oauth_timestamp' => time(),
            'oauth_version' => '1.0'
        );
        $base_info = $this->buildBaseString($url, 'GET', array_merge($query, $oauth));
        $composite_key = rawurlencode($consumer_secret) . '&';
        $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
        $oauth['oauth_signature'] = $oauth_signature;
        $header = array(
            $this->buildAuthorizationHeader($oauth),
            'Yahoo-App-Id: ' . $app_id
        );

        $options = array(
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_HEADER => false,
            CURLOPT_URL => $url . '?' . http_build_query($query),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false
        );
        $ch = curl_init();
        curl_setopt_array($ch, $options);
        $response = curl_exec($ch);
        curl_close($ch);
        $return_data = json_decode($response);

        if ($return_data === NULL) {
            return $response;
        } else
            return $return_data;
    }


	public function getWeatherForecast($woeid) {
		$query = "select * from weather.forecast where woeid=".intval($woeid);
		$result = $this->getResult($query, false);

		$info = null;
		if(is_object($result) && isset($result->query->results->channel)) {
			$info = $result->query->results->channel;
		}
		return $info;
	}

	public function getGeoPlace($placename) {
		//(1) to return only first result
		$query = "select * from geo.places(1) where text = '".addslashes($placename)."'";
		$result = $this->getResult($query, false);

		$place = null;
		if(is_object($result) && isset($result->query->results->place)) {
			$place = $result->query->results->place;
			if(is_array($place)) {
				$place = $place[0];
			}
		}
		return $place;
	}

	public function getGeoStates($country) {
		$query = "select * from geo.states where place='".addslashes($country)."'";
		$result = $this->getResult($query, false);

		$items = array();
		if(is_object($result) && isset($result->query->results->place)) {
			$items = $result->query->results->place;
		}
		return $items;
	}

	protected function getResult($query, $alltables = true) {
		$url = new JUri($this->endpoint);
		$url->setVar('q', rawurlencode($query));
		$url->setVar('format', rawurlencode('json'));
		$url->setVar('diagnostics', rawurlencode('true'));
		if($alltables) {
			$url->setVar('env', rawurlencode('store://datatables.org/alltableswithkeys'));
		}
		$url->setVar('callback', '');
		//&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

		$params = new JRegistry();
		/*if(JHttpTransportCurl::isSupported()) {
			$transport = new JHttpTransportCurl($params);
		} else{
			$transport = new JHttpTransportSocket($params);
		}*/
		//use socket to fix issue: can not verify CA cert file

		try
		{
			$transport = new JHttpTransportSocket($params);
			$result = $transport->request('GET', $url);
			$body = json_decode($result->body);
			return $body;
		} catch (Exception $e) {
			$app = JFactory::getApplication();
			$app->enqueueMessage($e->getMessage());
			return false;
		}
	}
}