2014-02-26 47 views
-2

我想从cron作业运行此脚本。在CakePHP中添加Cron作业

我有网址为这次行动www.abc.com/leads/generatedai​​lyreport

我要天天跑这个URL被打,使我PROGRAME运行。请帮忙。

public function generatedailyreport() { 

    $this->Lead->unbindModel(array('belongsTo' => array('Country','State','City','Course')), true); 
    $currentDate=date('Y-m-d 00:00:00'); 
    $previousDate=date('Y-m-d 00:00:00',strtotime('-1 day')); 

    // find Each Unniverty 
    // Find all the university 
    Controller::loadModel('University'); 
    $University = new University(); 
    $UniversityArray=$University->find('all',array(
     'conditions'=>array('University.status'=>1), 
     'recursive'=>-1)); 

    //First Row of the xls file 
    $listDetails[]=array('Name','Email Address','Contact Number','Query Text','University Name','Source','Created Date'); 
    $i=1; 
    foreach($UniversityArray as $value) 
    { 

     $UniversityId=$value['University']['id']; 
     $university_name=$value['University']['university_code']; 
     $universityName=$value['University']['university_name']; 
     $leadsDetails=array(); 
     $leadsDetails=$this->Lead->find('all',array(
      'conditions'=>array(
           'Lead.created BETWEEN DATE_SUB(CURDATE(), INTERVAL '.DAY_INTERVAL.' DAY) AND CURDATE() ', 
           //'Lead.created BETWEEN "'.$currentDate.'" AND "'.$previousDate.'"', 
           'Lead.master_lead_type_id'=>0, 
           'Lead.university_id'=>$UniversityId, 
        ), 
      'fields' => array('COUNT(*) as TotalLeads','University.university_name','Lead.created'), 
      'group' => array('Lead.university_id'), 
      'recursive'=>1 
      )); 

     //pr($leadsDetails); 

     if(!empty($leadsDetails)){ 
      $totalLeads=$leadsDetails[0][0]['TotalLeads']; 
      $AssignedUniversityName=$leadsDetails[0]['University']['university_name']; 
      $QueryDate=date('d M, Y',strtotime($leadsDetails[0]['Lead']['created'])); 

      //Total Report Array 
      $reportTotalArray[]=array(
       'University_Name'=>$AssignedUniversityName, 
       'Total Lead'=>$totalLeads, 
       'Date'=>$QueryDate, 
       ); 
     }else{ 
      $totalLeads=''; 
      $AssignedUniversityName=''; 
      $QueryDate=''; 

     } 

     if($totalLeads >0){ 
     $leadsFullDetails=array(); 
     $leadsFullDetails=$this->Lead->find('all',array(
      'conditions'=>array(
           'Lead.created BETWEEN DATE_SUB(CURDATE(), INTERVAL 10 DAY) AND CURDATE() ', 
           //'Lead.created BETWEEN "'.$currentDate.'" AND "'.$previousDate.'"', 
           'Lead.master_lead_type_id'=>0, 
           'Lead.university_id'=>$UniversityId, 
        ), 
      'fields' => array('Lead.first_name', 
           'Lead.email_address', 
           'Lead.contact_number', 
           'Lead.query_text', 
           'Lead.registered_from_source', 
           'University.university_name', 
           'Lead.created'), 
      'recursive'=>1 
      )); 

     // Creating XLS File and send it to Email 
     // Find all the leads from respective university 
     //pr($leadsFullDetails); 
     if(!empty($leadsFullDetails)) 
     { 
      foreach($leadsFullDetails as $data) 
      { 
       $listDetails[]=array(
          $data['Lead']['first_name'], 
          $data['Lead']['email_address'], 
          $data['Lead']['contact_number'], 
          $data['Lead']['query_text'], 
          $data['University']['university_name'], 
          $data['Lead']['registered_from_source'], 
          $data['Lead']['created'], 
          ); 
      } 
      //Make an array of all Leads 
      $allLeadsArray[$universityName]=$listDetails; 

      $filename='Lead_query_'.$university_name.'_'.time().'-'.date('y-m-d',strtotime($data['Lead']['created'])).'.xls'; 
      $fp = fopen('../webroot/files/'.$filename, 'w'); 
      $fileNamexls='../webroot/files/'.$filename; 
      foreach ($listDetails as $fields) 
      { 
       fputcsv($fp, $fields, "\t", '"'); 
      } 
      $listDetails[]=array('Name','Email Address','Contact Number','Query Text','University Name','Source','Created Date'); 
      fclose($fp); 

      // Sending Emails with attachement 
      $message="Total Lead Query: ".$totalLeads."\n\r"; 
      $message_2 ="Assigned University Name: ".$AssignedUniversityName; 
      $message_3 ="Query Date: ".$QueryDate; 
      $this->set('message',$message); 
      $this->set('message_2',$message_2); 
      $this->set('message_3',$message_3); 

      if($UniversityId==1){ 
       $toEmail=UOL_EIILM_EMAIL; 
       $bccEmailArray=BCC_EMAIL; 
       global $ccArrayEIILM; 
       $ccArray=$ccArrayEIILM; 

      }else if($UniversityId==2){ 
       $toEmail=UOL_JRU_EMAIL; 
       $bccEmailArray=BCC_EMAIL; 
       global $ccArrayJRU; 
       $ccArray=$ccArrayJRU; 

      }else if($UniversityId==3){ 
       $toEmail=UOL_RAITECH_EMAIL; 
       $bccEmailArray=BCC_EMAIL; 
       global $ccArrayRAITECH; 
       $ccArray=$ccArrayRAITECH; 

      }else if($UniversityId==4){ 
       $toEmail=UOL_NIILM_EMAIL; 
       $bccEmailArray=BCC_EMAIL; 
       global $ccArrayNIILM; 
       $ccArray=$ccArrayNIILM; 

      }else if($UniversityId==5){ 
       $toEmail=UOL_RAI_EMAIL; 
       $bccEmailArray=BCC_EMAIL; 
       global $ccArrayRAI; 
       $ccArray=$ccArrayRAI; 

      }else if($UniversityId==6){ 
       $toEmail=UOL_IUC_EMAIL; 
       $bccEmailArray=BCC_EMAIL; 
       global $ccArrayIUC; 
       $ccArray=$ccArrayIUC; 

      } 

      $this->Email->to =$toEmail; 
      $this->Email->cc = array($ccArray); 
      $this->Email->bcc = array($bccEmailArray); 
      $this->Email->subject = 'Daily Query Report'; 
      $this->Email->replyTo = '[email protected]'; 
      $this->Email->from ='[email protected]'; 
      $this->Email->template = 'daily_report'; // note no '.ctp' 
      $this->Email->attachments =array($fileNamexls); 
      $this->Email->sendAs = 'html'; 
      $this->Email->send(); 
      $message=''; 
      $totalLeads=''; 
      $AssignedUniversityName=''; 
      $QueryDate=''; 
      $fileNamexls=''; 
      $ccArray=''; 
      $toEmail=''; 
      $listDetails=''; 

     } 
    } 

    $i++; 
    } 
    pr($allLeadsArray); 

    // Send Final Email to Mr, Rai 
    //pr($reportTotalArray); 

    $message=''; 
    if(!empty($reportTotalArray)){ 
    foreach($reportTotalArray as $reportData){ 
     $message.="Assigned University Name: ".$reportData['University_Name']; 
     $message.="<br/>"; 
     $message.="Total Lead Query: ".$reportData['Total Lead']; 
     $message.="<br/>"; 
     $message.="Query Date: ".$reportData['Date']; 
     $message.="<br/>"; 
     $message.="<br/><br/><br/><br/>"; 
    } 
    //echo $message; 
    $this->set('totalReport',$message); 


    $leadsTotalFullDetails=$this->Lead->find('all',array(
      'conditions'=>array(
           'Lead.created BETWEEN DATE_SUB(CURDATE(), INTERVAL 10 DAY) AND CURDATE() ', 
           //'Lead.created BETWEEN "'.$currentDate.'" AND "'.$previousDate.'"', 
           'Lead.master_lead_type_id'=>0 
        ), 
      'fields' => array('Lead.first_name', 
           'Lead.email_address', 
           'Lead.contact_number', 
           'Lead.query_text', 
           'Lead.registered_from_source', 
           'University.university_name', 
           'Lead.created'), 
      'recursive'=>1 
      )); 


     // Creating XLS File and send it to Email 
     // Find all the leads from respective university 
     //pr($leadsFullDetails); 
     if(!empty($leadsTotalFullDetails)) 
     { 

      foreach($allLeadsArray as $key=>$dataAll){ 
       $listAllDetailss[]=array($key,'','Total Lead-',count($dataAll),'','','',''); 
       $listAllDetailss[]=array('Name','Email Address','Contact Number','Query Text','University Name','Source','Created Date'); 
       foreach($dataAll as $leadsData){ 
        $listAllDetailss[]=$leadsData; 
       } 
       $listAllDetailss[]=array('','','','','','',''); 
       $listAllDetailss[]=array('','','','','','',''); 
       $listAllDetailss[]=array('','','','','','',''); 
      } 

      //$listAllDetails[]=array('Name','Email Address','Contact Number','Query Text','University Name','Source','Created Date'); 
      /* 
      foreach($leadsTotalFullDetails as $datas) 
      { 

      $listAllDetails[]=array(
          $datas['Lead']['first_name'], 
          $datas['Lead']['email_address'], 
          $datas['Lead']['contact_number'], 
          $datas['Lead']['query_text'], 
          $datas['University']['university_name'], 
          $datas['Lead']['registered_from_source'], 
          $datas['Lead']['created'], 
          ); 
      } 
      */ 
      $allfilename='LeadAllReport_'.time().'-'.date('y-m-d',strtotime($data['Lead']['created'])).'.xls'; 
      $fpp = fopen('../webroot/files/'.$allfilename, 'w'); 
      $fileNamexls='../webroot/files/'.$allfilename; 
      foreach ($listAllDetailss as $fieldss) 
      { 
       fputcsv($fpp, $fieldss, "\t", '"'); 
      } 

      fclose($fpp); 

     $this->Email->to =ALL_TO_EMAIL; 
     $this->Email->cc = array(ALL_TO_CC); 
     $this->Email->bcc = array(ALL_TO_BCC); 
     $this->Email->subject = 'Daily Query Report'; 
     $this->Email->replyTo = '[email protected]'; 
     $this->Email->from ='[email protected]'; 
     $this->Email->template = 'daily_report_total'; // note no '.ctp' 
     $this->Email->attachments =array($fileNamexls); 
     $this->Email->sendAs = 'html'; 
     $this->Email->send(); 

     // Delete All files after sending on emails 
     echo "$i Mail Sent."; 
    } 
} 
    die; 

} 
+0

