【问题标题】:put Json encoded Data from php file into array on将 php 文件中的 Json 编码数据放入数组中
【发布时间】:2015-11-14 09:08:15
【问题描述】:

我已经为数据库中的所有事件构建了一个服务。 在 javascript 中调用该服务可以正常工作。现在我正在尝试在 php 中获取页面加载的结果。在服务页面(events.php)上,我有一个这样的开关功能:

$switcher = isset($_REQUEST["set"]) ? $_REQUEST["set"] : "";
switch ($switcher)
{
case 'list':    
    $events = $_crs->listCourse();
    echo json_encode($events);
break;
...
default:
break;
}

在 javascript 中,我给出了一个像这样进行切换的参数:

url: "events.php?set=all";

在 PHP 中,我试图通过运行以下命令来获得结果:

$list = file_get_contents( "events.php?set=list&from=$from&to=$to&category=&limit=15");

问题 1:当我这样做时,我收到此错误: 无法打开流:中没有这样的文件或目录

问题 2:省略 get 变量时,我会从 events.php 文件中获取原始 php 代码!

我该如何解决这个问题?

【问题讨论】:

  • @Mujnoi Gyula Tamas:感谢您的快速回复,但我不明白这个答案对我有什么帮助,因为我的问题不关心包括?
  • 原理是一样的。一种可能可行的解决方法是从本地主机流式传输,但我不知道这将如何影响共享主机。 file_get_contents( 'http://localhost/events.php?set=list&from=$from&to=$to&category=&limit=15' ),如果不使用您的域名的完整 URL 来获得响应。 LE:你为什么不为你的两个页面创建一个共享类?

标签: php arrays json


【解决方案1】:

查看 (file_get_contents)[http://php.net/manual/en/function.file-get-contents.php] 的手册页。

您正在传递“events.php?set=list&from=$from&to=$to&category=&limit=15”,它理解为一个文件名。因此出现消息“没有这样的文件或目录”。

我认为你想要的是执行文件 events.php 并读取结果。

file_get_contents 也可以做到这一点。再看一遍手册,了解各种示例以开始使用。

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多