首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >URL中包含空格的file_get_contents

问URL中包含空格的file_get_contents
EN

Stack Overflow用户
提问于 2012-03-07 14:53:25
回答 2查看 16K关注 0票数 8

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

这是我的代码,有什么建议可以让它工作吗?这看起来很简单,但我被卡住了:/

代码语言:javascript
复制
<?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辣辣椒时,它也会将空格转换为%20,但如果浏览器似乎将%转换为%25。

有人能帮帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-07 14:57:56

代码语言:javascript
复制
$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;
票数 17
EN

Stack Overflow用户

发布于 2019-05-26 21:16:03

代码语言:javascript
复制
$data = json_encode($_POST);

$url = "http://www.index.com?data=";

echo file_get_contents($url . urlencode($data));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9597022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档