你可能想要使用类似curl来每天获取页面。你可能会更好地问某个地方,如[服务器故障](http://serverfault.com/);这与编程无关。 –

回答

1

这不是一个cakePHP工作。您可以使用您的cpanelhosting。在做你cpanel找到crond jobsscheduled tasks ..从那里你必须把你的期望file path并适当timingscronjob .. ,如果你的软管提供商犯规提供cron作业比你可以使用putty创建cronjobs ..

http://php-opensource-help.blogspot.in/2010/03/how-to-set-up-cron-job-file-using-putty.html

让我简单介绍一下。

它不事的天气里cron作业是写在corephp或CakePHP的.. cron作业的计划任务自动火从在其上设定特定的cronjob所需的时间服务器..

你需要向您的托管服务提供商询问您是否想在您的服务器上登录setup a cron job。他们会引导你进一步...

+0

没有来自cron jon,我们无法找到任何网页对于此URL www.abc.com/leads/generatedai​​lyreport 我想要这个URL,让我知道有什么方法每天自动点击这个URL – user3251674

+0

@ user3251674对不起。 。我没有得到你... –

+0

当我点击一个URL“www.mywebsite.com/leads/generatedai​​lyreport” 这将发送所有每日报告到不同的不同的电子邮件地址,现在我想运行这个脚本使用cron作业,由于CAKE PHP我不能设置成这个URL的cron,因为cron需要至少一个带扩展名.php的文件,现在让我知道什么是方法,以便我可以将这个URL设置为cron。 – user3251674

4

使用Shell脚本。 您不希望机器人或其他人直接在URL中访问您的cron。

App/Console/Command中创建壳。

如果你有ssh访问,使用crontab,你需要添加类似:

0 9 * * * /var/www/site.com/vendors/cakeshell myShell -app myApp 

More info here

+0

请让我知道销售脚本,我使用的是CAKE PHP 2.3.0 – user3251674

+1

你能查看这个页面http://book.cakephp.org/2.0/en/console-and-shells.html并设置脚本作为cron?然后尝试将其添加到crontab?我不能给出一个更清晰的例子,除了正式文件 – cornelb

+0

我已经看过他们的文件,但可以写剧本。 – user3251674

0

HELLO FRND您可以轻松地在蛋糕PHP创建的cron乔恩

 class HelloShell extends AppShell 
     { 
     public $uses=array('Jobs','JobFeeds','JobMapping','User'); 

      public function main() 
      { 
       $this->out('Hello world.'); 
      } 

    } 

这是我放在app/console/command/HelloShell.php中的文件

我在我的ubantu终端有命令。 $ CD /选择/ LAMPP/htdocs中/ JobFinder /应用/比进入

 /opt/lampp/htdocs/JobFinder/app$ crontab -e 

这将打开这样

 */1 * * * * cd /opt/lampp/htdocs/JobFinder/app && Console/cake hello $ 

    # Edit this file to introduce tasks to be run by cron. 
    # 
    # Each task to run has to be defined through a single line 
    # indicating with different fields when the task will be run 
    # and what command to run for the task 
    # 
    # To define the time you can provide concrete values for 
    # minute (m), hour (h), day of month (dom), month (mon), 
    # and day of week (dow) or use '*' in these fields (for 'any').# 
    # Notice that tasks will be started based on the cron's system 
    # daemon's notion of time and timezones. 
    # 
    # Output of the crontab jobs (including errors) is sent through 
    # email to the user the crontab file belongs to (unless redirected). 
    # 
    # For example, you can run a backup of all your user accounts 
    # at 5 a.m every week with: 
            [ Unknown Command ] 
    ^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos 
    ^X Exit  ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell 
已设置的定时器

命令行要完成

什么任务
public function insert() 
    { 
     App::import('Utility', 'Xml'); 
     // $this->loadModel('Jobs'); 
     $this->Jobs->deleteAll(array('1 = 1')); 

     $AllUrls=$this->JobFeeds->find('all'); 
     if(is_array($AllUrls) && count($AllUrls)>0) 
     { 
      foreach($AllUrls as $urls) 
      { 
       $xml = Xml::build($urls['JobFeeds']['url']); 
       //echo "<pre>";print_r($xml);die; 
       $cat_id=$urls['JobFeeds']['job_category_id']; 
       $feed_id=$urls['JobFeeds']['id']; 
       foreach($xml as $items) 
       { 
        foreach($items->item as $job) 
        { 

         App::import('Model', 'JobMapping'); 
         $this->JobMapping = new JobMapping(); 
         $AllData=$this->JobMapping->find('all',array('conditions'=>array('JobMapping.job_feeds_id'=>$feed_id))); 

         App::import('Model', 'Jobs'); 
         $this->Jobs = new Jobs(); 
         foreach($AllData as $allData) 
         { 
          $data['title']  =$job->$allData['JobMapping']['title']; 
          $data['description']=$job->$allData['JobMapping']['description']; 
          $data['link']  =$job->$allData['JobMapping']['link']; 
          $data['guid']  =$job->$allData['JobMapping']['guid']; 
          $data['pubDate'] =$job->$allData['JobMapping']['pubDate']; 
          $data['Updated_date']= date("Y-m-d H:i:s"); 
          $data['inserted_date']= date("Y-m-d H:i:s"); 
          $data['cat_id']  =$cat_id; 
          //echo "<pre>";print_r($data); 
          $this->Jobs->save($data); 
         } 
        } 
       } 
      } 
     } 


    } 
在此代码我已经在数据库插入1分钟内的数据

(将自动更新我的我的作业表enter code here

一个与shell相关的常见事情是使其作为一个cronjob运行,以便在一段时间内清理数据库或发送新闻简报。这是安装,例如:

*/5 * * * * cd /full/path/to/app && Console/cake myshell myparam 
# * * * * * command to execute 
# │ │ │ │ │ 
# │ │ │ │ │ 
# │ │ │ │ \───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, 
    | | | |   or use names) 
# │ │ │ \────────── month (1 - 12) 
# │ │ \─────────────── day of month (1 - 31) 
# │ \──────────────────── hour (0 - 23) 
# \───────────────────────── min (0 - 59)