【问题标题】:file_get_contents with url as variable以 url 作为变量的 file_get_contents
【发布时间】:2011-01-12 18:39:39
【问题描述】:

我可以使用这样的东西来检索与域相关的外部页面吗?

<?php
    $q_pass =$_REQUEST['query_passed'];
    $fetcher = "http://www.abc.com/search?q=".$q_pass;
 $homepage = file_get_contents($fetcher);
 echo $homepage;
?>

我传递了一个变量,并想从 abc.com 检索结果。可以将 $fetcher 传递给 file_get_contents 以检索页面的内容吗?当我点击这个 php 页面时,我得到了空白页面。但如果我使用类似

的东西,它又可以正常工作了
 <?php
        $fetcher = "http://www.abc.com/search?q=query";
        $homepage = file_get_contents($fetcher);
        echo $homepage;
 ?>

这里发生了什么?对此有什么技术解释吗?

【问题讨论】:

  • 当然,有可能。尝试调试 $_REQUEST["query_passed"] 以找出问题所在
  • 由于某种原因,$_REQUEST['query_passed'] 未设置。这是 GET 还是 POST 请求?浏览器窗口中的查询字符串是什么(? 后面的那位)?
  • $_SERVER['QUERY_STRING']

标签: php file-get-contents


【解决方案1】:

您应该在 $q_pass 上使用 urlencode。我猜你在 query_passed 中传递了一个带有空格的值。

【讨论】:

  • 这是我搞砸的空格。在将其作为参数传递给 PHP 文件时使用了未编码的字符串。
猜你喜欢
  • 1970-01-01
  • 2013-05-10
  • 2015-07-19
  • 2012-03-25
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多