什么是优化代码和查询的最佳方法?

时间:2016-10-30 15:12:05

标签: php mysql arrays

我有来自php IMAP(提取电子邮件)的数百个响应,我想将所有提取的电子邮件插入数据库。问题是在服务器停留时间是40秒(主机提供商对我说),我的脚本花了超过40秒。由于提取记录和插入查询服务器返回以下错误。

[an error occurred while processing this directive]

这是Php代码

$imap = @imap_open("{mail.******.com:143/novalidate-cert}", '*****@***.com', '*****');


            if(isset($imap)){
                $message_count = @imap_num_msg($imap);
                $array_mail = array();
                if($message_count > 0){
                    for ($m = 1; $m <= $message_count; ++$m){
                        //$header = @imap_header($imap, $m);
                        $header = @imap_rfc822_parse_headers(imap_fetchheader($imap, $m));
                        $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host;

                        $from = $email[$m]['fromaddress'];
                        $from_email = $email[$m]['from'];
                        $array_mail[] = $from_email;
                        if(isset($header->cc)){
                            $hedCC = $header->cc;
                            foreach($hedCC as $s){
                                //echo $s->mailbox.'@'.$s->host.'<br />';
                                $array_mail[] = $s->mailbox.'@'.$s->host;
                            }
                        }

                        if(isset($header->to)){
                            $hedTO = $header->to;
                            foreach($hedTO as $t){
                                //echo $t->mailbox.'@'.$t->host.'<br />';
                                $array_mail[] = $t->mailbox.'@'.$t->host;
                            }
                        }
                    }
                    $i = 0;
                    if (!empty($array_mail)) {
                        $insertCount = 0;
                        foreach($array_mail as $m_){
                            // INSERT HERE
                            //echo $m_.'<br />';
                            $isExistWithID  = $this->isExistWithID($user_id,$m_);
                            $email_err      = $this->isValidEmail($m_);
                            $isValid = 0;
                            if($email_err == false){
                                $isValid = 1;
                            }
                            if($isExistWithID==0 && $isValid==0){
                                $insert = $this->pdoConnection->prepare("
                                    INSERT INTO ws_email (
                                        u_id,
                                        we_email, 
                                        date_added
                                    ) 
                                    VALUES(
                                        :u_id, 
                                        :we_email, 
                                        :date_added
                                    )
                                    ");
                                $insert->bindParam(':u_id', $u_id);
                                $insert->bindParam(':we_email', $we_email);
                                $insert->bindParam(':date_added', $date_added);

                                // Insert Data
                                $u_id           = $_SESSION['USERID'];
                                $we_email       = $m_;
                                $date_added     =  date("Y-m-d H:i:s");
                                if($insert->execute()){
                                    $insertCount++;
                                }
                            }
                            $i++;

                        }// end foreach
                        $response['success'] = $insertCount.'Email address(s) fetched successfully.';
                    }
                    //echo $i; 
                    //
                }else{
                    $response['error']      = 'No record found';
                }
            }else{
                $response['error']      = 'IMAP does not connect'; 
            }
        }//end if error is equal to 0
        /************ END NEW 29 SEP *************/
        echo json_encode($response, JSON_PRETTY_PRINT);

我尝试尽可能地优化代码,但仍然花费很长时间,并且由于此服务器返回错误。

我想请你指导我如何优化它。谢谢。

0 个答案:

没有答案
相关问题