2014-09-18 110 views
0

我剛開始學習PHP(我有一些溫和的Java EXP)傳遞變量在PHP

,但我不能弄清楚如何簡單地傳遞一個變量作爲自變量。

請參閱下面的修補地下api。我爲我需要傳遞的URL創建變量,但是當我將它放在第二行時,它似乎沒有讀取變量。 url是有問題的變量。當我直接將URL作爲參數放置時,它的工作原理沒有問題。但是,下面的代碼給出了一個錯誤。

<?php 
    $url = "http://api.wunderground.com/api/KEY/geolookup/conditions/q/IA/19115"; 
     $json_string = file_get_contents($url); 
     $parsed_json = json_decode($json_string); 
     $location = $parsed_json->{'location'}->{'city'}; 
     $temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; 
     echo "Current temperature in ${location} is: ${temp_f}\n"; 
    ?> 

錯誤:

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "geolookup": 1 
    , 
    "conditions": 1 
    } 
    } 
     , "location": { 
     "type":"CITY", 
     "country":"US", 
     "country_iso3166":"US", 
     "country_name":"USA", 
     "state":"PA", 
     "city":"Philadelphia", 
     "tz_short":"EDT", 
     "tz_long":"America/New_York", 
     "lat":"40.09180450", 
     "lon":"-75.04257202", 
     "zip":"19115", 
     "magic":"1", 
     "wmo":"99999", 
     "l":"https://stackoverflow.com/q/zmw:19115.1.99999", 
     "requesturl":"US/PA/Philadelphia.html", 
     "wuiurl":"http://www.wunderground.com/US/PA/Philadelphia.html", 
     "nearby_weather_stations": { 
     "airport": { 
     "station": [ 
     { "city":"Northeast Philadelphia", "state":"PA", "country":"US", "icao":"KPNE", "lat":"40.07889938", "lon":"-75.01335144" } 
     ,{ "city":"Philadelphia Wings Airport", "state":"PA", "country":"US", "icao":"KLOM", "lat":"40.13755417", "lon":"-75.26513672" } 
     ,{ "city":"Mount Holly", "state":"NJ", "country":"US", "icao":"KVAY", "lat":"39.94076157", "lon":"-74.84071350" } 
     ,{ "city":"Doylestown", "state":"PA", "country":"US", "icao":"KDYL", "lat":"40.33015823", "lon":"-75.12286377" } 
     ] 
     } 
     , 
     "pws": { 
     "station": [ 
     { 
     "neighborhood":"Beth Ayres/High Road", 
     "city":"Huntingdon Valley", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAHUNTI3", 
     "lat":40.115807, 
     "lon":-75.053764, 
     "distance_km":2, 
     "distance_mi":1 
     }, 
     { 
     "neighborhood":"Grant and Academy", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA26", 
     "lat":40.069057, 
     "lon":-75.006996, 
     "distance_km":3, 
     "distance_mi":2 
     }, 
     { 
     "neighborhood":"Bryn Athyn", 
     "city":"Huntingdon Valley", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAHUNTI4", 
     "lat":40.133362, 
     "lon":-75.060265, 
     "distance_km":4, 
     "distance_mi":2 
     }, 
     { 
     "neighborhood":"Rydal", 
     "city":"Jenkintown", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAJENKI3", 
     "lat":40.098648, 
     "lon":-75.100975, 
     "distance_km":5, 
     "distance_mi":3 
     }, 
     { 
     "neighborhood":"Philadelphia", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA37", 
     "lat":40.126778, 
     "lon":-74.996559, 
     "distance_km":5, 
     "distance_mi":3 
     }, 
     { 
     "neighborhood":"Chalfont", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA45", 
     "lat":40.076885, 
     "lon":-74.977242, 
     "distance_km":5, 
     "distance_mi":3 
     }, 
     { 
     "neighborhood":"Elkin\u0027s Park - Logan\u0027s Alley", 
     "city":"Jenkintown", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAJENKI4", 
     "lat":40.085697, 
     "lon":-75.126213, 
     "distance_km":7, 
     "distance_mi":4 
     }, 
     { 
     "neighborhood":"Willow Grove Weather Center", 
     "city":"Willow Grove", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAWILLO7", 
     "lat":40.142101, 
     "lon":-75.106400, 
     "distance_km":7, 
     "distance_mi":4 
     }, 
     { 
     "neighborhood":"NOS_PORTS Tacony-Palmyra Bridge, NJ", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"MTPBN4", 
     "lat":40.011940, 
     "lon":-75.042999, 
     "distance_km":8, 
     "distance_mi":5 
     }, 
     { 
     "neighborhood":"Cinnaminson Street", 
     "city":"Riverton", 
     "state":"NJ", 
     "country":"US", 
     "id":"KNJRIVER2", 
     "lat":40.010277, 
     "lon":-75.010132, 
     "distance_km":9, 
     "distance_mi":5 
     }, 
     { 
     "neighborhood":"Fitzwatertown", 
     "city":"Willow Grove", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAWILLO10", 
     "lat":40.149128, 
     "lon":-75.129265, 
     "distance_km":9, 
     "distance_mi":5 
     }, 
     { 
     "neighborhood":"Dresher Pa", 
     "city":"Dresher", 
     "state":"PA", 
     "country":"US", 
     "id":"KPADRESH3", 
     "lat":40.138088, 
     "lon":-75.150169, 
     "distance_km":10, 
     "distance_mi":6 
     }, 
     { 
     "neighborhood":"", 
     "city":"Palmyra", 
     "state":"NJ", 
     "country":"US", 
     "id":"KNJPALMY4", 
     "lat":40.000000, 
     "lon":-75.010002, 
     "distance_km":10, 
     "distance_mi":6 
     }, 
     { 
     "neighborhood":"North Hills", 
     "city":"North Hills", 
     "state":"PA", 
     "country":"US", 
     "id":"KPANORTH5", 
     "lat":40.116211, 
     "lon":-75.165329, 
     "distance_km":10, 
     "distance_mi":6 
     }, 
     { 
     "neighborhood":"", 
     "city":"Cinnaminson", 
     "state":"NJ", 
     "country":"US", 
     "id":"KNJCINNA2", 
     "lat":39.997898, 
     "lon":-75.008797, 
     "distance_km":10, 
     "distance_mi":6 
     }, 
     { 
     "neighborhood":"Croydon", 
     "city":"Croydon", 
     "state":"PA", 
     "country":"US", 
     "id":"KPACROYD1", 
     "lat":40.087749, 
     "lon":-74.903839, 
     "distance_km":11, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"Delran-Millside Heights", 
     "city":"Delran", 
     "state":"NJ", 
     "country":"US", 
     "id":"KNJDELRA2", 
     "lat":40.008129, 
     "lon":-74.955276, 
     "distance_km":11, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"oreland", 
     "city":"Oreland", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAORELA2", 
     "lat":40.119926, 
     "lon":-75.177963, 
     "distance_km":11, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"Weather Genius @ Old Wyndmoor", 
     "city":"Wyndmoor", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAWYNDM3", 
     "lat":40.078907, 
     "lon":-75.192093, 
     "distance_km":12, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"CWOP# AR939", 
     "city":"Penndel", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPENND1", 
     "lat":40.156097, 
     "lon":-74.917603, 
     "distance_km":12, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"Downtown Fort Washington", 
     "city":"Fort Washington", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAFORTW5", 
     "lat":40.147438, 
     "lon":-75.179253, 
     "distance_km":13, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"Church Rd, Cinnaminson", 
     "city":"Cinnaminson", 
     "state":"NJ", 
     "country":"US", 
     "id":"KNJCINNA1", 
     "lat":39.979568, 
     "lon":-74.991234, 
     "distance_km":13, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"West Mount Airy", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA32", 
     "lat":40.058418, 
     "lon":-75.192741, 
     "distance_km":13, 
     "distance_mi":7 
     }, 
     { 
     "neighborhood":"Mount Airy", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA54", 
     "lat":40.054298, 
     "lon":-75.191826, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"HorshamWeather.Com", 
     "city":"Horsham", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAHORSH1", 
     "lat":40.192310, 
     "lon":-75.129883, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Maple Glen", 
     "city":"Maple Glen", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAMAPLE4", 
     "lat":40.162884, 
     "lon":-75.170181, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Germantown", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA39", 
     "lat":40.023037, 
     "lon":-75.173729, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Port Richmond (JcWeather)", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA4", 
     "lat":39.980930, 
     "lon":-75.112869, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Germantown Ave. Chestnut Hill", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA11", 
     "lat":40.071766, 
     "lon":-75.201759, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Jenks Academy-Chestnut Hill", 
     "city":"Philadelphia", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAPHILA53", 
     "lat":40.074879, 
     "lon":-75.203316, 
     "distance_km":13, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Erdenheim", 
     "city":"Erdenheim", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAERDEN1", 
     "lat":40.089100, 
     "lon":-75.211998, 
     "distance_km":14, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"", 
     "city":"Richboro", 
     "state":"PA", 
     "country":"US", 
     "id":"KPARICHB2", 
     "lat":40.221783, 
     "lon":-75.021301, 
     "distance_km":14, 
     "distance_mi":8 
     }, 
     { 
     "neighborhood":"Ivyland, PA", 
     "city":"Warminster", 
     "state":"PA", 
     "country":"US", 
     "id":"KPAWARMI4", 
     "lat":40.222992, 
     "lon":-75.049301, 
     "distance_km":14, 
     "distance_mi":8 
     } 
     ] 
     } 
     } 
    } 
    , "current_observation": { 
     "image": { 
     "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png", 
     "title":"Weather Underground", 
     "link":"http://www.wunderground.com" 
     }, 
     "display_location": { 
     "full":"Philadelphia, PA", 
     "city":"Philadelphia", 
     "state":"PA", 
     "state_name":"Pennsylvania", 
     "country":"US", 
     "country_iso3166":"US", 
     "zip":"19115", 
     "magic":"1", 
     "wmo":"99999", 
     "latitude":"40.09180450", 
     "longitude":"-75.04257202", 
     "elevation":"46.00000000" 
     }, 
     "observation_location": { 
     "full":"Beth Ayres/High Road, Huntingdon Valley, Pennsylvania", 
     "city":"Beth Ayres/High Road, Huntingdon Valley", 
     "state":"Pennsylvania", 
     "country":"US", 
     "country_iso3166":"US", 
     "latitude":"40.115807", 
     "longitude":"-75.053764", 
     "elevation":"250 ft" 
     }, 
     "estimated": { 
     }, 
     "station_id":"KPAHUNTI3", 
     "observation_time":"Last Updated on September 18, 6:04 PM EDT", 
     "observation_time_rfc822":"Thu, 18 Sep 2014 18:04:46 -0400", 
     "observation_epoch":"1411077886", 
     "local_time_rfc822":"Thu, 18 Sep 2014 18:04:53 -0400", 
     "local_epoch":"1411077893", 
     "local_tz_short":"EDT", 
     "local_tz_long":"America/New_York", 
     "local_tz_offset":"-0400", 
     "weather":"Clear", 
     "temperature_string":"71.0 F (21.7 C)", 
     "temp_f":71.0, 
     "temp_c":21.7, 
     "relative_humidity":"66%", 
     "wind_string":"Calm", 
     "wind_dir":"SSW", 
     "wind_degrees":207, 
     "wind_mph":0.0, 
     "wind_gust_mph":0, 
     "wind_kph":0, 
     "wind_gust_kph":0, 
     "pressure_mb":"1016", 
     "pressure_in":"30.01", 
     "pressure_trend":"0", 
     "dewpoint_string":"59 F (15 C)", 
     "dewpoint_f":59, 
     "dewpoint_c":15, 
     "heat_index_string":"NA", 
     "heat_index_f":"NA", 
     "heat_index_c":"NA", 
     "windchill_string":"NA", 
     "windchill_f":"NA", 
     "windchill_c":"NA", 
     "feelslike_string":"71.0 F (21.7 C)", 
     "feelslike_f":"71.0", 
     "feelslike_c":"21.7", 
     "visibility_mi":"10.0", 
     "visibility_km":"16.1", 
     "solarradiation":"--", 
     "UV":"1","precip_1hr_string":"0.00 in (0 mm)", 
     "precip_1hr_in":"0.00", 
     "precip_1hr_metric":" 0", 
     "precip_today_string":"0.00 in (0 mm)", 
     "precip_today_in":"0.00", 
     "precip_today_metric":"0", 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "forecast_url":"http://www.wunderground.com/US/PA/Philadelphia.html", 
     "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KPAHUNTI3", 
     "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=40.115807,-75.053764", 
     "nowcast":"" 
    } 
} 
+0

