PHP json url传递错误替换&与,,


PHP json url not passing correctly replacing & with &

得到这个错误

[Tue Aug 18 14:41:57.614006 2015] [:error] [pid 21389] [client 67.90.138.68:5794] PHP警告:file_get_contents(http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=BR: Vigilante Crate): failed to open stream: HTTP request failed!HTTP/1.0 500内部服务器错误'r'n

<?php
foreach($json2['rgDescriptions'] as $i){
$item = $i['market_name'];
$icon = $i['icon_url'];
$fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=' . $item;
$grab = file_get_contents($fetchdata);
$id = json_decode($grab, true);
echo '<img src="'. $imgurlbase . $icon . '/64fx64f">' . '<a class="btn btn-primary btn-lg" href="#" role="button">' . $item . '</a>'. '<a class="btn btn-primary btn-lg" href="#" role="button">'. $id['lowest_price'].'</a>'.'<br>';
}

由于某些原因&在基础url被传递为'&

我不是最好的php,但没有我正在寻找是为什么。它是否与json_decode有关?这是我唯一的猜测。

您需要对字符串进行编码:

$fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=' . urlencode($item);

works for me:

php > $fetchdata ='http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name='.urlencode('BR: Vigilante Crate');
php > $grab = file_get_contents($fetchdata);
php > print_r(json_decode($grab));
stdClass Object
(
    [success] => 1
    [lowest_price] => $0.07
    [volume] => 4,994
    [median_price] => $0.07
)