douwei3863 2013-11-29 21:51
浏览 142
已采纳

curl发布到包含sleep()函数的php文件

I want to send an email with automatted content from a login script, but it should be sent with a delay of some (randomized) seconds (for sending one part of a key pair).

Thats why I was trying to use curl, which generally works, instead of using "include" or a class, but the main script should not have to wait until sleep() is ended.

main code is (simple curl code) playing around with "CURLOPT_MUTE,1" and "CURLOPT_RETURNTRANSFER,false" did not work at all.


    <?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 looks something like this


    <?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']);
    ?>

As I mentioned above, I got stuck a bit. The main script which is for html output, shall not wait, until email is sent, after sleep().

1st question: How "delayed_mail.php" can be executed independent (Send email whenever you want to do so, but leave my script running!)

2nd question: How can I set an internal path like '../../delayed_mail.php' or getcwd().'/delayed_mail.php' (which both doesn´t seem to work) instead of full "http://"-URL ?

Thanks for your efforts.

  • 写回答

2条回答 默认 最新

  • dongzhang6677 2013-11-30 01:27
    关注

    YES I want to answer my question :-)

    The solution is, NOT to use curl at all :-), thanks to Martin Lakes´s post at http://php.net/manual/en/function.exec.php I used a Linux command. For Windows based servers it must look different, but there´s not enough space on here ;-)

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

    which is explained here:

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

    (All parameters queued with a space)

    Just put all required parameters into $params, e.g. as an array(), you´ll get it as a key of $argv, which is an array() too and here you are.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题