PHP 更改 URL 的一部分
PHP Change Part of a URL
我正在为一个网站开发一个 wunderground 天气集成,我想 change/edit 请求 URL 的部分 API。我已经尝试了几个在 Stack overflow 上找到的解决方案,但是 none 已经奏效了。
$state = 'NC';
$city = 'Boone';
$json_string = file_get_contents("https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/'.$state.'/'.$city.'.json");
$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";
我好像想不通。如您所见,我正在尝试使州和城市 URL 展示位置可编辑(州和城市)。
它将输出:
Current temperature in [city name] is: [temp]
对如何编辑请求有任何想法URL?
这部分是错误的:
$json_string = file_get_contents("https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/'.$state.'/'.$city.'.json");
这应该是这样的:
$json_string = file_get_contents("https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/".$state."/".$city.".json");
或者像这样
$json_string = file_get_contents('https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/'.$state.'/'.$city.'.json');
查看匹配的引号和撇号。
我正在为一个网站开发一个 wunderground 天气集成,我想 change/edit 请求 URL 的部分 API。我已经尝试了几个在 Stack overflow 上找到的解决方案,但是 none 已经奏效了。
$state = 'NC';
$city = 'Boone';
$json_string = file_get_contents("https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/'.$state.'/'.$city.'.json");
$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";
我好像想不通。如您所见,我正在尝试使州和城市 URL 展示位置可编辑(州和城市)。
它将输出:
Current temperature in [city name] is: [temp]
对如何编辑请求有任何想法URL?
这部分是错误的:
$json_string = file_get_contents("https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/'.$state.'/'.$city.'.json");
这应该是这样的:
$json_string = file_get_contents("https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/".$state."/".$city.".json");
或者像这样
$json_string = file_get_contents('https://api.wunderground.com/api/MY-API-KEY/geolookup/conditions/q/'.$state.'/'.$city.'.json');
查看匹配的引号和撇号。