通过php命令行执行js函数 [英] execute js function by php command line
问题描述
我在Codeigniter工作。我有一个view.book.php,上面有一个js文件frontend_book.js,它在其中运行,其中包含一个同步日历的功能。
I am working in Codeigniter. I have a view, book.php, with a js file, frontend_book.js, running behind it containing a function to sync the calendar.
frontend_book.js的摘录
Snippet from frontend_book.js
googleSync: function() {
provider_id= $('#select-provider').val();
getUrl = GlobalVariables.baseUrl + 'google/sync/' + provider_id;
jQuery.get(getUrl,provider_id, console.log('Google sync successful'),'json');
}
我想使用cron作业独立于视图运行此功能我的google.php控制器中的一个php函数,它是这样的:
I would like to run this function independent of the view in a cron job using a php function in my google.php controller, that does something like this:
public function syncallproviders() {
//get the array of providers:
$this->load->model('providers_model');
$providers = $this->providers_model->get_available_providers();
//for each providers as provider run the js googleSync
foreach ($providers['id'] as $provider) {
//Run googleSync where #select-provider is $provider
}
}
这样我就可以使用
php /mypath/index.php "google" syncallproviders
我如何集成它?
这是我最好的猜测将两者放在一起:
This is my best guess at putting the two together:
<?php
//get the array of providers:
$this->load->model('providers_model');
$providers = $this->providers_model->get_available_providers();
//for each providers as provider run google sync
foreach ($providers as $provider) { ?>
<script type="text/javascript">
var getUrl=<?php echo $this->config->base_url().'/google/sync/'.$provider['id']; ?>,
var provider_id =<?php echo $provider['id']; ?>,
jQuery.get(getUrl,provider_id, console.log('Google sync successful'),'json'),
</script>
<?php }
这看起来正确吗?现在如何从命令行运行它?如果我从命令行运行浏览器,该如何代替浏览器?
Does this look right? Now how can I make this run from a command line? What can take the place of a browser if I run it from command line?
推荐答案
如果出于某种原因,您想要要将其作为cron运行,并且您想要保持应用程序的当前状态(用php编写的mvc),则可以使用无头浏览器(例如phantomjs)从以bash身份运行的bash脚本中找到所需的url。 cron。这有点令人费解-但却可以满足您的要求。 -如果您想通过php保留所有软件包,甚至还有一个php软件包- http://jonnnnyw.github .io / php-phantomjs /
if, for what ever reason, you wanted to run this as a cron, and you wanted to maintain the application as it is now (mvc written in php) then you could use a headless browser (such as phantomjs) to hit the desired url from a bash script that is run as a cron. This is a bit convoluted - but it would do what you wanted. - there is even a php package if you wanted to keep it all via php - http://jonnnnyw.github.io/php-phantomjs/
这篇关于通过php命令行执行js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!