【发布时间】:2017-11-05 10:15:48
【问题描述】:
<?php
echo "<h1>What's The Weather Like?</h1>";
define('API_KEY','********');
$apikey = API_KEY;
$user_ip = '183.91.3.13'; // Remove comment to use this to test when you put
this file in localhost
//$user_ip = $_SERVER['REMOTE_ADDR']; // Comment out this line if you test
in your localhost
$details =
json_decode(file_get_contents("http://ipinfo.io/{$user_ip}/json"));
$city_name = $details->city;
$loc_details =
json_decode(file_get_contents("http://dataservice.accuweather.com/locations/v1/ cities/search?q={$city_name}&apikey={$apikey}"));
$loc_key = $loc_details[0]->Key;
$weather_details =
json_decode(file_get_contents("http://dataservice.accuweather.com/forecasts/v1/daily/1day/{$loc_key}?apikey={$apikey}"));
// print_r($weather_details);
// $result = json_decode($data, true);
echo $weather_details[0]->EffectiveDate;
?>
当我尝试运行网络时,它显示了这个问题 致命错误:不能在第 16 行的 C:\xampp\htdocs\aiw\index.php 中使用 stdClass 类型的对象作为数组 我希望你不要不喜欢,因为我是一个新的学习者
【问题讨论】:
-
提供您获得的 JSON 输出示例。此外,公开您的 API 密钥通常是个坏主意,请用空字符串或星号行替换它