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 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