Php 对Restler 3 API的HTML响应支持
我需要支持不同的响应格式,如JSON和HTML,到目前为止,JSON响应格式很好,现在我尝试从我的API支持HTML响应,并面临一些问题 我需要从一些API返回JSON响应,从一些API返回HTML响应,但所有API(方法)都存在于同一个类中 若我在supported formats列表中添加JSON和HTML,那个么所有API都只返回HTML响应,不知道如何管理这种情况Php 对Restler 3 API的HTML响应支持,php,restler,Php,Restler,我需要支持不同的响应格式,如JSON和HTML,到目前为止,JSON响应格式很好,现在我尝试从我的API支持HTML响应,并面临一些问题 我需要从一些API返回JSON响应,从一些API返回HTML响应,但所有API(方法)都存在于同一个类中 若我在supported formats列表中添加JSON和HTML,那个么所有API都只返回HTML响应,不知道如何管理这种情况 $r->setSupportedFormat('JsonFormat','HtmlFormat'); 这是我用来设置JS
$r->setSupportedFormat('JsonFormat','HtmlFormat');
这是我用来设置JSON和HTML支持格式列表的代码块,任何人请告诉我如何处理这个场景
index.php
use Luracast\Restler\Defaults;
use Luracast\Restler\Filter\RateLimit;
use Luracast\Restler\Format\UploadFormat;
use Luracast\Restler\Format\HtmlFormat;
use Luracast\Restler\Restler;
require_once 'vendor/restler.php';
require_once('config.php');
Defaults::$throttle = 20;
Defaults::$cacheDirectory = PLAY_API_CACHE_DIR;
// Setup restler
$r = new Restler();
$r->addAPIClass('test');
$r->addAPIClass('Resources');
$r->addFilterClass('RateLimit');
$r->setSupportedFormats('JsonFormat', 'UploadFormat', 'HtmlFormat');
$r->handle();
require_once 'BaseHandler.php';
class test extends BaseHandler {
// Initialize API class attributes
public function __construct() {
parent::__construct();
}
/**
* Request the breakdown,by category, of a user's synced data.
*
* @param string $auth_token SSO Authentication Token
*
* @url GET getStorageUsage
*/
public function getStorageUsage($auth_token = '') {
// Required parameters checkup
if (!$auth_token && isset($_SESSION['play_auth_token'])) $auth_token = $_SESSION['play_auth_token'];
if (!$auth_token ) return PlayErrorCodes::throwParameterMissingError();
// Get a breakdown,by category, of a user's synced data using Sync API call
return $this->callAPI('sync', 'getStorageUsage', array('auth_token' => $auth_token));
}
/**
* Requests the full HTML document representing a users data graphically
*
* @param string $auth_token SSO Authentication Token
* @param string $client Name of the client requesting the widget. android is the only acceptable and default value.
* @param string $path Path of the resource to display. Defaults to '/'
*
* @url GET getWidget
* @view getWidgetView
*/
public function getWidget($auth_token = '',$client = '',$path = '') {
// Required parameters checkup
if (!$auth_token && isset($_SESSION['play_auth_token'])) $auth_token = $_SESSION['play_auth_token'];
if (!$auth_token) return PlayErrorCodes::throwParameterMissingError();
// Get the full HTML document representing a users data graphically using Sync API call
$this->resDecodeFlag = false;
return $this->callAPI('sync', 'getWidget', array('auth_token' => $auth_token, 'client' => $client, 'path' => $path));
}
}
使用Luracast\Restler\Defaults;
使用Luracast\Restler\Filter\RateLimit;
使用Luracast\Restler\Format\UploadFormat;
使用Luracast\Restler\Format\HtmlFormat;
使用Luracast\Restler\Restler;
需要一次“vendor/restler.php”;
需要一次_('config.php');
默认值::$throttle=20;
默认值::$cacheDirectory=PLAY\u API\u CACHE\u DIR;
//设置还原器
$r=新的Restler();
$r->addAPIClass('test');
$r->addAPIClass(“资源”);
$r->addFilterClass('RateLimit');
$r->setSupportedFormats('JsonFormat','UploadFormat','HtmlFormat');
$r->handle();
test.php
use Luracast\Restler\Defaults;
use Luracast\Restler\Filter\RateLimit;
use Luracast\Restler\Format\UploadFormat;
use Luracast\Restler\Format\HtmlFormat;
use Luracast\Restler\Restler;
require_once 'vendor/restler.php';
require_once('config.php');
Defaults::$throttle = 20;
Defaults::$cacheDirectory = PLAY_API_CACHE_DIR;
// Setup restler
$r = new Restler();
$r->addAPIClass('test');
$r->addAPIClass('Resources');
$r->addFilterClass('RateLimit');
$r->setSupportedFormats('JsonFormat', 'UploadFormat', 'HtmlFormat');
$r->handle();
require_once 'BaseHandler.php';
class test extends BaseHandler {
// Initialize API class attributes
public function __construct() {
parent::__construct();
}
/**
* Request the breakdown,by category, of a user's synced data.
*
* @param string $auth_token SSO Authentication Token
*
* @url GET getStorageUsage
*/
public function getStorageUsage($auth_token = '') {
// Required parameters checkup
if (!$auth_token && isset($_SESSION['play_auth_token'])) $auth_token = $_SESSION['play_auth_token'];
if (!$auth_token ) return PlayErrorCodes::throwParameterMissingError();
// Get a breakdown,by category, of a user's synced data using Sync API call
return $this->callAPI('sync', 'getStorageUsage', array('auth_token' => $auth_token));
}
/**
* Requests the full HTML document representing a users data graphically
*
* @param string $auth_token SSO Authentication Token
* @param string $client Name of the client requesting the widget. android is the only acceptable and default value.
* @param string $path Path of the resource to display. Defaults to '/'
*
* @url GET getWidget
* @view getWidgetView
*/
public function getWidget($auth_token = '',$client = '',$path = '') {
// Required parameters checkup
if (!$auth_token && isset($_SESSION['play_auth_token'])) $auth_token = $_SESSION['play_auth_token'];
if (!$auth_token) return PlayErrorCodes::throwParameterMissingError();
// Get the full HTML document representing a users data graphically using Sync API call
$this->resDecodeFlag = false;
return $this->callAPI('sync', 'getWidget', array('auth_token' => $auth_token, 'client' => $client, 'path' => $path));
}
}
需要一次'BaseHandler.php';
类测试扩展了BaseHandler{
//初始化API类属性
公共函数构造(){
父项::_构造();
}
/**
*请求按类别细分用户的同步数据。
*
*@param string$auth_令牌SSO身份验证令牌
*
*@url获取getStorageUsage
*/
公共函数getStorageUsage($auth_token=''){
//所需参数检查
如果(!$auth\u令牌和&isset($会话['play\u auth\u令牌])$auth\u令牌=$会话['play\u auth\u令牌]);
if(!$auth_token)返回PlayErrorCodes::throwParameterMissingError();
//使用同步API调用按类别获取用户同步数据的明细
返回$this->callAPI('sync','getStorageUsage',array('auth_-token'=>$auth_-token));
}
/**
*请求以图形方式表示用户数据的完整HTML文档
*
*@param string$auth_令牌SSO身份验证令牌
*@param string$请求小部件的客户端的客户端名称。android是唯一可接受的默认值。
*@param string$要显示的资源的路径。默认值为“/”
*
*@url获取getWidget
*@view-getWidgetView
*/
公共函数getWidget($auth_令牌=“”,$client=“”,$path=“”){
//所需参数检查
如果(!$auth\u令牌和&isset($会话['play\u auth\u令牌])$auth\u令牌=$会话['play\u auth\u令牌]);
if(!$auth_token)返回PlayErrorCodes::throwParameterMissingError();
//使用syncapi调用以图形方式获取表示用户数据的完整HTML文档
$this->resDecodeFlag=false;
返回$this->callAPI('sync','getWidget',array('auth_token'=>$auth_token',client'=>$client',path'=>$path));
}
}
在这里,我需要为“getStorageUsage”返回JSON响应,为“getWidget”返回HTML响应
我看过的参考资料是:先谢谢你。。。Siva.正如@Luceos正确指出的那样,浏览器发送的accept头总是更喜欢HTML 为了克服这种情况,您可以随时使用相应的扩展请求这些资源
getStorageUsage.json
getWidget.html
$r = new Restler();
$r->addAPIClass('MyApiClass');
$r->setSupportedFormats('JsonFormat', 'UploadFormat');
$r->setOverridingFormats('HtmlFormat');
$r->handle();
然后在getWidget方法上添加@format
注释以指定HtmlFormat
,如下所示
/**
* Requests the full HTML document representing a users data graphically
*
* @param string $auth_token SSO Authentication Token
* @param string $client Name of the client requesting the widget. android is the only acceptable and default value.
* @param string $path Path of the resource to display. Defaults to '/'
*
* @url GET getWidget
* @view getWidgetView
* @format HtmlFormat
*/
public function getWidget($auth_token = '', $client = '', $path = '') {
//your logic comes here
}
到目前为止您尝试了什么,代码在哪里?我们无法查看无法阅读的代码。。你犯了什么错误?你在哪里失败了?@Luceos:用代码块更新了我的问题。这里没有得到任何错误,在设置了HtmlFormat支持之后,所有API都返回HTML响应。您如何进行调用。Restler根据您发送的Accept标头标识返回格式。因此,如果您从浏览器调用Web服务,它将返回HTML。