3

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).

AvK
  • 75
  • 1
  • 9

5 Answers5

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.net/manual/en/book.curl.php

Anyone
  • 2,814
  • 1
  • 22
  • 27
  • 2
    Though I agree that what the OP seems to want to do is nightmarish, this answer is not strictly true - it's possible to use JavaScript integrated with PHP on the server. – Pointy Apr 09 '13 at 14:08
  • If you really want to run javascript server side, you have to consider other options because PHP is able to run these kind of things itself ;) – Anyone Apr 09 '13 at 14:12
  • yes I am not saying it's a good idea, just that it's possible. People seem to be able to bolt anything in the world onto PHP, for whatever reason. – Pointy Apr 09 '13 at 14:13
1

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.

0

You can run Javascript via cron in a Javascript runtime like node.js: http://nodejs.org/

christopher_b
  • 958
  • 5
  • 13
0

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 become complicated.

Community
  • 1
  • 1
alvinc
  • 385
  • 3
  • 10
0

you can schedule javascript with cron by using Node.js

Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224