我有一个问题,即使我将空格替换为 %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=red hot chili pests 时,它也会将空格转换为 %20 ,但如果浏览器看起来会将 % 转换为 %25 。
有人可以帮我吗?
始终使用
urlencode()
表示单个参数,或 http_build_query()
表示 HTTP 查询字符串中的多个参数:
$song = $_GET['song']);
$url = "http://www.lyrdb.com/lookup.php?for=fullt&q=";
$content = file_get_contents($url . urlencode($song));
或
$url = "http://www.lyrdb.com/lookup.php";
$query = ['for' => 'fullt', 'q' => $song];
$content = file_get_contents($url . '?' . http_build_query($query));
$data = json_encode($_POST);
$url = "http://www.index.com?data=";
echo file_get_contents($url . urlencode($data));