I'm working on a simple script to choose best server based. The script works perfeclty on my computer but crontab is not working as expected.
Here is my crontab rule:
* * * * * curl -s 'https://my-domain.com/server_status.php'
Here is my: server_status.php
<?php
if (isset($_POST['server_cdn'])) {
if (!empty($_POST['server_cdn'])) {
$path = '../player/server.txt';
$f = fopen($path, "w+");
fwrite($f, $_POST['server_cdn']);
fclose($f); chmod($path, 0777);
exit();
}
}
?>
<script>
$.getJSON("/player/servers.json", function(json) {
var script_name = json.name;
var script_version = json.version;
var prefix = json.prefix;
var _svrs = Object.keys(json.servers).length;
var mach_available = {};
for(i = 1; i <= _svrs; i++) {
if (json.servers['server'+i].status == 1) {
$.ajax({
url: "https://cdn" + json.servers['server'+i].cdn + "." + json.prefix + "/" + json.file,
type: 'POST',
async: false,
cache: false,
timeout: 7000,
error: function(){ mach_available['cdn' + json.servers['server'+i].cdn] = null; },
success: function(response){
mach_available['cdn' + json.servers['server'+i].cdn] = parseInt(response);
}
});
} else {
mach_available['cdn' + json.servers['server'+i].cdn] = null;
}
}
var min = Object.entries(mach_available).sort(function(x,y) {return x[1]-y[1]})[0];
$.ajax({
type: 'POST',
url: '/player/server_status.php',
data: {"server_cdn": min[0]},
async: true,
success: function() { }
});
});
</script>
My javascript will load a .json file and verify some factors, after this, will get a data from external URL and call server_status.php file with a $_POST data, the script will detect that $_POST exists and will create a file on server-side. Everything is working on my computer, but crontab is not creating the file at server-side.
I guess that the problem is not related to PHP, only Javascript, I don't know what is exactly happening. Can you help me solve this issue?
Thank you very much.