【问题标题】:string auto-adding amp; when there is & in the URL字符串自动添加放大器;当 URL 中有 & 时
【发布时间】:2016-05-25 08:49:08
【问题描述】:

如何停止浏览器添加 amp;在 URL 中的每个 & 上都有?

所以我正在尝试从 URL 获取 JSON 文件

(http://steamcommunity.com/market/priceoverview/?currency=1&appid=570&market_hash_name=Inscribed%20Blades%20of%20Voth%20Domosh).

但是,它继续添加 amp;在每个 & 上都有,这使得 JSON 返回 null。

网址变为:(http://steamcommunity.com/market/priceoverview/?currency=1&appid=570&market_hash_name=Inscribed%20Blades%20of%20Voth%20Domosh

谁能帮我阻止我的 php 一直这样做?

这是我的代码:

$priceURL = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=570&market_hash_name=Genuine%20Bloodfeather%20Wings   ';

                                        $priceString = file_get_contents($priceURL);
                                        $price_json = json_encode($priceString, true);
                                        echo $price_json['lowest_price'];   

【问题讨论】:

  • 也分享你的php代码
  • 这是正确的行为,如果“&”应该作为文本发送。否则,它将被视为请求的查询部分的参数之间的分隔符 (...?par1=val1&par2=val2)。在服务器端,在 php 脚本中,您可以在使用它之前对其进行解码 ($par = decode_url($_GET['par1']))。
  • $priceURL = 'steamcommunity.com/market/priceoverview/…'; $priceString = file_get_contents($priceURL); $price_json = json_encode($priceString, true); echo $price_json['lowest_price'];

标签: php json url


【解决方案1】:

你应该解码url,像这样使用。

  $priceURL = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=570&market_hash_name=Genuine%20Bloodfeather%20Wings   ';

 $priceURL =  urldecode($priceURL);

【讨论】:

  • 我正在使用 file_get_contents 和 json_decode。使用您告诉我的内容后,我收到此错误“file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known.”
  • @KMackinley。看来你的网址不对直接用浏览器试试
  • 我认为它是正确的 url.. (steamcommunity.com/market/priceoverview/…) 你可以尝试打开它
  • 你做错了。您需要使用 php curl 或 js ajax 来获取 api 响应。 file_get_contents 不会返回 php 执行的响应。 Onlt textual like html 将返回。
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2012-03-27
相关资源
最近更新 更多