2016-12-02 51 views
0

我试图使用简单的天气API,但无论如何,它不会将它识别为对象,无论我做什么。这是我的代码:Json_decode引发“尝试获取非对象的属性”

$url = "https://www.amdoren.com/api/weather.php?api_key=za8LEJ8F9mcHK8SvLxdM98rM9mNFjW&lat=40.7127837&lon=-74.0059413"; 
$curl = curl_init($url); 
$curl_response = curl_exec($curl); 
$jsonobj = json_decode($curl_response); 
$msg = "Temperature in ".$city."will be: ". $jsonobj->forecast->max_c; 

,这是我想用$jsonojb->forecast->max_c到达数据:

{ 
"error" : 0, 
"error_message" : "-", 
"forecast":[ 
{"date":"2016-12-02", 
"avg_c":8, 
"min_c":5, 
"max_c":11, 
"avg_f":46, 
"min_f":41, 
"max_f":52, 
    (...) 

,但它不工作。我做错了什么人?

回答

2

预测是一个数组,所以你必须使用这样的:

$forecast = $jsonobj->forecast; 
$forecast[0]->max_c; 
1

你可以尝试这样的代码:

$url = "https://www.amdoren.com/api/weather.php?api_key=za8LEJ8F9mcHK8SvLxdM98rM9mNFjW&lat=40.7127837&lon=-74.0059413"; 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

$curl_response = curl_exec($curl); 
$jsonobj = json_decode($curl_response); 

$msg = "Temperature in ". $city . " will be: ". $jsonobj->forecast[0]->max_c; 
echo $msg; 

希望它能帮助!

1

“预测”是一个数组,你必须添加[0]来获得第一个,然后你将能够得到你的“max_c”。

此外,您的API不会给你这个城市。您必须使用Google地理编码API将lon,lat转换为城市名称。

$api_key = "za8LEJ8F9mcHK8SvLxdM98rM9mNFjW"; 

    $lon = -74.0059413; 
    $lat = 40.7127837; 


    $url = "https://www.amdoren.com/api/weather.php?api_key=".$api_key."&lat=".$lat."&lon=".$lon.""; 


    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $response = json_decode(curl_exec($ch), true); 


    $max_c = $response['forecast'][0]['max_c']; 

    // Your API doesn't return the city name. 
    $city = "City Name"; 
    $msg = "Temperature in ".$city."will be: ". $max_c; 
    echo $msg; 

要获得您的城市名称,您可以使用以下功能。您将需要更换API密钥。

// Geocode 
function geocode($lat,$lon){ 

    $details_url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lon."&key=YOUR_API_KEY"; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $details_url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $response = json_decode(curl_exec($ch), true); 

    // If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST 
    if ($response['status'] != 'OK') { 
    return null; 
    } 

    $formatted_address = $response['results'][0]['formatted_address']; 
    $geometry = $response['results'][0]['geometry']; 

    $longitude = $geometry['location']['lat']; 
    $latitude = $geometry['location']['lng']; 

    $array = array(
     'lat' => $geometry['location']['lng'], 
     'lon' => $geometry['location']['lat'], 
     'location_type' => $geometry['location_type'], 
     'formatted_address' => $formatted_address 
    ); 

    return $array; 

} 
+0

你的代码是解决了在这个意义上,它不会引发错误问题的唯一,但它仍然没有显示任何东西,这不是关于城市的名字,因为我已经付了护理但忘了提及(这是一个组合框,你只能选择像三个城市一样)。所以它就像纽约的温度将会是: - 就是这样。 – ProgrammingNewbie

+0

@ProgrammingNewbie当我尝试使用您的API密钥时,它给我一个超出限制的错误。这可能是问题所在。尝试做print_r($ response)并查看是否有错误。 – dontanios

+0

@ProgrammingNewbie只是再次尝试,您的API密钥是问题。看看这里:http://d.tanios.ca/sh/(相同的代码),它使用你的密钥时会给出0。 – dontanios

相关问题