admin管理员组

文章数量:1327826

I have php file that is to be executed as cronjob, this php file contains some javascript.

I will explain the flow :

  1. the Php is used to retrive some data(A LIST OF URLS) from DB.
  2. For each URL Obtained, a Java script API is used.
  3. THe result Obj returned from API contains data for each url.
  4. 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 :

  1. the Php is used to retrive some data(A LIST OF URLS) from DB.
  2. For each URL Obtained, a Java script API is used.
  3. THe result Obj returned from API contains data for each url.
  4. 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).

Share Improve this question edited Apr 15, 2013 at 18:46 AvK asked Apr 9, 2013 at 14:03 AvKAvK 751 silver badge9 bronze badges 2
  • 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
Add a ment  | 

5 Answers 5

Reset to default 3

You 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

本文标签: Can CronJobs execute a php containing JavascriptIf NoAny AlternativesStack Overflow