php - Using cURL with wunderground api -
hey i'm having issues using curl (i can't use file_get_contents because web host won't allow it) access parts of wunderground weather api.
when use following code access info on weather conditions have no issues whatsoever:
<? $json_url = 'http://api.wunderground.com/api/b2b4a1ad0a889006/geolookup/conditions /q/ia/cedar_rapids.json'; // json string request $json_string = '[http://api.wunderground.com/api/b2b4a1ad0a889006/geolookup/conditions /q/ia/cedar_rapids.json]'; // initializing curl $ch = curl_init( $json_url ); // configuring curl options $options = array( curlopt_returntransfer => true, ); // setting curl options curl_setopt_array( $ch, $options ); // getting results $result = curl_exec($ch); // getting json result string $parsed_json = json_decode($result); $location = $parsed_json->{'location'}->{'city'}; $temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; echo "current temperature in ${location} is: ${temp_f}\n"; ?>
however, when make slight modifications in order data on high , low tides using following code nothing:
<? $json_url = 'http://api.wunderground.com/api/b2b4a1ad0a889006/tide/q/nj/wildwood.json'; // json string request $json_string = '[http://api.wunderground.com/api/b2b4a1ad0a889006/tide/q/nj/wildwood.json]'; // initializing curl $ch = curl_init( $json_url ); // configuring curl options $options = array( curlopt_returntransfer => true, ); // setting curl options curl_setopt_array( $ch, $options ); // getting results $result = curl_exec($ch); // getting json result string $tide_time = $parsed_json->{'tide'}->{'tidesummary'}->{'date'}->{'pretty'}; echo "high tide @ ${tide_time} "; ?>
now know issue may i'm dealing array in second example i'm not sure how modify code. know record first low tide [3] , i've tried making following modification no luck.
$tide_time = $parsed_json->{'tide'}->{'tidesummary'}->[3]->{'date'}->{'pretty'}; echo "high tide @ ${tide_time} ";
any appreciated!
the second parameter of json_decode
allows decode json array
instead of object
. try turning on know you're dealing with:
$response = json_decode($result, true);
if fails maybe you're not using api correctly?
Comments
Post a Comment