Automatic file transfer (daily) - php

Is it possible to automaticly download xml files from one server to another server on a daily basis with PHP?
The goal is to create a webapplication in CakePHP which makes use of an xml report that comes from a online accountingserver.
So it can be done using a cronjob? But is cron supported with PHP?
Where can i configure that cronjob?
What kind of code should i write to get the file from the accountingserver in the first place?

Yes, you can create a PHP script that will download the data from the second server and then call it from using a cron job. Cron jobs allow you to run code at specific times. More info on that here.
It would probably look something like this:
0 0 * * * php path/to/file.php
This basically tells it to run file.php at minute 0, hour 0, every day, every month.

Surely you can.
You write a PHP program that does this transfer once, when called. Then we run that program automatically every day at a fixed time by setting up a cron job.

Related

Run timed PHP function via SQL

Is there a way to run a PHP file automatically every hour?
Via SQL or some other way.
I'm currently working on a file who's job is to bring information from a table every hour.
This file needs to execute every hour.
Is there any way to do this?
Neither PHP nor MySQL has this ability.
This function is normally available either by your operating system or external tool.
Windows has a job scheduler, or Unix and variants has crontab, where you can specify a command that must be run on a periodic basis.
it is not a build-in facility. however it can be done by some external tools or using some other php code written for it and loaded on the machine
If you have cron on your host, you can use it. Cron runs php script or any other script every time you want to. It's automated so you just put time details in to it and let it do it's magics itself.
First, put your script that you want to run every hour into file. Then search cron, or crontab from your cpanel(or what are you using). Then set time details(if you have those inputs on your cpanel). Then, write to command input: php /your/full/path/here/. If you don't have GUI for setting crons(cron times), you can use this command:
0 * * * * php /your/full/path/here/
Cronjobs has been mentioned in the questions above me. And you should try to find a solution with cronjobs.
But if you can't, you can use the php time_sleep_until function.
The doc is here: http://www.php.net/manual/en/function.time-sleep-until.php
You input a timestamp in the first parameter, and the php script will sleep until given time.
And then you just while it forever. Not a good solution, but it works.
Remember to ignore user abort and set timeout to infinite.

How To Schedule Task With PHP?

How to develop a schedule task system like the one used by Wordpress to schedule post? Do they use Cron Job? If they use it, how they configure it through PKP script?
As far as I know, WordPress just sets the publish date to some time in the future. It doesn't actually cause an event to happen in the future, it just looks that way when someone views the site.
You'd need a cron job if you actually wanted to do something in the future, web servers only respond to requests made by user agents.
Use cron and in iterations get run php_cli interpreter with your script.
I use php cron tasks, they look like the following
0 0 * * * * php -f /var/www/public_html/mycronphpfile.php
This means, at midnight, every day, run the command line php and execute the specified file.

Schedule and execute a PHP script automatically

I have written a PHP script which generates an SQL file containing all tables in my database.
What I want to do is execute this script daily or every n days. I have read about cron jobs but I am using Windows. How can I automate the script execution on the server?
You'll need to add a scheduled task to call the URL.
First of all, read up here:
MS KB - this is for Windows XP.
Second, you'll need some way to call the URL - i'd recommend using something like wget - this way you can call the URL and save the output to a file, so you can see what the debug output is. You can get hold of wget on this page.
Final step is, as Gabriel says, write a batch file to tie all this up, then away you go.
e: wget is pretty simple to use, but if you have any issues, leave a comment and I'll help out.
ee: thinking about it, you don't even really need a batch file, and could just call wget directly..
add a scheduled task to request the url. either using a batch file or a script file (WSH).
http://blog.netnerds.net/2007/01/vbscript-download-and-save-a-binary-file/
this script will allow you to download binary data from a web source. Modify it to work for you particular case. This vbs file can either be run directly or executed from within a script. Alternately you do not have to save the file using the script, you can just output the contents (WScript.Echo objXMLHTTP.ResponseBody) and utilize the CMD out to file argument:
cscript download.vbs > logfile.log
save that bad boy in a .bat file somewhere useful and call it in the scheduler: http://lifehacker.com/153089/hack-attack-using-windows-scheduled-tasks
Cron is not always available on many hosting accounts.
But try this:
http://www.phpjobscheduler.co.uk/
its free, has a useful interface so you can see all the scheduled tasks and will run on any host that provides php and mysql.
You can use ATrigger scheduling service. A PHP library is also available to create scheduled tasks without overhead. Reporting, Analytics, Error Handling and more benefits.
Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.
Windows doesn't have cron, but it does come with the 'at' command. It's not as flexible as cron, but it will allow you to schedule arbitrary tasks for execution from the command line.
Yes, You can schedule and execute your php script on windows to run automatically. In linux like os u will have cron but on windows u can schedule task using task scheduler.
If your code is in remote hosted server then create a cron-job for the same.
Else if in local then use a scheduled task in windows.Its easy to implement.I am having servers with so many scheduled tasks running.

