【问题标题】:PHP Change Part of a URLPHP 更改 URL 的一部分
【发布时间】:2015-07-14 17:50:03
【问题描述】:

我正在为一个网站进行奇妙的天气集成,我想更改/编辑 API 请求 URL 的部分内容。我已经尝试了几个在 Stack Overflow 上找到的解决方案,但都没有奏效。

$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 位置可编辑(州和城市)。

它会输出这个:

[城市名称]的当前温度是:[温度]

对如何编辑请求 URL 有任何想法吗?

【问题讨论】:

  • 更改变量$state$city 不是满足您的需求吗?
  • 我认为您在查询字符串中混淆了 "',您将变量放置在其中...

标签: php url text replace edit


【解决方案1】:

这部分错了:

$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');

查看匹配的引号和撇号。

【讨论】:

  • 太棒了!我知道我错过了一些东西。谢谢一百万!
猜你喜欢
  • 1970-01-01
  • 2018-12-28
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多