与$ _GET一起使用时,file_get_contents不显示任何内容

时间:2013-01-10 19:07:33

标签: php get file-get-contents

将file_get_contents与$ _GET结合使用时遇到问题。例如,我正在尝试使用file_get_contents加载以下页面:

https://bing.com/?q=how+to+tie+a+tie

如果我像这样加载它,页面加载正常:

HTTP://localhost/load1.php

<?
echo file_get_contents("https://bing.com/?q=how+to+tie+a+tie");
?>

然而,当我像这样加载它时,我遇到了问题:

HTTP://localhost/load2.php URL = HTTPS:?//bing.com/ Q =如何+领带+ A +绑

<?
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>

当我使用第二种方法加载时,我得到一个空白页面。检查页面源不返回任何内容。当我回复$ enterurl时,我得到“https://bing.com/?q=how to tie a tie”。似乎“+”标志消失了。

此外,加载 http://localhost/load2.php?url = https://bing.com/?q =如何正常工作。网页出现了。

任何人都知道可能导致问题的原因是什么?

谢谢!

更新

尝试使用urlencode()来实现这一目标。我有一个带有输入和提交字段的标准表单:

<form name="search" action="load2.php" method="post">
<input type="text" name="search" />
<input type="submit" value="Go!" />
</form>

然后更新load2.php网址:

<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
echo '<script type="text/javascript">window.location="load2.php?url=https://bing.com/?q='.$search.'";</script>';
}
?>

这里的代码被破坏了。 $ enteredurl仍然返回与以前相同的值。 (https://bing.com/?q=how to tie a tie)

1 个答案:

答案 0 :(得分:1)

您必须正确编码参数http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie应为http://localhost/load2.php?urlhttps%3A%2F%2Fbing.com%2F%3Fq%3Dhow%2Bto%2Btie%2Ba%2Btie。您可以在JavaScript中使用encodeURIComponent来执行此操作,或在php中使用urlencode


<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
    $url = urlencode('https://bing.com/?q='.$search)
    echo '<script type="text/javascript">window.location="load2.php?url='.$url.'";</script>';
}
?>
相关问题