How can I run PHP script in certain interval (e.g. once a day)?

I have a php script that reads one file through http(the file is on other domain). I would like to read this file only once or twice a day, instead of connecting to it every time the website is refreshed.
Is there any other way than doing it with cron?
I dont want to use cron cause I prefer to setup this behaviour in the script itself .. so it is flexible, so I can use it anywhere without setting up cron every time.
thanks
I've done this kind of thing in the past when I didn't have access to cron:
$lastRunLog = '/path/to/lastrun.log';
if (file_exists($lastRunLog)) {
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 86400) {
//its been more than a day so run our external file
$cron = file_get_contents('http://example.com/external/file.php');
//update lastrun.log with current time
file_put_contents($lastRunLog, time());
}
}
If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.
You can also use Web Based Cron if you want to hit a site on a timed interval.
You could even use a database table - really simple in structure, id, date, script url, and whatever you need - and add a row every time you run the script.
Then, before run the script simply check the numbers of row for each day you have.
You can use a Cronjob. You can then run the php script by the command line.
php /someplace/somefile.php
The Cronjob would be the following if you update every day.
0 0 * 0 0 php /someplace/somefile.php
Since you explicitly state that you don't want to use cron, the only other way to do this (without something analogous to cron) is to set up your script as a daemon. However, unless you really need the flexibility that daemons provide, cron is much easier and simpler.
Here's one daemon walk-through.
If you're using a Linux distro with systemd:
I had a need for scheduling yearly based jobs, independent of the application (in case the system rebooted or anything like that), and I was given the suggestion to use systemd Timers. The Arch Wiki Page on it gives some examples.
If you are on a *nix environment you can use cron jobs
What's wrong with cron?
You have a couple choices with cron - your php can be invoked by the command line PHP interpreter, or you could use wget or fetch or the equivalent to invoke your PHP on the server.
In general, PHP run from within the context of the web server has a time limit on how long it can execute, so in general you can't set up "background" PHP threads to do stuff "later".

Resetting a MySQL Field value without user execution

I need to reset a MySQL Field value automatically at midnight. It is a specific column in a specific row in a table. I know how to do this in PHP but I do not know how to execute the PHP Script at midnight without someone having to do it themselves. Do you have any viable solutions?
Edit:--------------------
Preferably without using Cron Jobs.
If you are running on linux you would use a cronjob
On most distros there is a command called crontab that schedules tasks for you and a specific format you need:
0 0 * * * php /path/to/file.php
EDIT:
Wrote this before you edited yours :p I'm not sure there is any other way. Do you have a specific reason not to use a cronjob?
You may not even need to specify the php part if the php file is marked as executable i.e
0 0 * * * /path/to/file.php
Just do "chmod +x /path/to/file.php" form the linux command line
There are web based cron services too. Basically you set up an account and then they visit an URL of your choosing at a regular interval. On your server you set up a page that does what you need to get done. Preferably give it a unlikely-to-find-name like myjob789273634ahhh8s2nhw8sghusgf874wfu.php. You get the idea. (Remember that PHP-scripts timeout after like 30secs.)
Here's a google search:
**oops I'm new so I can't post URL apparently. Just search for "web based cron".
Good luck
/0
You could write a job scheduler into your program that runs jobs in a cron-like way. It would require a user to interact with the system to trigger, but it might be good enough depending on your needs. This is much more complicated than just running a cronjob, and does not ensure prefect timing (since it wont run until a user hits a page).
You'd probably need to add a table into you database that would list the job, the time you want them done, and a locking flag to avoid concurrent attempts to run the job. Each time your script runs, you'd check this table for overdue jobs and run them as needed.
Asking how to reliably set off a script at the same time every night without cron (or a scheduled task, on Windows) is like asking how to make a dynamic website without a server-side language.
If your app absolutely relies on a script running exactly at midnight, cron is a requirement. If your users have a hosting company that (stupidly) does not permit cron, they're going to be out of luck.

Categories