0

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

user3588722
  • 5
  • 1
  • 6
  • HTTP is not even involved here, so of course there it is not possible to use HTTP URLs. – CBroe May 10 '14 at 16:38
  • You're got [command-line options](http://www.php.net/manual/en/features.commandline.options.php) (see also [`getopt()`](http://www.php.net/manual/en/function.getopt.php)) or [the arguments](http://www.php.net/manual/en/reserved.variables.argv.php). You'll also want to know if [you're in CLI mode](http://stackoverflow.com/a/933375/451969) if you're going to use one script for both HTTP and CLI contexts. – Jared Farrish May 10 '14 at 16:45
  • You are vulnerable to [sql injection attacks](http://bobby-tables.com) – Marc B May 10 '14 at 16:46
  • i can use curl to send variables? [cpde]curl -sS 'http://example.com/script_name.php?x=VARIABLE&file=VARIABLE&exist=VARIABLE&added=VARIABLE'[/code] but how in this case get sending variable and run script again? maybe its stupic question, I'm a php beginner – user3588722 May 10 '14 at 16:48

1 Answers1

1

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
)
chiliNUT
  • 18,989
  • 14
  • 66
  • 106