我刚开始学习PHP(我有一些温和的Java exp)
但我无法弄清楚如何简单地将变量作为参数传递。
请参阅下面的修补地下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";
?>
错误:
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:
在浏览器中使用URL时的JSON响应:
{
"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":"/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 :(得分:0)
实际上输出没有问题。
如果我使用你的代码并在$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";
我得到输出:
费城目前的气温为: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";
但它会给我完全相同的结果:
费城目前的气温为:71
我已经在PHP 5.3和PHP 5.5中对它进行了测试,在这两个版本中代码都没有任何问题,因此您可能无法始终获得API提供的结果。
所以你应该检查json_decode的结果是否为null:
$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无法编码
答案 1 :(得分: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";
答案 2 :(得分: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";
?>