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: e.g. 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.
echo curl_error($ch);. The error message suggest that you are trying to write to HTTP Url which is strange , can you paste a line where that error is triggered?$fp = fopen($abFilename, "w+");so you are actually trying to write to HTTP url$fp = fopen('http: //www.website.com/path/to/cron/dailyStat_20140930_12345678.gz', "w+");which is not possible. As @Dimitri suggested try to put in your croncommand: /web/cgi-bin/php5 cd $HOME/html/path/to/php/cron/ && php ronStats.php > cron.log 2>&1and set $abFileName='./dailyStat_20140930_12345678.txt'cd pathtoyourcron && /web/cgi-bin/php5 runStats.phpcommonly you should be able to output script error and std by adding ` >cron.log 2>&1` so try first one and if it works trycd pathtoyourcron && /web/cgi-bin/php5 runStats.php >cron.log 2>&1hope this will work