drupal未定义函数静态html并发布到php
我已将自己的 php 文件放置在 /sites/all/modules/$.post(
调用它。
然而,php 似乎没有按预期执行,而是被调用(Apache HTTPD 中的访问日志显示它已发布到)。
如果我尝试手动请求相同的 php 页面,我会收到以下错误:
致命错误:调用 /full/path/to/sites/modules/
/ 中未定义的函数 db_insert()在线 x 上。
我在 php 页面中的 echo
语句在该错误上方正确输出,并且仅使用 $_GET['paramname']
。 (和 _POST,更改为测试直接请求)打印一些查询字符串参数以进行调试)。
另外,当我添加对 watchdog 的调用时,我收到:
致命错误:调用 /full/path/to/sites/modules/
/ 中未定义的函数 watchdog()在线 x 上。
是不是不能直接访问PHP页面呢?或者我需要导入 Drupal 库吗?或者我只是简单地错过了 Drupal 工作方式的其他内容?
I have placed my own php file in /sites/all/modules/<myfolder> and call it via $.post(<myphppage>)
from a static html page (javascript function) also in /sites/all/modules/<myfolder>.
However, the php does not appear to be executing as expected, but is getting called (access logs in Apache HTTPD show it is post'ed to).
If I try to manually request the same php page, I receive this error:
Fatal error: Call to undefined function db_insert() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
The echo
statement I have in the php page is outputted properly above this error, and simply uses $_GET['paramname']
. (And _POST, changed for testing direct request) to print a few query string parameters for debugging).
Also, when I added a call to watchdog
, I receive:
Fatal error: Call to undefined function watchdog() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
Is it not possible to access the PHP page directly? Or is there a Drupal library I need to import? Or am I just plain missing something else with how Drupal works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到有关未定义函数的错误是因为您的 PHP 文件独立于 Drupal 并且它没有引导它。
Drupal 执行您尝试执行的操作的方法是创建一个模块(请参阅创建 Drupal 7.x 模块 和创建 Drupal 6.x 模块)定义它处理的 URL hook_menu();然后将该 URL 与
$.post()
一起使用。作为引导 Drupal 方式的示例,请参阅 index.html 的内容。每个 Drupal 安装附带的 php 文件。 (以下代码是从 Drupal 7 使用的代码。)
代码从 Drupal 6 更改为 Drupal 7,但是 drupal_bootstrap() 在两个 Drupal 版本中都有定义。
无论如何,正确的方法是创建一个模块。有关模块应使用引导 Drupal 的 PHP 文件的原因列表,请参阅 是否存在第三方模块需要使用自己的文件(类似于 xmlrp.php、cron.php 或验证.php?
You are getting those errors about undefined functions because your PHP file is independent from Drupal and it is not bootstrapping it.
The Drupal way of doing what you are trying to do is to create a module (see Creating Drupal 7.x modules, and Creating Drupal 6.x modules) that defines a URL it handles with hook_menu(); that URL is then used with
$.post()
.As example of the way of bootstrapping Drupal, see the content of the index.php file that comes with every Drupal installation. (The following code is the one used from Drupal 7.)
The code changes from Drupal 6 to Drupal 7, but drupal_bootstrap() is defined in both Drupal versions.
The correct way is to create a module, anyway. For a list of reasons why a module should use a PHP file that bootstraps Drupal, see Are there cases where a third-party module would need to use its own file similar to xmlrp.php, cron.php, or authenticate.php?