douao3063 2016-06-23 11:11
浏览 40

用PHP参数的PHP cron作业

Hello i have following script that i want to execute :

30 12 * * * /usr/bin/php -q /var/www/dev/create_report.php type=NR date=$(date +\%Y\%m\%d --date='2 days')

But im still getting following messages :

PHP Notice:  Undefined index: type in /var/www/dev/create_report.php on line 18
enter code here`PHP Notice:  Undefined index: date in /var/www/dev/create_report.php on line 28
PHP Notice:  Undefined offset: 2 in /var/www/dev/create_report.php on line 30
PHP Notice:  Undefined offset: 1 in /var/www/dev/create_report.php on line 30

PHP Notice:  Undefined offset: 1 in /var/www/dev/create_report.php on line 31
PHP Notice:  Undefined offset: 2 in /var/www/dev/create_report.php on line 31
PHP Notice:  Undefined index: type in /var/www/dev/create_report.php on line 41
PHP Notice:  Undefined variable: result in /var/www/dev/create_report.php on line 89
PHP Warning:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /var/www/dev/create_report.php on line 89

Can anybody help me please ? Im getting sick of this.

Thanks in advance.

UPDATE #1 code of create_report.php

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
 </head>
 <body>
<?php
ini_set('max_execution_time', 300);
require_once 'Classes/PHPExcel.php';
//PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
// pripojenie sa k dtb
$mysqli = new mysqli('192.168.0.20', 'mfric', 'mFRIC6389mf', 'asterisk');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
switch ($_GET['type']){
    case 'BB' : $typ = "Banska_Bystrica";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWEBB%' or campaign_id like 'RWEAKVBB';");break;
    case 'TN' : $typ = "Trencin";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWETN%';");break;
    case 'NR' : $typ = "Nitra";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWENR%';");break;
    //case 'ZA' : $typ = "Liptovsky_Mikulas";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWEZA_RK' or campaign_id like 'RWEZA_LM';");break;
    case 'ZAA' : $typ = "Zilina";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWEZA%' or campaign_id like 'RWEZA_LM' or campaign_id like 'RWEZA_RK';");break;
    case 'TT' : $typ = "Trnava";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWETT%';");break;        
    case 'BA' : $typ = "Bratislava";$result =mysqli_query($mysqli, "select tabulka from creator_db where campaign_id like 'RWEBA%';");break;        
    default : $typ = "N/A";
}
$datum=$_GET['date'];
$datum2=explode("-",$datum);
$datum_c2 = $datum2[2].".".$datum2[1].".".$datum2[0];
$datum_sheetname = $datum2[0].$datum2[1].$datum2[2];
// inicializacia objektu
$objPHPExcel = new PHPExcel();
//nastavenie parametrov 
$objPHPExcel->getProperties()->setCreator("eCall Slovakia");
//$objPHPExcel->setLastModifiedBy("eCall Slovakia");
//$objPHPExcel->setTitle("eCall Slovakia - report RWE AUDIT NR");
//$objPHPExcel->setSubject("eCall Slovakia - report RWE AUDIT NR");
// inicializacia "sheetu" prveho
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('RWE audit '.$_GET['type'].' '.$datum_sheetname);
//nastavenie rowcount na = 1 / zacinam prvym riadkom;
$rowCount = 5;
//nastavenie autosize na vsetky stlpce
foreach(range('D','Q') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
        ->setAutoSize(true);
}
foreach(range('A','B') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
        ->setAutoSize(true);
}

$objPHPExcel->getActiveSheet()->getColumnDimension('C')
        ->setWidth('15');
$objPHPExcel->getActiveSheet()->getColumnDimension('K')
        ->setWidth('17')
        ->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')
        ->setWidth('10')
        ->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('M')
        ->setWidth('15')
        ->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')
        ->setWidth('15')
        ->setAutoSize(false);
$objPHPExcel->getActiveSheet()->SetCellValue('A1',"eCall Slovakia / RWE - audit - report zaujmov");
$objPHPExcel->getActiveSheet()->SetCellValue('A2',"Dátum : ".$datum_c2);
$objPHPExcel->getActiveSheet()->SetCellValue('A4',"IMK");
$objPHPExcel->getActiveSheet()->SetCellValue('B4',"Okres");
$objPHPExcel->getActiveSheet()->SetCellValue('C4',"Telefónne číslo");
$objPHPExcel->getActiveSheet()->SetCellValue('D4',"Meno");
$objPHPExcel->getActiveSheet()->SetCellValue('E4',"Priezvisko");
$objPHPExcel->getActiveSheet()->SetCellValue('F4',"Ulica");
$objPHPExcel->getActiveSheet()->SetCellValue('G4',"Číslo");
$objPHPExcel->getActiveSheet()->SetCellValue('H4',"Mesto");
$objPHPExcel->getActiveSheet()->SetCellValue('I4',"PSČ");
$objPHPExcel->getActiveSheet()->SetCellValue('J4',"Poschodie");
$objPHPExcel->getActiveSheet()->SetCellValue('K4',"Kontakt pre audítora");
$objPHPExcel->getActiveSheet()->SetCellValue('L4',"Volal agent - osobné číslo");
$objPHPExcel->getActiveSheet()->SetCellValue('M4',"Volal - dátum");
$objPHPExcel->getActiveSheet()->SetCellValue('N4',"Dátum stretnutia:");
$objPHPExcel->getActiveSheet()->SetCellValue('O4',"Čas stretnutia:");
$objPHPExcel->getActiveSheet()->SetCellValue('P4',"email");
$objPHPExcel->getActiveSheet()->SetCellValue('Q4',"Poznámky");
//nacitanie databaz a nasledne vytiahnutie potrebnych dat
$i=0;
while ($row = mysqli_fetch_assoc($result))
   {
    $pole[$i] = $row['tabulka'];
    $i++;
   }
$pocet = $i;
mysqli_close($mysqli);
$mysqli = new mysqli('192.168.0.20', 'mfric', 'mFRIC6389mf', 'asterisk');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
for($j=0;$j<$pocet;$j++){
    $mysqli->set_charset("utf8"); 
    $result = mysqli_query($mysqli, "select * from ".$pole[$j]." join vicidial_list on ".$pole[$j].".lead_id=vicidial_list.lead_id where (vicidial_list.status='ZP1' or vicidial_list.status='NZX') and left(".$pole[$j].".au_datum_a_stretnutie,10)='".$datum."';");
    while ($row = mysqli_fetch_assoc($result))
   {
    $okres = str_replace("Bánska","Banská",$row['slct_okres']); 
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,$row['slct_kraj']);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,$okres);
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,$row['tel']);
    $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,$row['meno']);
    $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,$row['priezvisko']);
    $objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount,$row['ulica']);
    $objPHPExcel->getActiveSheet()->SetCellValue('G'.$rowCount,$row['popisne_cislo']);
    $objPHPExcel->getActiveSheet()->SetCellValue('H'.$rowCount,$row['mesto']);
    $objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowCount,$row['psc']);
    $objPHPExcel->getActiveSheet()->SetCellValue('J'.$rowCount,$row['au_poschodie']);
    $objPHPExcel->getActiveSheet()->SetCellValue('K'.$rowCount,$row['au_kontakt_kv']);
    $objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowCount,$row['kod_predajcu']);
    $objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount,$row['last_local_call_time']);
    $objPHPExcel->getActiveSheet()->SetCellValue('N'.$rowCount,$row['au_datum_a_stretnutie']);
    $objPHPExcel->getActiveSheet()->SetCellValue('O'.$rowCount,$row['au_cas_a_stretnutie']);
    $objPHPExcel->getActiveSheet()->SetCellValue('P'.$rowCount,$row['email']);
    $objPHPExcel->getActiveSheet()->SetCellValue('Q'.$rowCount,$row['poznamka_klient']);
    $rowCount++;
   }
}

// stylovanie ------------------------------------------------------------------------------------
$styleArray = array(
  'borders' => array(
    'allborders' => array(
      'style' => PHPExcel_Style_Border::BORDER_MEDIUM
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('A4:Q4')->applyFromArray($styleArray);
unset($styleArray);


$styleArray = array(
  'borders' => array(
    'allborders' => array(
      'style' => PHPExcel_Style_Border::BORDER_DOTTED
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('A5:Q'.($rowCount-1))->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'font'  => array(
        'bold'  => false,
        'color' => array('rgb' => '000000'),
        'size'  => 11,
        'name'  => 'Calibri'
    )
);

$objPHPExcel->getActiveSheet()->getStyle('A5:Q'.($rowCount-1))->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 11,
        'name'  => 'Calibri'
    )
);

$objPHPExcel->getActiveSheet()->getStyle('A4:Q4')->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 16,
        'name'  => 'Arial'
    )
);

$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 14,
        'name'  => 'Arial'
    )
);

$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'borders' => array(
    'left' => array(
      'style' => PHPExcel_Style_Border::BORDER_MEDIUM
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('A5:A'.($rowCount-1))->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'borders' => array(
    'right' => array(
      'style' => PHPExcel_Style_Border::BORDER_MEDIUM
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('Q5:Q'.($rowCount-1))->applyFromArray($styleArray);
unset($styleArray);

$styleArray = array(
  'borders' => array(
    'bottom' => array(
      'style' => PHPExcel_Style_Border::BORDER_MEDIUM
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('A'.($rowCount-1).':Q'.($rowCount-1))->applyFromArray($styleArray);
unset($styleArray);

$objPHPExcel->getActiveSheet()->getStyle('M5:M50')
    ->getNumberFormat()
    ->setFormatCode('yyyy-mm-dd');
//-------------------------------------------------------------------------------------------------------------------------------------------   
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save("rwe/report/eCall Slovakia - RWE - audit -  ".$typ." - report - zaujmy - ".$datum_sheetname.".xlsx" );
// ODOSIELANIE EMAILOM
$yourName = 'TEST';
$yourEmail_2 = 'martina.lunzerova@ecallslovakia.sk';
ini_set("include_path", "Classes/PHPMailer");
require("class.phpmailer.php");
$mail = new PHPMailer();

$mail->From     = 'rwereport@ecallslovakia.sk';
$mail->FromName = 'eCall Slovakia - RWE';
$mail->AddAddress($yourEmail_2);
$mail->AddCC('martin.fric@ecallslovakia.sk');
//$mail->AddCC('katarina.kralova@ecallslovakia.sk');
$mail->AddAttachment("rwe/report/eCall Slovakia - RWE - audit -  ".$typ." - report - zaujmy - ".$datum_sheetname.".xlsx", "eCall Slovakia - RWE - audit -  ".$typ." - report - zaujmy - ".$datum_sheetname.".xlsx");
$mail->WordWrap = 50;                              // set word wrap
$mail->IsHTML(true);  
$mail->CharSet = 'UTF-8';                             // send as HTML
$mail->Subject  =  'RWE - audit - report zaujmov - '.$typ.' '.$datume_sheetname;
$mail->Body     =  'Dobrý deň,<br/><br/>v prilohe Vám zasielame report záujmov z telemarketingovej kampane RWE<br/>-⁠ audit, ktorý je spracovávaný spoločnosťou eCall Slovakia.<br/><br/>Mesto:Nitra<br/>Na dátum:'.$datum_sheetname.'<br/></br>S pozdravom,<br/>';
//$mail->AltBody  =  'Dobrý deň,<br/><br/>v prilohe Vám zasielame report záujmov z telemarketingovej kampane RWE<br/>-⁠ audit, ktorý je spracovávaný spoločnosťou eCall Slovakia.<br/><br/>Mesto:Nitra<br/>Na dátum:'.$datum_sheetname.'<br/></br>S pozdravom,<br/>';

if ( $mail->Send()){
   ?><script>alert('Mail bol odoslany');</script><?
}

?>
</body>
</html>

UPDATE #2

PHP Notice:  Undefined offset: 1 in /var/www/dev/create_report.php on line 10
array(3) {
  ["/var/www/dev/create_report.php"]=>
  NULL
  ["type"]=>
  string(2) "NR"
  ["date"]=>
  string(10) "2016-06-25"
}
PHP Notice:  Undefined index: type in /var/www/dev/create_report.php on line 24
PHP Notice:  Undefined index: date in /var/www/dev/create_report.php on line 34
PHP Notice:  Undefined offset: 2 in /var/www/dev/create_report.php on line 36
PHP Notice:  Undefined offset: 1 in /var/www/dev/create_report.php on line 36
PHP Notice:  Undefined offset: 1 in /var/www/dev/create_report.php on line 37
PHP Notice:  Undefined offset: 2 in /var/www/dev/create_report.php on line 37
PHP Notice:  Undefined index: type in /var/www/dev/create_report.php on line 47
PHP Notice:  Undefined variable: result in /var/www/dev/create_report.php on line 95
PHP Warning:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /var/www/dev/create_report.php on line 95
PHP Fatal error:  Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file rwe/report/eCall Slovakia - RWE - audit -  N/A - report - zaujmy - .xlsx.' in /var/www/dev/Classes/PHPExcel/Writer/Excel2007.php:389
Stack trace:
#0 /var/www/dev/create_report.php(244): PHPExcel_Writer_Excel2007->save('rwe/report/eCal...')
#1 {main}
  thrown in /var/www/dev/Classes/PHPExcel/Writer/Excel2007.php on line 389

UPDATE #3

    <!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
 </head>
 <body>
<?php
for($i=0; $i<$argc; $i++){//iterate over one parameter at a time
    list($key, $val) = explode('=', $argv[$i]);//explode parameter at =
    $parameter[$key]=$val;//make kvp pair in $parameter
}

var_dump($parameter);
ini_set('max_execution_time', 300);
require_once 'Classes/PHPExcel.php';
//PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
// pripojenie sa k dtb
$mysqli = new mysqli('192.168.0.20', 'mfric', 'mFRIC6389mf', 'asterisk');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
} 

...

  • 写回答

2条回答 默认 最新

  • douqiang1910 2016-06-23 11:20
    关注

    Check how you retreive the cli arguments, passed to the php script.

    For testing purpose add this to your script:

    var_dump($argc); //will print number of arguments passed 
    var_dump($argv); //argument itself
    

    Then add a "logging" to your cron table.

    30 12 * * * /usr/bin/php -q /var/www/dev/create_report.php type=NR date=$(date +\%Y\%m\%d --date='2 days') &>/home/your_user/cron.log 
    

    This way the output will be written to /home/your_user/cron.log and you will see what is passed as argument.

    To actually get the values I would run over the $argc counter and explode the values on = to get a kvp pair.

    list($key, $val) = explode('=', $argv[1]);
    var_dump(array($key=>$val));
    

    for your Update #1

    Your script contains html and php code. So when you call your script with php /var/www/dev/create_report.php it will always output the html code unterpreted. Think about that and if you really want it this way. Outputting html code to cli is pretty ugly. But it's possbile. What's broken with your code is the $_GET usage. This won't work in cli scripts as I mentioned above.

    $parameter = []; //create a array that will hold parameter
    for($i=1; $i<$argc; $i++){//iterate over one parameter at a time
        list($key, $val) = explode('=', $argv[$i]);//explode parameter at =
        $parameter[$key]=$val;//make kvp pair in $parameter
    }
    
    var_dump($parameter);
    

    This will show

    **array(2) {
      'type' =>
      string(2) "NR"
      'date' =>
      string(8) "20160625"
    }**
    

    Now you can access your cli parameter as you want. Just use $datum=$parameter['date']; instead of $datum=$_GET['date'];

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