【问题标题】:curl post to php file with sleep() function includedcurl 发布到包含 sleep() 函数的 php 文件
【发布时间】:2013-11-29 21:51:52
【问题描述】:

我想通过登录脚本发送一封包含自动内容的电子邮件,但发送时应该延迟一些(随机)秒(用于发送密钥对的一部分)。

这就是为什么我尝试使用通常有效的 curl,而不是使用“include”或类,但主脚本不必等到 sleep() 结束。

主要代码是(简单卷曲代码) 玩弄“CURLOPT_MUTE,1”和“CURLOPT_RETURNTRANSFER,false”根本不起作用。


    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"http://domain/path/to/delayed_mail.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        'to=' . $to . '&from=' . $from . '&subject=...');
    curl_exec ($ch);
    curl_close ($ch);
    ?>

delayed_mail.php 看起来像这样


    <?php
    //path to mail class
    //some POST and GET REQUEST filters and authentications
    //with      $_REQ[$key]=$value;   as output

    $delay = rand(2,32);
    sleep($delay);
    $ddlab->mail->html($_REQ['to'],$_REQ['from'],...,$_REQ['options']);
    ?>

正如我上面提到的,我有点卡住了。用于 html 输出的主脚本,在 sleep() 之后,不应等待,直到发送电子邮件。

第一个问题:如何独立执行“delayed_mail.php”(随时发送电子邮件,但让我的脚本运行!)

第二个问题:如何设置内部路径,例如 '../../delayed_mail.php' 或 getcwd().'/delayed_mail.php' (两者似乎都不起作用)而不是完整的“ http://"-URL ?

感谢您的努力。

【问题讨论】:

  • 我的问题可能包括“有没有其他方法可以代替使用 curl 来达到与描述相同的结果?”

标签: php curl sleep delayed-execution


【解决方案1】:

curl 不在 php 超时限制下计算 这意味着 curl 命令可以永远运行以获得响应,但您可以使用 curl 内部选项来限制它 有了这些信息,您的脚本必须使用常规的 php sleep 命令!

$delay = rand(2,32);
    sleep($delay);

会随机延迟 2 到 32 秒

2-如果使用您设置的完整路径,则 curl 会更好!

http://domain/path/to/delayed_mail.php

curl 使用这个 url 就像你在浏览器中输入它一样!!

【讨论】:

  • 随机延迟...这没关系,也是我想要的。我知道,我可以使用 sleep(7) 设置固定延迟。这不是问题的一部分:-)
【解决方案2】:

是的,我想回答我的问题 :-)

解决方案是,根本不使用 curl :-),感谢 Martin Lakes 在http://php.net/manual/en/function.exec.php 的帖子,我使用了 Linux 命令。对于基于 Windows 的服务器,它必须看起来不同,但这里没有足够的空间;-)


    <?php
    passthru("
        /usr/bin/php
        /home/www/path/to/delayed_mail.php
        " . $params . "
        >
        /home/www/path/to/delayed_mail.log
        2>&1
        &
        ");
    ?>

这里解释:


    <?php
    passthru("
        linux_php_call
        executed_file
        transferred_parameters
        linux_command_for_output 
        output_file
        linux_commands
        ampersand_at_the_end_for_silent_mode
        ");
    ?>

(所有参数以空格排队)

只需将所有必需的参数放入 $params,例如作为一个 array(),你会得到它作为 $argv 的键,这也是一个 array(),你在这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 2015-07-29
    相关资源
    最近更新 更多