0

Hey I'm having some issues using cURL (I can't use file_get_contents because my web host won't allow it) to access parts of the wunderground weather api.

When I use the following code to access info on weather conditions I have no issues whatsoever:

<? $json_url = 'http://api.wunderground.com/api/b2b4a1ad0a889006/geolookup/conditions
  /q/IA/Cedar_Rapids.json';



// jSON String for 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 I make slight modifications in order to get data on high and low tides using the following code I get nothing:

<? $json_url = 'http://api.wunderground.com/api/b2b4a1ad0a889006/tide/q/NJ/Wildwood.json';



// jSON String for 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 is at ${tide_time} ";
?>

Now I know the issue may be that I'm dealing with an array in the second example but I'm not sure how to modify the code. I know that the record for the first low tide is [3] and I've tried making the following modification with no luck.

$tide_time = $parsed_json->{'tide'}->{'tideSummary'}->[3]->{'date'}->{'pretty'}; 
echo "High tide is at ${tide_time} ";

Any help would be greatly appreciated!

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
matt tuman
  • 83
  • 1
  • 5
  • 14

1 Answers1

0

The second parameter of json_decode allows you to decode the JSON into an array instead of an object. Try turning that on so you always know what you're dealing with:

$response = json_decode($result, true);

If that fails maybe you're not using the API correctly?

Jared
  • 2,978
  • 4
  • 26
  • 45