【问题标题】:Luracast Restler is sending 'text/html' instead of JSONLuracast Restler 正在发送“文本/html”而不是 JSON
【发布时间】:2012-05-02 09:19:14
【问题描述】:

我有一个使用 LuraCast Restler API 实现的 REST Web 服务。到目前为止,它一直运行良好。突然间,我不再收到响应我对服务的请求的 JSON 数据了。

X-Debug 总是在对服务的每个请求中返回以下通知。

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php on line 1136
Call Stack
#   Time    Memory  Function    Location
1   0.0013  631072  {main}( )   ../restIndex.php:0
2   0.0969  1170720 Restler->addAPIClass( ) ../restIndex.php:14
3   0.0971  1181912 Restler->generateMap( ) ../restler.php:248
4   0.0971  1184616 parse_doc( )    ../restler.php:652
5   0.0971  1184984 DocParser->parse( ) ../restler.php:1157
6   0.0972  1205520 DocParser->parseLines( )    ../restler.php:1073
7   0.0972  1209704 DocParser->parseLine( ) ../restler.php:1079
8   0.0973  1209944 DocParser->setParam( )  ../restler.php:1111
9   0.0973  1209944 DocParser->formatClass( )   ../restler.php:1120

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php on line 1136
Call Stack
#   Time    Memory  Function    Location
1   0.0013  631072  {main}( )   ../restIndex.php:0
2   0.1065  1224952 Restler->addAuthenticationClass( )  ../restIndex.php:15
3   0.1065  1225304 Restler->addAPIClass( ) ../restler.php:260
4   0.1068  1228176 Restler->generateMap( ) ../restler.php:248
5   0.1068  1229568 parse_doc( )    ../restler.php:652
6   0.1068  1229936 DocParser->parse( ) ../restler.php:1157
7   0.1068  1239456 DocParser->parseLines( )    ../restler.php:1073
8   0.1068  1241448 DocParser->parseLine( ) ../restler.php:1079
9   0.1069  1241696 DocParser->setParam( )  ../restler.php:1111
10  0.1069  1241696 DocParser->formatClass( )   ../restler.php:1120

( ! ) Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php:1136) in /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php on line 385
Call Stack
#   Time    Memory  Function    Location
1   0.0013  631072  {main}( )   ../restIndex.php:0
2   0.1102  1228048 Restler->handle( )  ../restIndex.php:16
3   0.1233  1461248 Restler->sendData( )    ../restler.php:366
4   0.2814  1547080 header ( )  ../restler.php:385

( ! ) Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php:1136) in /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php on line 386
Call Stack
#   Time    Memory  Function    Location
1   0.0013  631072  {main}( )   ../restIndex.php:0
2   0.1102  1228048 Restler->handle( )  ../restIndex.php:16
3   0.1233  1461248 Restler->sendData( )    ../restler.php:366
4   0.2835  1547048 header ( )  ../restler.php:386

( ! ) Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php:1136) in /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php on line 387
Call Stack
#   Time    Memory  Function    Location
1   0.0013  631072  {main}( )   ../restIndex.php:0
2   0.1102  1228048 Restler->handle( )  ../restIndex.php:16
3   0.1233  1461248 Restler->sendData( )    ../restler.php:366
4   0.2846  1547064 header ( )  ../restler.php:387

( ! ) Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php:1136) in /Applications/MAMP/htdocs/aem.davidcasillas.es/public/webservice/restler/restler.php on line 388
Call Stack
#   Time    Memory  Function    Location
1   0.0013  631072  {main}( )   ../restIndex.php:0
2   0.1102  1228048 Restler->handle( )  ../restIndex.php:16
3   0.1233  1461248 Restler->sendData( )    ../restler.php:366
4   0.2914  1547072 header ( )  ../restler.php:388

最后数据是作为响应而不是 JSON 格式发送的,所以我的 movil 应用程序无法再处理它。

正如我所说,到目前为止,该服务一直运行良好。我不知道这个通知是否与错误有关,但它们显然似乎是。

你能指出我如何调试这个问题的任何方向吗?

如果这有任何帮助,这里是我的代码:

这是我配置 Restler 的索引脚本:

<?php

require_once '../../BaseDatos.php';
require_once 'restler/restler.php';

#set autoloader
#do not use spl_autoload_register with out parameter
#it will disable the autoloading of formats
spl_autoload_register('spl_autoload');


$r = new Restler();
$r->setSupportedFormats('JsonFormat');
$r->addAPIClass('Rest');
$r->addAuthenticationClass('Seguridad');
$r->handle();

这是公共 API 类(我只展示一种方法)

class Rest
{

    /**
     * @brief Una instancia de la clase BaseDatos
     *
     * @var BaseDatos $db
     */
    private $db;

    /**
     * \brief Constructor de la clase.
     *
     * Inicializa una instancia de la clase BaseDatos.
     *
     */
    function __construct() {
        $this->db = BaseDatos::getInstance();
    }
    public function getFuentes($nombreUsuario, $password) {

        $fuentes = $this->db->getFuentes();
        $fuentes = array('fuentes' => $fuentes);
        return $fuentes;
    }

这是检索要返回的信息的 BaseDatos 类方法:

public function getFuentes() {
    //SELECT * FROM fuentes
    $queryString = 'SELECT * FROM ' . TABLA_FUENTES . ' ORDER BY nombreFuente';
    $queryResult = $this->query($queryString);
    if ($this->errno != 0) {
        //ERRRO. mysql
        $this->setErrorNoAndMessage(ErrorCode::ERROR_MYSQL, $this->error);
        return FALSE;
    }
    $resultado = array();
    while ($row = $queryResult->fetch_assoc() ) {
        $resultado[] = $row;
    }

    $this->resetErrorNoAndMessage();
    return $resultado;

}

【问题讨论】:

    标签: php rest restler


    【解决方案1】:

    问题与我最近添加的文档评论有关。我在类文档中添加的@class 标签产生了冲突,可能是因为 Restler 的 DocParser 没有处理这个特定的标签。

    当 DocParser 尝试执行 formatClass() 方法以响应 @class 标记而不是 PHP 中的 Class 声明时,我在问题中包含的前 2 个通知发生。

    也许对这个 API 有深入了解的人可以补充一些亮点。

    只需从文档中删除此标签,一切都会恢复正常。

    【讨论】:

    • 感谢@david-casillas 报告此问题。你是对的 @class 应该采用特定格式,这会导致问题。我们将在下一个版本中修复它。 @class 当前用于在初始化后设置另一个类的属性。阅读this example了解更多详情。
    猜你喜欢
    • 2011-12-06
    • 1970-01-01
    • 2014-11-11
    • 2011-12-15
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 2022-06-29
    相关资源
    最近更新 更多