对Restler 3 API的HTML响应支持

时间:2013-12-09 10:19:36

标签: php restler

我需要支持不同的响应格式,如JSON和HTML,到目前为止,这对JSON响应格式来说很好,现在我尝试支持来自API的HTML响应并面临问题。

我需要从某些API返回JSON响应,并从某些API返回HTML响应,但所有API(方法)都存在于同一个类中。

如果我在支持的格式列表中添加JSON和HTML,那么所有API都只返回HTML响应,不知道如何管理这种情况。

$r->setSupportedFormats('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();  

test.php的

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)); 
    }

}

在这里,我需要返回'getStorageUsage'的JSON响应和'getWidget'的HTML响应。

我所经历的参考文献是:
http://restler3.luracast.com/examples/_013_html/readme.html
https://github.com/Luracast/Restler/tree/master/public/examples/_013_html#html-format

提前致谢... Siva。

1 个答案:

答案 0 :(得分:1)

正如@Luceos正确地指出,浏览器发送始终更喜欢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
}