如何将file_get_contents()与URL中的非ASCII字符一起使用


How to use file_get_contents() with non-ASCII characters in URL?

我目前正试图从网站获取数据,并在我的PHP脚本中使用它。

我试图联系的链接是:http://steamcommunity.com/market/priceoverview/?country=US&货币=3&appid=730&市场现金名称=★%20卡口

这是我的代码:

<?php
$skin = $_GET['market_hash_name'];
$link = "http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);
echo $FN;
?>

这就是我如何使用我的链接:

http://myhost.com/getPrice.php?market_hash_name=★ Bayonet

这是我得到的错误:

警告:file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US&货币=3&appid=730&market_hash_name=â…卡口):无法打开流:HTTP请求失败!HTTP/1.0 500内部服务器错误,F:''examplep''htdocs''getPrice.php第5行

编辑:

好吧,所以我已经找到了解决问题的方法,但现在又出现了一个新的错误:

警告:file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US&货币=3&appid=730&market_hash_name=%E2%98%85+卡口+%7C+污点(出厂新)):无法打开流:HTTP请求失败!HTTP/1.0 500内部服务器错误,F:''examplep''htdocs''getPrice.php第7行

这就是我现在使用链接的方式:

getPrice.php?市场现金名称=★卡口|染色

这是我的代码:

function getPrice($string) {
$skin = urlencode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=".$skin." ".$string;
$content = file_get_contents($link);
$data = json_decode($content, true);
return $data['lowest_price'];
}
$FN = getPrice("(Factory New)");
$MW = getPrice("(Minimal Wear)");
$FT = getPrice("(Field-Tested)");
$WW = getPrice("(Well-Worn)");
$BS = getPrice("(Battle-Scarred)");

echo "Factory New: $FN; Minimal Wear: $MW; Field-Tested: $FT; Well-Worn: $WW; Battle-Scared: $BS";

在其他字符中,非ASCII字符必须在查询字符串中进行URL编码(也称为百分比编码)。

$skin = url_encode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);