2014-05-10 79 views
0

我有一个用于解析xml文件的FOR循环,但我使用$ _GET参数来接收在URL中循环结束时发送的变量(meta http-equiv =“Refresh”content =“ 0; url ...)在网络浏览器中一切正常,但我想使用CRON作业自动完成,因此可以做这样的事情吗?我会非常感谢任何建议或示例在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 =变量&文件=变量&存在=变量&加入=变量

+0

HTTP甚至不参与在这里,所以当然不可能使用HTTP URL。 – CBroe

+0

你有[命令行选项](http://www.php.net/manual/en/features.commandline.options.php)(另见['getopt()'](http:// www .php.net/manual/en/function.getopt.php))或[参数](http://www.php.net/manual/en/reserved.variables.argv.php)。如果您打算在HTTP和CLI上下文中使用一个脚本,您还需要知道[您是否处于CLI模式](http://stackoverflow.com/a/933375/451969)。 –

+0

你很容易[sql注入攻击](http://bobby-tables.com) –

回答

1

如果你想传递参数给一个cron,使用

sample.php

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

...

> php sample.php apples oranges 

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