2014-09-26 70 views
0

我currentley试图从网站获取数据,并在我的PHP脚本中使用它。如何在URL中使用file_get_contents()和非ASCII字符?

我想达到的联系是: http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=★%20Bayonet

这里是我的代码:

<?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&currency=3&appid=730&market_hash_name =★卡口):无法打开流:HTTP请求失败! HTTP/1.0 500 F中内部服务器错误:\ XAMPP \ htdocs中\ getPrice.php第5行

编辑:

好了,我已经找到了我的问题的解决方案,但现在一个新的错误正在上传:

Warning:file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85+Bayonet+%7C+Stained(Factory New)):无法打开流:HTTP请求失败! HTTP/1.0 500 F中内部服务器错误:\ XAMPP \ htdocs中\ getPrice.php线路7

这是我如何用我现在的链接:

getPrice.php market_hash_name =★刺刀|彩绘

这是我的代码:

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"; 
+0

'HTTP://steamcommunity.com/market/priceoverview/国家= US&货币= 3的appid = 730&market_hash_name =%E2%98%85%20Bayonet' – Steve 2014-09-26 15:45:30

+0

尝试设置的unicodecaractère而不是复制/粘贴这样的天气caractère:★ – 2014-09-26 15:47:19

+0

根据你的编辑,你需要使用Curl。 – 2014-09-26 16:05:57

回答

1

在其他字符,非ASCII字符必须在查询字符串URL-encoded (aka percent-encoded)

$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); 
+0

请检查我的新文章 – user3544504 2014-09-26 16:02:21