URL中带有空格的file_get_contents [英] file_get_contents with spaces in URL
问题描述
我有一个问题,即使我将空格替换为%20并获得此内容,浏览器获得的最终URL也会将%20"变为%2520"
I have an issue where even if I replace the spaces to %20 and get this content the ultimate url the browser gets turns the "%20" into "%2520"
这是我的代码,有什么建议可以使其正常工作吗?似乎很容易,但是我被卡住了:/
Here's my code, any suggestions to get this to work? it seems easy but I'm stuck :/
<?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 结果应输出一个歌词列表.
if you go to http://webservices.lyrdb.com/lookup.php?q=red%20hot%20chili%20peppers&for=fullt The result should output a list of lyric codes.
当我访问我的网站/?song = red hot chili peppers时,它也将空格转换为%20,但是如果浏览器似乎将%转换为%25.
When i go to my website /?song=red hot chili peppers , it too converts spaces to %20 's but if it seems the browser converts the %'s to %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;
这篇关于URL中带有空格的file_get_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!