php - 以 url 作为变量的 file_get_contents

标签 php file-get-contents

我可以使用类似这样的方法来检索域外部的页面吗?

<?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;
 ?>

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

最佳答案

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

关于php - 以 url 作为变量的 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4672597/

相关文章:

php - 使用 regexr 工具识别 (weat)her

php - 在 PHP 中使用 URL 获取元素的特定内容 block

php - 如何在 PHP 中调用 file_get_contents() 而不等待结果?

php - 我有一个带有 html div 内容的 php 脚本。需要使用包括 css 的 html 代码获取 php

javascript - $.ajax() 不提供从静态 json 文件获取 json 的请求

php - 如何在laravel 5.4 的迁移文件中使用原始mysql 语句?

PHP 作用域、$GLOBALS 和单例

javascript - 使用ajax和jquery提交表单

php - cURLlib/File_get_contents 只加载部分数据

PHP file_get_contents,在include之前对包含的文件执行PHP