0

我有一个围绕 Wordpress 构建的应用程序。它是相当定制的,但我仍然利用并欣赏 Wordpress 附带的很多东西。现在我要做的是提供一个 RESTful API,它可以脱离 Wordpress 数据库。我正在使用一个名为Restler的开源产品,到目前为止我对它非常满意,这使得使用 PHP 提供这项服务非常容易,但我想让这个 PHP 加载 Wordpress 机器,以便我可以利用我已经构建的代码更容易以及方便的 Wordpress 功能。

使用 Restler,您有一个定义接口的 PHP 类文件;我在开头添加了导入以加载 WP 环境:

<?php
include "AddWordpress.php"; // loads wordpress in AJAX mode

/**
 * All actions that the system will process through this service.
 *
 * @package lg-api
 * @return  json    LG_JSON_Activity    
*/
class Actions {
    /**
    * LIST action types
    *
    * List all the action-types available
    *
    * @url GET /
    */
    function list_actions() {
        $actions = new LG_TAX_Action();
        $action_list = $actions->dataset();
        return "\n" . json_encode($action_list) . "\n\n";
    }

}

AddWordpress.php 在哪里(为开发环境设置调试):

<?php
error_reporting(E_ALL);
$site_name='yoursite';
$site_domain='www.yoursite.com';

/**
* Construct a fake $_SERVER global to get WordPress to load a specific site.
* This avoids alot of messing about with switch_to_blog() and all its pitfalls.
*/
$_SERVER=array(
'HTTP_HOST'=>$site_domain,
'REQUEST_METHOD'=>'GET',
'REQUEST_URI'=>"/{$site_name}/",
'SERVER_NAME'=>$site_domain,
);

// Remove all our bespoke variables as they'll be in scope as globals and could affect WordPress
unset($site_name,$site_domain);

// Pretend that we're executing an AJAX process. This should help WordPress not load all of the things.
define('DOING_AJAX',true);

// Stop WordPress doing any of its normal output handling.
define('WP_USE_THEMES',false);

// turn on debugging
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);

// Load WordPress - intentionally using an absolute URL due to issues with relative paths on the CLI.
include "/[path-to-root-www]/wp-load.php";

这种方法非常适合在跳转到实际代码并通过浏览器进行测试之前创建命令行测试文件,但由于某种原因,当我使用 Restler 运行它时,我遇到了一系列错误,例如:

Notice:  is_404 was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 294
Notice:  is_home was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_archive was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_404 was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_tag was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_category was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944

如果我只是将以下代码添加到类文件的底部,然后从命令行运行它,它就可以正常工作:

 $foo = new Actions();
 echo $foo->list_actions();

任何人都可以就为什么会发生这种情况给出任何指示吗?

更新

我更改了错误处理以给我一个堆栈跟踪并获得以下信息:

#0 /[path-to-www-root]/wp-includes/query.php(708): _doing_it_wrong('is_404', 'Conditional que...', '3.1')
#1 /[path-to-www-root]/wp-content/themes/pagelines/includes/library.functions.php(68): is_404()
#2 /[path-to-www-root]/wp-content/themes/pagelines/includes/library.options.php(43): is_pagelines_special(Array)
#3 /[path-to-www-root]/wp-content in /[path-to-www-root]/wp-includes/functions.php on line 2944

所以最初的调用似乎来自我的主题调用 query.php。这是更多信息,但我不确定它真正提供了多少洞察力,因为我很确定解决这个问题的方法是解决为什么导入的初始状态在从命令行运行与尝试追溯所有问题之间会有所不同发生在它开始时的贫穷状态。

4

0 回答 0