简体   繁体   中英

PHP script execution using a cron job

I have a script called cronStats.php that pulls data from Apple's autoingest tool (data on app downloads, region etc) and populates a database for access later on.

When the script is executed in the browser, everything works correctly. But when using a scheduled cron job to execute the script there's an error.

Here is the relevant code:

function pullITCData($username,$password,$VND,$date) {

$fields_string = "USERNAME=" . urlencode($username);
$fields_string .= "&PASSWORD=" . urlencode($password);
$fields_string .= "&VNDNUMBER=" . $VND;
$fields_string .= "&TYPEOFREPORT=Sales";
$fields_string .= "&DATETYPE=Daily";
$fields_string .= "&REPORTTYPE=Summary";
$fields_string .= "&REPORTDATE=$date";  

$fn = "dailyStat_" . $date . "_" . $VND;
$filename = $fn . ".gz";

//$abFN = __DIR__ . "/" . $fn;
//$abFilename = __DIR__ . "/" . $filename;

$abFN = $fn;
$abFilename = $filename;

$ch = curl_init();
echo("<br>abFN url is $abFN");
echo("<br>abFilename url is $abFilename");  
$fp = fopen($abFilename, "w+");

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, 'https://reportingitc.apple.com/autoingestion.tft');
curl_setopt($ch, CURLOPT_POST, 7);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_FILE, $fp);

//execute post
$contents = curl_exec ($ch);
$fileCreated = false;

if ($contents === false) {
    echo("<br> no contents"); 
} else {
    echo("<br>contents found");
}


fclose($fp);
curl_close($ch);

//delete the opened file
if ($fileCreated == true){
    unlink("$abFN");
}

}

I've removed some code for brevity. Basically, what happens is I pass a string of variables to Apple's ingest tool, which then returns with a .txt.gz file (which is then opened and parsed, not shown here).

When browser executed, I get the "Contents Found" statement, but via Cron Job, the "No Contents" statement meaning that the CURL_EXEC is failing for some reason.

My thinking is that the .GZ file cannot be created because the Cron Job is executing from another directory (?). I tried setting an absolute URL to write the .txt.gz to, but this failed also:

[function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in

I'm at a loss as to how to proceed. Any ideas?

EDIT: thank you for your feedback, it's appreciated. Based on BojanT's cron command:

/web/cgi-bin/php5 cd "$HOME/html/path/to/php/cron/ && php cronStats.php > cron.log 2>&1

I'm getting the following error:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file

EDIT 2 - Work Around: work around method that is working for anyone in a similar situation. It's not elegant / efficient, and doesn't actually solve the issue, but for what it's worth: set up a cron job on another file: eg runCron.php:

 <?php

executeScript();

function executeScript() {
file_get_contents("http://website.com/path/to/php/cron/cronStats.php");
informOfExecution();
}

which then executes the actual file at the absolute url. informOfExecution() method sends an email to me notifying of update. it would be nice to cron job the file directly, but done is better than perfect. thanks all.

Change to your file's directory with 'cd' and then run your file. Example:

* * * * * cd /f1/f2/f3/f4/ && php thing.php >> /home/output/outputs.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM