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!

4

1 回答 1

0

的第二个参数json_decode允许您将 JSON 解码为一个array而不是一个object. 尝试打开它,以便您始终知道自己在处理什么:

$response = json_decode($result, true);

如果失败,也许您没有正确使用 API?

于 2013-05-20T02:46:25.557 回答