普通网友 2014-09-18 21:57
浏览 37

在PHP中传递变量

I am just beginning to learn PHP ( I have some moderate Java exp)

but I cant figure out how to simply pass a variable as an argument.

Please see below from tinkering with the underground api. I create the variable for the URL I need to pass but when I put it in the second line it doesnt seem to read the variable. url is the variable in question. When i place the url directly as the argument then it works without issue. However the below code gives an error.

  <?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}
";
    ?>

Error:

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 response when using URL in browser:

{
  "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":""
    }
}
  • 写回答

3条回答 默认 最新

  • douji8347 2014-09-18 21:59
    关注

    Actually there is no problem with output.

    If I use your code and in $json_string I put JSON string you provided for the following code (the same as yours):

    $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}
    ";
    

    I get output:

    Current temperature in Philadelphia is: 71

    Of course I would use for it simpler notation:

    $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}
    ";
    

    But it will give me exact same result:

    Current temperature in Philadelphia is: 71

    I've tested it in PHP 5.3 and PHP 5.5 and in both versions the code is working without any problem, so it might be possible that you don't always get the result you provided from API.

    So you should check if result of json_decode is not 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}
    ";
    }
    else {
        echo "JSON cannot be encoded";
    }
    

    and if I run this code without a change I get:

    JSON cannot be encoded

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突