I have php file that is to be executed as cronjob, this php file contains some javascript.
I will explain the flow :
- the Php is used to retrive some data(A LIST OF URLS) from DB.
- For each URL Obtained, a Java script API is used.
- THe result Obj returned from API contains data for each url.
- The data is then sent back to as an AJAX Call for each url to a php file .
Can this be implemented Via CRON JOBS ?
OR
Is there any method to schedule javascript to run periodically, like cron for php?
UPDATE: i could manage the javascript call to API with PHP curl ,And the cron Job is getting executed perfectly. But i dont think it is the correct solution to this question may be Node.Js is the solution(i didnt test it yet).
I have php file that is to be executed as cronjob, this php file contains some javascript.
I will explain the flow :
- the Php is used to retrive some data(A LIST OF URLS) from DB.
- For each URL Obtained, a Java script API is used.
- THe result Obj returned from API contains data for each url.
- The data is then sent back to as an AJAX Call for each url to a php file .
Can this be implemented Via CRON JOBS ?
OR
Is there any method to schedule javascript to run periodically, like cron for php?
UPDATE: i could manage the javascript call to API with PHP curl ,And the cron Job is getting executed perfectly. But i dont think it is the correct solution to this question may be Node.Js is the solution(i didnt test it yet).
- 1 Why do you need to use javascript to call the api? – Jack Commented Apr 9, 2013 at 14:05
- google apis require javascript calls. – AvK Commented Apr 9, 2013 at 14:11
5 Answers
Reset to default 3You can't run Javascript in Cronjobs because Javascript is ran by browsers. I think you should take a look at curl in php to call an api instead.
http://www.php/manual/en/book.curl.php
You have to split the work: Cron the JS, Cron the PHP. In the middle, deliver one's results to another. Agree with phantomjs usage for JS execution (or casperJS-I prefer). Execute the JS, output to JSON as a file, read from the file using file_get_contents from PHP. And define these actions in two different cron jobs.
You can run Javascript via cron in a Javascript runtime like node.js: http://nodejs/
phantomjs is one possibility, see this thread wget + JavaScript?
Otherwise you could run Node.js on your server to execute JavaScript in a CLI type environment but mixing node.js and PHP could bee plicated.
you can schedule javascript with cron by using Node.js