是你的文件擴展名.php? – 2014-09-18 21:58:08

+0

看看返回的JSON,它不包含您正在嘗試閱讀的值。當我加載該URL時,我得到'{「response」:{「version」:「0.1」,「termsofService」:「http://www.wunderground.com/weather/api/d/terms.html」,「features 「:{},」error「:{」type「:」keynotfound「,」description「:」這個鍵不存在「}}}'沒有」location「或任何東西,因此你的錯誤。看起來他們想要一個認證密鑰,而且你沒有提供認證密鑰。 – bnxio 2014-09-18 22:00:53

+0

@SterlingArcher是的。絕對看起來像它沒有得到變量。 – user2941841 2014-09-18 22:01:11

回答

0

確實存在具有輸出沒有問題:在瀏覽器中使用URL時

Notice: Undefined property: stdClass::$location in D:\Dev\xampp\htdocs\welcome.php on line 6 

Notice: Trying to get property of non-object in D:\Dev\xampp\htdocs\welcome.php on line 6 

Notice: Undefined property: stdClass::$current_observation in D:\Dev\xampp\htdocs\welcome.php on line 7 

Notice: Trying to get property of non-object in D:\Dev\xampp\htdocs\welcome.php on line 7 
Current temperature in is: 

JSON響應。

如果我用你的代碼,並在$json_string我把你的下面的代碼(和你的一樣)提供的JSON字符串:

