【问题标题】:GET paramets in CRON job获取 CRON 作业中的参数
【发布时间】:2014-05-11 00:51:04
【问题描述】:

我有一个用于将 xml 文件解析为部分的 FOR 循环,但我使用 $_GET 参数在 URL 中的循环结束时接收变量发送 (meta http-equiv="Refresh" content="0; url.. .). 在 Web 浏览器中一切正常,但我想使用 CRON 作业自动完成。所以可以做这样的事情吗?我将非常感谢任何建议或示例

我的代码:

    $name = 'tmp_add_xml';
if(isset($_GET['file'])){
    $xmlfile = 'import/'.$_GET['file'].'.xml';
} else {
    $xmlfile = 'import/'.$name.'.xml';
    $xmlurl = 'URL_XML_FILE';

    $downxml = file_get_contents($xmlurl);
    file_put_contents($xmlfile, $downxml); 
}

$xml = simplexml_load_file($xmlfile);
$xml_offer = $xml->xpath("//offer");
$total = count($xml_offer);

if(isset($_GET['x']) && isset($_GET['exist']) && isset($_GET['added'])){
    $x = $_GET['x'];
    $product_exist = $_GET['exist'];
    $product_added = $_GET['added'];
} else {
    $x = $product_exist = $product_added = 0;
}
$limit = $x+1000;
for($z = $x; $z <= $limit; $z++){
    $productid = $xml_offer[$z]->id;

    if(mysql_num_rows(mysql_query("SELECT reference FROM ps_product WHERE reference = '".$productid."'")) > 0){ 
        $product_exist++;
    } else {
        $product_added++;
    }

    if($z==$total){
        echo 'Import 100%';
        exit;
    } elseif($z==$limit){
        print '<meta http-equiv="Refresh" content="0; url='.$_SERVER['PHP_SELF'].'?x='.($x+1).'&file='.$name.'&exist='.$product_exist.'&added='.$product_added.'">';
        exit;
    }
    $x++;

}

网址: script_name.php?x=VARIABLE&file=VARIABLE&exist=VARIABLE&added=VARIABLE

【问题讨论】:

  • 这里甚至没有涉及HTTP,所以当然不可能使用HTTP URL。
  • 你有command-line options(另见getopt())或the arguments。如果您打算对 HTTP 和 CLI 上下文使用一个脚本,您还想知道 you're in CLI mode
  • 你很容易受到sql injection attacks
  • 我可以使用 curl 发送变量吗? [cpde]curl -sS 'example.com/… 但在这种情况下如何获取发送变量并再次运行脚本?也许这是一个愚蠢的问题,我是一个 php 初学者

标签: php cron


【解决方案1】:

如果要将参数传递给 cron,请使用 $argv

sample.php

<?php
print_r($argv);
exit;
?>

...

> php sample.php apples oranges

Array
(
    [0] => sample.php
    [1] => apples
    [2] => oranges
)

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2022-05-20
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多