2015-05-04 25 views
0

我目前正在寫東西,使用蒸汽網絡API,我試圖通過網絡API來獲取蒸汽市場價格像這樣:如何在PHP中跳過這些&符號?

$url = 'http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name='.$itemInfo['market_hash_name']; 

$itemInfo['market_hash_name']是從另一個JSON我從得到的東西API。

應該返回是這樣的:

http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=SCAR-20 | Contractor (Well-Worn) 

和它,這是很好的,因爲當我扔東西到瀏覽器,把它翻譯成

http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=SCAR-20%20|%20Contractor%20(Well-Worn) 

返回的JSON我需要。

但是,當它與get_file_contents使用的一些原因,它將類似於在與號這樣:

http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=Operation Breakout Weapon Case 

,如果我把它變成一個瀏覽器窗口,它沒有返回值。因此,如果我使用htmlspecialcharshtml_entity_decode並不重要,因爲無論何時我將它放入get_file_contents,它都會再次對連字符進行編碼。

我該怎麼做?

+0

當你看到'http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=SCAR-20%20|%20Contractor%20(Well-Worn) '你在瀏覽器中查看源代碼還是輸出? – chris85

+0

當我運行這個,它會返回成功: 'var_dump(file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=SCAR-20 | Contractor(Well-Worn)')) ;' – infomaniac

回答

1

因此,如果我使用htmlspecialchars或html_entity_decode,cuz每當我把它放入get_file_contents它只是再次編碼&符號它並不重要。

無論那些爲URI -encoding。這是urlencode的工作。

所以:

$url = 'http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name='.urlencode($itemInfo['market_hash_name']); 
+0

我已經試過urlencode,但是它只能在itemInfo上...我的不好,謝謝:) – Rune