$json_string = 'Here is the JSON string you provided'; 
$parsed_json = json_decode($json_string); 
$location = $parsed_json->{'location'}->{'city'}; 
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; 
echo "Current temperature in ${location} is: ${temp_f}\n"; 

我得到的輸出:

Current temperature in Philadelphia is: 71

當然,我中會使用它更簡單的表示法:

$json_string = 'Here is the JSON string you provided'; 
$parsed_json = json_decode($json_string); 
$location = $parsed_json->location->city; 
$temp_f = $parsed_json->current_observation->temp_f; 
echo "Current temperature in {$location} is: {$temp_f}\n"; 

但它會給我完全相同的結果:

Current temperature in Philadelphia is: 71

我在PHP 5.3和PHP 5.5進行了測試,並在這兩個版本的代碼工作沒有任何問題,所以它可能是可能的,你不要總是讓你從API提供的結果。

所以,你應該檢查是否json_decode的結果不爲空:

$json_string = 'Not valid JSON string'; 
$parsed_json = json_decode($json_string); 
if (!is_null($parsed_json)) { 
    $location = $parsed_json->location->city; 
    $temp_f = $parsed_json->current_observation->temp_f; 
    echo "Current temperature in {$location} is: {$temp_f}\n"; 
} 
else { 
    echo "JSON cannot be encoded"; 
} 

