dtvgo28624 2014-05-10 16:22
浏览 40
已采纳

在CRON作业中获取参数

I have a FOR loop for parsing xml file as parts, but I using a $_GET paramets to receive variable sending at the end of loop in URL (meta http-equiv="Refresh" content="0; url...). In Web browser everything works fine but I want to use CRON job to do it automatically. So it is possible to do something like this? I'll be very grateful for any advice or example

My code:

    $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++;

}

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

  • 写回答

1条回答 默认 最新

  • dqc19941228 2014-05-10 16:51
    关注

    If you want to pass parameters to a cron, use $argv

    sample.php

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

    ...

    > php sample.php apples oranges
    
    Array
    (
        [0] => sample.php
        [1] => apples
        [2] => oranges
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算