yii 在cpanel中设置Cronjob调用php框架方法

eivnm1vs  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(137)

我写了一个代码来调用邮件功能,它将在特定的时间运行。我正在使用Yii框架,我想调用someController的一个someMethod。
我在命令栏写了wget http://example.com/mycontroller/mymethod,它正在运行,但是只发送邮件到数据库的第一个邮箱,之后,它抛出一个错误404 not found:

Connecting to example.com|50.22.11.25|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2015-08-25 01:15:08 ERROR 404: Not Found.`

cronjob命令的正确语法是什么?
下面是我尝试通过cronJob运行的代码:

<?php
    class SendMailController extends Controller
    {
        public function actionSendMailUsingCronJob()
        {
            $response = array();
            $countMail = 0;
            $mail_template = Yii::app()->db->createCommand()
            ->select('*')
            ->from('mail_mailing_template')
            ->where('status=:status', array(':status'=>'not_sent'))
            ->queryAll();
            foreach ($mail_template as $single){
                $html = $single['template'];
                $subject = $single['subject'];
                if($single['send_to'] == 'student'){
                    $response['tbl_used'] = 'students';
                    $mailingList =Yii::app()->db->createCommand()
                                ->select('*')
                                ->from('mail_students')
                                ->queryAll();
                }
                elseif($single['send_to'] == 'owtrainer'){
                    $response['tbl_used'] = 'gym_owner';
                    $mailingList =Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('mail_gym_owners')
                    ->queryAll();
                }
                elseif($single['send_to'] == 'all'){
                    $response['tbl_used'] = 'all';
                    $student_list =Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('mail_students')
                    ->queryAll();
                    $ownerTrainer_list =Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('mail_gym_owners')
                    ->queryAll(); 
                    $mailingList = array_merge($student_list,$ownerTrainer_list);

                }
                foreach ($mailingList as $singleMail){
                        $this->sendMailToUsers($response,$singleMail,$subject,$html);
                }
            }
        }
        public function sendMailToUsers($response,$user,$subject,$html){
            require_once('helper/phpmailer/class.phpmailer.php');
            $from_email=Yii::app()->params['adminEmail'];
            $webName=Yii::app()->name;

            $mail    = new PHPMailer();
            $mail->Subject = $subject ;

            $total_count = 0;

            $mail->AddAddress($user['users_email']) ;

            $mail->SetFrom($from_email, $webName);
            $mail->AddReplyTo($from_email,$webName);

            $body = "".$html."";
            $mail->MsgHTML($body);
            try{
                $sent = $mail->Send();
                if($sent) {
                    if($response['tbl_used'] == 'students'){
                        $model = Students::model()->findByPk($user['user_id']);
                    }elseif($response['tbl_used'] == 'gym_owner'){
                        $model = GymTrainersOwners::model()->findByPk($user['user_id']);
                    }elseif($response['tbl_used'] == 'all'){
                        return true;
                    }
                    if($model){
                        $model->count_sent =$model->count_sent +1;
                        $model->update();
                    }
                }
            }catch(Exception $e){
            }

        }
    }

下面是我正在使用的cronjob命令:

wget http://www.example.com/controller/method
ewm0tg9j

ewm0tg9j1#

如果你用cron来调用yii,最好的方法就是使用命令。

相关问题