URL中包含空格的file_get_contents


file_get_contents with spaces in URL

我有一个问题,即使我将空格替换为%20并获得此内容,浏览器获得的最终url也会将"%20"变成"%2520"

这是我的代码,有什么建议可以让它发挥作用吗?这看起来很容易,但我被卡住了:/

<?php
//$_GET['song'] will contain a song name with spaces
$song = str_replace(array("%20", "&", "?" , "/"), array(" ", "", "", ""), $_GET['song']);
// I use this to check how the GET 'song' looks after the str_replace
$list = "http://www.lyrdb.com/lookup.php?q=" . $song . "&for=fullt";
echo "list url is " . $list . "<hr>";
$content = file_get_contents("http://www.lyrdb.com/lookup.php?q=" . str_replace(" ", "%20", $song) . "&for=fullt");
echo $content;
?>

如果你去http://webservices.lyrdb.com/lookup.php?q=red%20hot%20chili%20peppers&for=fullt结果应该输出一个歌词代码列表。

当我访问我的网站/?song=红辣椒,它也会将空格转换为%20,但如果看起来浏览器会将%转换为%25。

有人能帮我吗?

$song = $_GET['song']);
$url = "http://www.lyrdb.com/lookup.php?for=fullt&q=";
echo "list url is " . htmlentities($url . $song) . "<hr>";
$content = file_get_contents($url . urlencode($song));
echo $content;
$data = json_encode($_POST);
$url = "http://www.index.com?data=";
echo file_get_contents($url . urlencode($data));