我想從cron作業運行此腳本。在CakePHP中添加Cron作業
我有網址爲這次行動www.abc.com/leads/generatedailyreport
我要天天跑這個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;
}
你可能想要使用類似curl來每天獲取頁面。你可能會更好地問某個地方,如[服務器故障](http://serverfault.com/);這與編程無關。 –