有可能使用ajax,jquery页面与cron? [英] it is possible to use ajax, jquery page with cron?

查看:287
本文介绍了有可能使用ajax,jquery页面与cron?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页是:


  1. 从不同的服务器下载一个阵列(first.php)

  2. php脚本解析值

  3. 解析的值会在下一个(ajax调用)页面(second.php)上通过ajax调用

  4. 是一些mysql查询

  5. 如果值通过条件,值会写入数据库

..所以,当我运行我的first.php ..它加载second.php,一切都很好..
但是我想知道如果它是可能让它由cron?



如果没有,我该怎么办?
谢谢。

解决方案

在这方面你需要了解一些事情。



第一个是PHP可以作为 web服务器模块或作为独立可执行文件运行。当你运行它作为一个web服务器模块,你从浏览器打开它,所有相关的web技术(html / css / js)等得到解析和工作在一致。 / p>

当使用cron从命令行运行它时,如 / usr / bin / php mywebpage.php



这里有两种解决方法:


  1. 只重写那些支持web的部分,使得ajax / js的东西得到PHP处理的
    。基本上的规则是,如果你是
    运行CLI php脚本,它应该只包含核心PHP。这是首选方法。您需要将ajax调用移动到同一个文件中,并使其像任何常规程序一样执行单个执行流程。


  2. 如果由于某种原因,做上面的,你可以尝试这样:
    / path / to / browser http://mysite/mywebpage.php 。这里你正在做的是,你正在运行一个浏览器可执行文件,然后调用网页的URL。这样,页面在浏览器的环境中被执行,它将能够解析和理解ajax / js调用。



what my page do is:

  1. download an array from different server (first.php)
  2. php script parse values
  3. parsed values are sent with ajax call
  4. on the next (ajax called) page (second.php) there are some mysql queries
  5. if values pass condition, values are written to database

.... So, when I run my first.php.. it loads second.php, everything's fine.. but what I want to know if it is possible to let it make by cron?

If not, what should I do? Thanks.

解决方案

There are certain things you need to understand in this regard.

The first is that PHP can be run as either a web server module or as a standalone executable. When you run it as a web server module, you open it from the browser, all related web technologies (html/css/js) etc get parsed and work in unison.

When you run it from command line using cron like say /usr/bin/php mywebpage.php then the php executable DOES NOT parse/understand the other web technologies and so your page will fail.

There are two workarounds for this:

  1. Rewrite only those web-enabled parts so that the ajax/js stuff gets handled by PHP. Basically rule of the thumb is that if you are running a CLI php script, it should contain ONLY core PHP. This is the preferred way. You will need to move the ajax calls to inside the same file and just make it a single execution flow like any regular program.

  2. If for some reason you cannot do the above, you can try something like this: /path/to/browser http://mysite/mywebpage.php. Here what you are doing is, you are running a browser executable and then calling the webpage URL. This way the page is being executed within the browser's environment and it will be able to parse and understand the ajax/js calls.

这篇关于有可能使用ajax,jquery页面与cron?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