2016-12-02 84 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

相關問題