,如果我不改變運行這段代碼,我得到:

JSON cannot be encoded

+0

不起作用。給出不同的錯誤。代碼中有 – user2941841 2014-09-18 22:00:49

+0

有一個額外的'''字符。 '城市'應改爲'城市' – hkulekci 2014-09-18 22:02:25

+0

你不能使用我提供的URL,因爲它不是真正的API密鑰。我將更新來自URL的響應。 – user2941841 2014-09-18 22:03:04

0

,而不是你需要把你的KEY API密鑰,然後將下面的代碼將工作,我承諾:

$url = "http://api.wunderground.com/api/Your_Key/conditions/q/CA/San_Francisco.json"; 
$json_string = file_get_contents($url); 
$parsed_json = json_decode($json_string, true); 

if (isset($parsed_json['response']['error']) { 
    die('error occured'); 
} 

$location = $data['current_observation']['display_location']['city']; 
$temp_f = $data['current_observation']['temp_f']; 
echo "Current temperature in $location is: $temp_f\n"; 
0

正確答案:

<?php 
$url = "http://api.wunderground.com/api/KEY/geolookup/conditions/q/IA/19115.json"; 
    $json_string = file_get_contents($url); 
    $parsed_json = json_decode($json_string); 
    $location = $parsed_json->location->city; 
    $temp_f = $parsed_json->current_observation->temp_f; 
    echo "Current temperature in {$location} is: {$temp_f}\n"; 
?> 
+0

這正是我寫在我的答案,但你的方法也應該工作。請看我編輯的答案 – 2014-09-19 06:28:27