我需要一些我创建的PHP类的帮助,现在我想扩展它以使用天气预报服务。
只需两个字来解释我想要实现的目标:使用Google API我想验证第一个用户在我的某个字段中编写的城市,并且然后我想向他展示,使用其他API,当前的天气以及他所在城市的3天预测。
我写了一个完美的课程,因为它连接到Google API并且由于JSON文件,我得到了我需要的所有信息(这里是国家和城市),然后我写了一个类似于连接到Wunderground API(免费的优秀网络服务)和JSON文件,我可以得到我想要的所有天气数据。
问题在于我想将我从第一堂课(我称之为GeoLocalization班)的国家和城市传递到第二堂课(GeoWeather班),以便显示我的用户选择的城市的天气预报
这是我的第一堂课:
<?php
class GeoLocalization
{
public $host = 'http://maps.google.com/maps/api/geocode/json?address={ADDRESS}&sensor=false';
public $street_number;
public $route;
public $locality;
public $county;
public $state_long;
public $state_short;
public $country_long;
public $country_short;
public $postal_code;
public $formatted_address;
public $latitude;
public $longitude;
public $status;
public function __construct($address)
{
$host = str_replace('{ADDRESS}', $address, $this->host);
$response = $this->fetch($host);
// decode the JSON into an associative array, setting the option true
$data = json_decode($response, true);
# This will print out the contents of the array in a nice readable format.
# echo '<pre>' . print_r($data, true) . '</pre>';
// set the properties
$this->street_number = $data['results'][0]['address_components'][0]['long_name'];
$this->route = $data['results'][0]['address_components'][1]['long_name'];
$this->locality = $data['results'][0]['address_components'][2]['long_name'];
$this->county = $data['results'][0]['address_components'][3]['long_name'];
$this->state_long = $data['results'][0]['address_components'][4]['long_name'];
$this->state_short = $data['results'][0]['address_components'][4]['short_name'];
$this->country_long = $data['results'][0]['address_components'][5]['long_name'];
$this->country_short = $data['results'][0]['address_components'][5]['short_name'];
$this->postal_code = $data['results'][0]['address_components'][6]['long_name'];
$this->formatted_address = $data['results'][0]['formatted_address'];
$this->latitude = $data['results'][0]['geometry']['location']['lat'];
$this->longitude = $data['results'][0]['geometry']['location']['lng'];
$this->status = $data['status'];
}
public function fetch($host)
{
if ( function_exists('curl_init') )
{
// use cURL to fetch data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
// return the transfer instead of outputting it out directly.
// if deactivated (with 0 instead of 1) you see the json from the API
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// execute
$response = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close ($ch);
}
else if ( ini_get('allow_url_fopen') )
{
//fall back to fopen()
$response = file_get_contents($host, 'r');
}
return $response;
}
}
这是我的第二个课程,扩展了父课程。
<?php
class GeoWeather extends GeoLocalization
{
public $api = 'http://api.wunderground.com/api/MY_API_KEY/geolookup/conditions/forecast/lang:IT/q/{COUNTRY}/{CITY}.json';
public $location;
public $current_weather_image;
public $current_weather_temperature;
public function __construct()
{
$needle = array('{COUNTRY}', '{CITY}');
$replace = array($this->state_long, $this->locality);
# replace the country and city
$api = str_replace($needle, $replace, $this->api);
$response = $this->get($api);
// decode the JSON into an associative array, setting the option true
$json = json_decode($response, true);
// set the properties
$this->location = $json['current_observation']['display_location']['full'];
$this->current_weather_image = $json['current_observation']['icon_url'];
$this->current_weather_temperature = $json['current_observation']['temp_c'] . ' °C' . ' (' . $json['current_observation']['temp_f'] . ' °F' . ')';
// access the parent constructor
parent::__construct($address);
}
public function get($api)
{
// control if the API in the other class answered positive
// if so, go on with this one.
if ( $this->status && function_exists('curl_init') )
{
// use cURL to get data
$ch = curl_init();
// set the URL
curl_setopt($ch, CURLOPT_URL, $api);
// return the transfer instead of outputting it out directly.
// if deactivated (with 0 instead of 1) you see the json from the API
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// execute
$response = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close ($ch);
}
else if ( ini_get('allow_url_fopen') )
{
//fall back to fopen()
$response = file_get_contents($api, 'r');
}
return $response;
}
}
我在另一个名为instance.php的文件中使用它们,这样:
<?php
require_once 'GeoLocalization.class.php';
$address = 'Roma';
$address = urlencode($address);
$map = new GeoLocalization($address);
if ( $map->status == 'OK' )
{
echo $map->locality . '<br />';
echo $map->state_long . '<br />';
}
else
{
echo 'address not found';
}
require_once 'GeoWeather.class.php';
$weather = new GeoWeather();
echo $weather->location . '<br />';
echo '<img src="' . $weather->current_weather_image . '" alt="" />' . '<br />';
echo $weather->current_weather_temperature . '<br />';
?>
我在本地看到的错误是在未定义的变量:address。然后我收到一个类似的级联错误:未定义的索引:current_observation等。
希望有人可以帮助我。
感谢。