doujiunai2169 2015-08-03 23:58
浏览 12
已采纳

php代码执行序列

I have a question about how does php code execute.

I am now only using php to connect to the mysql DB for iOs APP. So I create different APIs at php side. Then, at APP side, just submit post or get request to inquiry or update the DB.

So at php side, I did DB inquiry or update then return a json result to the APP.

Now I have a function, it does the following job:

function a(){
  1.Insert a record to the DB;
   if (insert successful){
      2. $this->echoJson ('Succeed','New Record has added');

      3. then send email to several email addresses.(using PHPMailer-master)
    }

}

When my APP side get the json data that indicates insertion is successful. The app will jump to another page.

The function works well. The only problem is step 3 takes around 5 seconds to run. Step 1 and 2 is very fast. It seems for the php side, it only returns the json data when all the codes have been executed. So my APP side needs to wait for 5 seconds to get the response. But I only care about if the insertion is successful.

Is it possible to return the json data once the insertion is successful then do the sending?

I am not sure if I explain the problem clearly.

In summary, I found the execution order is step 1 step 3 step 2. I want to make the order as step 1 step 2 step 3.

Thank you.

  • 写回答

3条回答 默认 最新

  • doqs8936 2015-08-04 00:06
    关注

    Move your PHPMailer funtion to another file (lets say send.php, which can be reached internally by http://example.com/send.php). Then you can make a curl request with a tiny timeout function that actually sends the mail.

    Step 3 becomes:

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'http://example.com/send.php');
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
    
    curl_exec($ch);
    curl_close($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?