MyBB PHP REST API没有正文返回响应200

时间:2019-05-22 12:28:10

标签: php rest mybb

我正在使用此剩余api https://github.com/mohamedbenjelloun/MyBB-RESTful-API-System 并发出这样的请求

https://example.com/api.php/authenticate

我可以在此api中发送像标头和多部分一样的信息

username,
password,
apikey,

成功了200正常-没有人返回响应t

并在负责该REST API的文件inc \ plugins \ restfulapi \ api \ authenticateapi.class.php中-验证端点是代码

<?php

// THERE IS MY CHANGED SCRIPT BECOUSE I NEED TO CHANGE WHOLE SCRIPT IF USER IS LOOGED ON SITE OR IF NOT

# This file is a part of MyBB RESTful API System plugin - version 0.2
# Released under the MIT Licence by medbenji (TheGarfield)
# 
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
/**
This interface should be implemented by APIs, see VersionAPI for a simple example.
*/
class AuthenticateAPI extends RESTfulAPI {

    public function info() {
        return array(
            "name" => "Authentication",
            "description" => "This API exposes authentication interface.",
            "default" => "activated"
        );
    }
    /**
    This is where you perform the action when the API is called, the parameter given is an instance of stdClass, this method should return an instance of stdClass.
    */
    public function action() {
        global $mybb, $db;
        if($this->is_authenticated()) {
            return $this->get_user();
        }
        elseif(isset($mybb->input["sessionid"]) && is_string($mybb->input["sessionid"])) {
            $sid = $db->escape_string($mybb->input["sessionid"]);
            $query = $db->query("SELECT s.uid FROM " . TABLE_PREFIX . "sessions s WHERE s.sid = '{$sid}'");
            $result = $db->fetch_array($query);
            if(empty($result)) {
                throw new UnauthorizedException("Not connected.");
            }
            else {
                $uid = $result['uid']; // no need to escape this, it's just been retrieved from db
                $query = $db->query("
                    SELECT u.*, f.*
                    FROM ".TABLE_PREFIX."users u
                    LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
                    WHERE u.uid='$uid'
                    LIMIT 1
                ");
                $user = (object) $db->fetch_array($query);
                if(empty($user)) {
                    throw new UnauthorizedException("Not connected");
                }
                $user->ismoderator = is_moderator("", "", $uid);
                return $user;
            }
        }
        else {
            throw new UnauthorizedException("Not connected.");
        }
    }
}

然后我尝试调试代码,它正确地设置了sessionid,然后进行查询以获取用户字段,一切正常。而且没有错误意味着一切正常,但是它给出了像img一样的空响应。

例如,我们还有其他用于数据的端点及其正确的返回对象inc \ plugins \ restfulapi \ api \ date.api.class.php

<?php

# This file is a part of MyBB RESTful API System plugin - version 0.2
# Released under the MIT Licence by medbenji (TheGarfield)
# 
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

/**
This interface should be implemented by APIs, see VersionAPI for a simple example.
*/
class DateAPI extends RESTfulAPI {

    public function info() {
        return array(
            "name" => "Date",
            "description" => "This API exposes date utility, very useful for external systems.",
            "default" => "activated"
        );
    }
    /**
    This is where you perform the action when the API is called, the parameter given is an instance of stdClass, this method should return an instance of stdClass.
    */
    public function action() {
        global $mybb;
        $stdClass = new stdClass();
        $timestamp = "";
        if(isset($mybb->input["timestamp"])) {
            $timestamp = (string) $mybb->input["timestamp"];
        }
        $ty = 1;
        if(isset($mybb->input["ty"]) && in_array($mybb->input["ty"], array("0", "1"))) {
            $ty = (int) $mybb->input["ty"];
        }

        $stdClass->date = my_date($mybb->settings['dateformat'], $timestamp, "", $ty);
        $stdClass->time = my_date($mybb->settings['timeformat'], $timestamp, "", $ty);
        $stdClass->timestamp = $timestamp;

        return $stdClass;
    }

}

给出

{
  "date": "Today",
  "time": "12:22 PM",
  "timestamp": ""
}

有趣的是,我之前曾遇到过这个问题,但是它消失了,不知道为什么,现在又回到我身边。 这里可能是什么问题?

那可能会有帮助

inc \ plugins \ restfulapi \ api \ apisystem.class.php此响应的输出器

/**
 * Builds the outputer (RESTfulOutput instance), or create a JSONOutput if nothing was found. If already built, that same instance is returned.
 */
public function build_outputer() {
    if(null === $this->outputer) {
        $declared_output = $this->declared_output();
        if(is_string($declared_output)) {
            if(file_exists(MYBB_ROOT . "inc/plugins/restfulapi/output/" . strtolower($declared_output) . "output.class.php")) {
                require_once "output/" . strtolower($declared_output) . "output.class.php";
                $outputclass = strtolower($declared_output) . "output";
                $this->outputer = new $outputclass;
                return $this->outputer;
            }
        }
        require_once "output/jsonoutput.class.php";
        $outputclass = "jsonoutput";
        $this->outputer = new $outputclass;
    }
    return $this->outputer;
}

和jsonoutput.class.php

class JSONOutput extends RESTfulOutput {

    /**
    This is where you output the object you receive, the parameter given is an instance of stdClass.
    */
    public function action($stdClassObject) {
        header("Content-Type: application/json; charset=utf-8");
        echo json_encode($stdClassObject);
    }
}

0 个答案:

没有答案