最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can CronJobs execute a php containing Javascript ? If No, Any Alternatives? - Stack Overflow

programmeradmin4浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论