How do I schedule an API call and database update - php

I have a php web app and have written a class that effectively invokes an API which requests data from our 3rd party membership system database. The API returns the data and saves the XML into a file (as it's a very large data set). This xml file is then parsed and I upload some of that data into my web application's database.
It takes approximately 4 hours for the API to return all of the data. Currently I invoke this process by clicking a button on my web page. However, I want to automate the process and schedule it to run every so often but have no idea where to begin or what to even Google. The web app runs on a Windows server which is running xammp. Any help is much appreciated.
I have managed to create a .bat file to execute my php script but I now get several errors in the CMD windows stating that it failed to open any of the files that I include in my script file:
My script file is:
<?php
include '../config.php';
include (ROOT.'/includes/my_session.php');
include (ROOT.'/model/dao.php');
include (ROOT.'/model/miller_api.php');
include (ROOT.'/model/branch_db.php');
$a = new miller_api;
$k = $a->apiRequest('key');
$a->apiRequest('branch', $k);
And here is a screen shot of the errors I am getting:
CMD errors

It is quite simple. Create a run.bat file with the following command
C:\xampp\php\php.exe C:\xampp\htdocs\SCRIPTFOLDER\SCRIPT.php
And schedule the run.bat file using windows Task Scheduler
https://www.techsupportalert.com/content/how-schedule-programs-run-automatically-windows-7.htm

Related

PHP Script results in Server Timeout

I'm running a script which reads a lot of data from the database. The script itself gets triggered by another script. (To ensure the script will not run twice or more times) Unfortunately I keep running into a server timeout.I tried to solve it with
set_time_limit(0);
and
ini_set('max_execution_time', 0);
but that didn't do the trick.
What the scirpt does is, it generates a CSV file which then should be downloaded by the user ( via browser ).
The file itself has to be created just in time, so I cannot create it during night and push it or anything like this.
Is there something like a best-practise?
Since I cannot change the generating script itself, how can I ensure, the file gets generated and the user gets informed that the file is ready for download?
(I can't use mail)?
Thank you so much in advance.

Run php file as cron job in windows 8.1 using php.exe

We are trying to setup php file into cron job in windows 8.1 system, to call GCM push notification and to get notification in our registerrd devices.
It works fine when we run cron using any browser like Chrome , Firefox then I get GCM push notification to my registered device but, If we try same configure using php.exe then we are not getting notification to our devices.
To schedule cron we are using Task Scheduler in that our trigger setting are like :-
And in our Action tab We have set up bellow lines:-
Program / script :- D:\xampp\php\php.exe
Add arguments (optional) :- -f D:\xampp\htdocs\PushNotificationSql\tasknotification.php
Start in (optional):- D:\xampp\htdocs\PushNotificationSql
After that We run manually then We get response as per below image but can not get notification into registered device.
Any help will be appreciate!
After many hurdles we got the solution of our problem
We have face three problems to make working this.
1). We have change our ActionTab to this
Program / script :- D:\xampp\php\php.exe
Add arguments (optional) :- -c D:\xampp\php\php.ini -f D:\xampp\htdocs\PushNotificationSql\tasknotification.php
Start in (optional):- [Remain empty]
This will successfully call php script from cmd using php.exe
2). In our php script there is code which is called php function from javascript and we knows that script code does not run using cmd its only handle by browser so we have change it and then it works fine.
3). And we have another php file include into this cron php file [tasknotification.php] so we have to write pull path of that included file.

Run python server script via php file and display output

After many trials and many days of search I am asking this question. I have tried many online solutions but doesn't seem to work for me. I have a python file Server.py that starts the flask server for me.
I want to start this file from a php file and display output. Right now I am trying with exec() function but the webpage keeps running and I could not figure if the server has started or not.
I have tried following ways with exec() and making a batch file
exec('C:/foldername/py/apps/webapp/s.bat &');
or
exec('C:/foldername/py/apps/webapp/s.bat > /dev/null');
or
$command = "python C:/folder/server.py";
$pid = popen( $command,"r");
Please help
I was able to achieve what I want in the end. I created a task schedule to open the batch file that can start my server. Then I set trigger on windows event. After that I created a batch file which creates an event in windows event logger and a php script to run that batch file. So, finally I was able to start python server file by php script with the help of task scheduler.

Informing User after Generating a Large Excel File in PHP

What is the best way to generate an excel file and inform the user after that?
I am using PHPExcel to generate an excel file from an MSSQL Server to a webserver and allow a user to download it using a link. The problem is that the each time we try to execute the PHP script it always throws a fast-cgi timeout error. The script needs to read up to 2000 - 5000 rows of data.
We tried to execute it via command prompt using exec() and Shell. It successfully generates the file in the server, but we don't have a way/method in informing the user after the script is completed.
exec() should return the result of running the external program - can't you use it? You can move generated file to a directory that is reachable for user and just give him the URL to the file.

Can I activate php script with FTP batch file?

I've written a daily batch file which uses FTP to login to my webserver and download a CSV full of new members using mget members.csv which is created on my site by PHP.
I've also have a php page on server which emails me with these new members. This php executes when I load the page in a browser but is it possible to execute it from the batch file?
I could also keep these members in a database if it's easier/securer but then ideally I wouldn't like to hold sensitive database login details in a batch file...
Many thanks
Using a scheduled batch file under Windows you could use the start command to execute a browser instance requesting the URL of your PHP script (which generates the email).
Put this in your scheduled batch file:
start www.stackoverflow.com
This one would use the systems default browser. To start a specific browser instead, you can use:
start /d "C:\Program Files\Mozilla Firefox" firefox.exe www.stackoverflow.com
start /d "C:\Program Files\Internet Explorer" iexplore.exe www.stackoverflow.com
:
You need to replace www.stackoverflow.com with the URL of your PHP script, of course^^
Why not just setup a cron job to check every x minutes/hours for populated csv file and send you the file if it is populated?
how to setup cron job

Categories