一个PHP脚本的JSON输出作为PHP中另一个脚本的输入

时间:2016-05-16 20:07:44

标签: php json

我有A.php脚本,它以JSON格式转换CSV文件。现在,通过手动复制粘贴将输出保存在另一个JSON文件中。 B.php脚本使用该JSON文件并在浏览器中显示数据表。 我想跳过第一步,我必须从A.php复制粘贴JSON输出并将其存储在文件中,以便B.php可以使用它。

如何将A.php文件输出作为输入输入到B.php? 我该怎么做?

PS-我对PHP脚本非常陌生。

2 个答案:

答案 0 :(得分:2)

您可以将A.php脚本return作为JSON,而不是(或同样)将JSON保存到文件中。然后,您可以使用include在文件B.php中获取它。

所以在文件A.php中:

// $json = however you are getting it from the CSV
// optionally output to file (if you still want to do this for some other reason)
return $json;

(确保return是脚本中的最后一件事,因为它之后什么都不会执行。)

在文件B.php中:

// instead of $json = [PASTE FILE CONTENTS HERE], do this:
$json = include 'A.php';

答案 1 :(得分:0)

如果你真的想要使用外部json文件,你可以使用文件处理程序php函数。

<?php
$fp = fopen('data.json', 'w');
fwrite($fp, $file_A_results);
fclose($fp);

之后,你必须手动调用B.php ......所以请考虑不要恐慌的答案!

相关问题