Я использую этот отдых API https://github.com/mohamedbenjelloun/MyBB-RESTful-API-System
И сделайте запрос вот так
https://example.com/api.php/authenticate
Я могу отправить как заголовок и как multipart в этом API,
username,
password,
apikey,
и получил успех 200 ОК - тело не возвращено для ответа
и в файле inc \ plugins \ restfulapi \ api \ authenticateapi.class.php, который отвечает за этот REST API - конечной точкой аутентификации является код
<?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 его outputer для этого ответа
/**
* 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);
}
}