【问题标题】:Execute multiple PHP scripts from 1 PHP script in Browser从浏览器中的 1 个 PHP 脚本执行多个 PHP 脚本
【发布时间】:2014-11-27 06:37:16
【问题描述】:

情况是这样的。我创建了多个名为 operation.php 的 php 文件,并托管在我的域中。像这样的:

  1. example.com/operation.php
  2. example123.com/operation.php
  3. example1234.com/operation.php

好的,现在我想要编写 1 个单独的 PHP 脚本,它将成为所有这些 operation.php 脚本的母体。如果我从浏览器执行motherexample.com/operaterun.php,那么所有这些php脚本都会一个接一个地运行。

这可能吗?这会导致服务器中断吗?我不想一次运行所有这些,可能在每个脚本执行之间间隔 10 秒。

需要帮助!

更新我不确定你们是否得到它,但这是另一个例子.. 假设您有 100 个站点,每个站点都有位于 example001.com/maintenance.php 的 maintenance.php,现在无法在浏览器中逐个加载和运行这 100 个 maintenance.php。所以这就是为什么我想要 1 个单一的 mother-maintenance.php,当从浏览器运行时,它会一个一个地执行这些 maintenance.php,或者可能有一些时间间隔!

【问题讨论】:

  • 你在那里建了一个小僵尸网络?
  • @Darren 大声笑不!单独的 php 脚本是一个维护脚本,我必须为我的每个网站手动运行它。 :-) 这需要时间!我想自动化整个过程!
  • 都是好人!我只是在工作中感到无聊哈哈,您应该查看 vinay 发布的内容(使用 wget)或类似的内容。您还需要防止其他人随机访问它
  • 做一个控制台命令不是更好吗?在浏览器中执行此操作意味着您正在处理诸如浏览器超时和其他奇怪问题之类的事情。

标签: php browser


【解决方案1】:

如果用户正在这样做,我会推荐 AJAX。 否则你可以试试这样的代码。

<?php
$somearg = escapeshellarg('blah');
exec("php file2.php $somearg > /dev/null &");

发现于 (https://stackoverflow.com/a/1110260/4268741)

但是,您需要对其进行一些更改才能使其在您的项目中运行。

【讨论】:

  • 您可以做的另一件事是在第一个 operation.php 文件的末尾添加header('Location: http://example123.com/operation.php'); 等等。
【解决方案2】:

您需要从motherexample.com/operaterun.php 运行类似wget 的调用。 wget 适用于 linux,如果您使用的是 windows 域检查替代方案 例子

wget http://example.com/operation.php
wget http://example123.com/operation.php
wget http://example1234.com/operation.php

【讨论】:

    【解决方案3】:

    您可以使用 AJAX 请求。只需从您的母脚本向子页面发出请求。

    或者您可以使用简单的方法 - 为每个孩子使用 iframe。如果您有 100 个子页面,则只需在母页面 100 iframe 上生成 src 到子页面。或者您可以通过 javascript 创建此 iframe 并将其添加到页面。然后您可以在请求之间进行延迟。

    【讨论】:

    • 是的!我在谷歌中检查了这一点。我不能在 iframe 执行之间使用延迟?对于通过 javascript 使用 iframe,我该怎么做?请帮忙..
    • @Swayam 例如你可以检查这个问题stackoverflow.com/questions/3069915/…
    • 假设我将这些脚本中的 100 个放在 Mother.html 文件中 - 我将如何在每一个之间插入延迟?其次,我不需要这些在窗口元素加载后运行,因为这是为了在母域的单独路径中运行,例如 example-mother.com/mother-maintenance.php
    • @Swayam 由 PHP javascript 数组生成,其中包含 iframe 的 url。在 javascript 函数中创建将从数组中获取当前元素,为此 url 创建 iframe 并增加当前数字并使用 setTimeout 在 10 秒内调用 self。加载页面后调用此函数。
    【解决方案4】:

    我认为 file_get_contents 可以帮助你:

    <?php
    //index.php
    set_time_limit(0);
    
    $op_list = array(
        "example.com/operation.php",
        "example123.com/operation.php",
        "example1234.com/operation.php",
    );
    
    $opts = array(
        "http" => array(
            "method" => "get",
            "timeout" => 60,
            )
    );
    
    $context = stream_context_create($opts);
    
    foreach($op_list as $key => $value)
    {
        if($response = file_get_contents($value, null, $context))
        {
            $json_decode = json_decode($response, true);
    
            echo "{$value}: time_start: {$json_decode['time_start']}, time_end: {$json_decode['time_end']} <br />";
        }
        else
        {
            echo "{$value}: fail <br />";
        }
    }
    
    ?>
    

    这是一个完整的例子:

    http://try.valinv.com/php/21a6092d20601c77982f7c1ec6b2cc23.html

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多