【问题标题】:Luracast Restler: "Naming" returned objectsLuracast Restler:“命名”返回的对象
【发布时间】:2011-10-20 13:24:31
【问题描述】:

我正在使用漂亮的 Restler REST 框架将数据库中的一些资源公开给各种客户端。

现在从服务返回结果时,格式有点像这样(我在这里使用 Restler 网站自己的示例作为基础):

[
 {
   "id": 1,
   "name": "Jac Wright",
   "email": "jacwright@gmail.com"
 }
]

或者在 XML 中:

<?xml version="1.0"?>
<response>
  <item>
    <id>1</id>
    <name>Jac Wright</name>
    <email>jacwright@gmail.com</email>
  <item>
</response>

让我有点恼火的是,在 JSON 中,结构是匿名的(如果这是在这里使用的正确术语?),而在 XML 中,对象被包装在“项目”标签中。我希望看到由资源类型名称包裹的返回结构。像这样:

[
 "author": {
   "id": 1,
   "name": "Jac Wright",
   "email": "jacwright@gmail.com"
 }
]

<?xml version="1.0"?>
<response>
  <author>
    <id>1</id>
    <name>Jac Wright</name>
    <email>jacwright@gmail.com</email>
  <author>
</response>

如果我还没有完全理解相关代码,如果不修改 Restler 本身,这是否可能?

【问题讨论】:

    标签: php api rest


    【解决方案1】:

    example you have pointed out 返回以下内容

    return array(
        array('id'=>1,  'name'=>'Jac Wright',   'email'=>'jacwright@gmail.com'),
        array('id'=>2,  'name'=>'Arul Kumaran', 'email'=>'arul@luracast.com'  ),
    );
    

    我们需要做的就是将单个数组包装在另一个数组中,键为“author”。例如author.php

    <?php
    class Author {
        function post ($request_data){
            print_r($request_data);
            return $request_data;
        }
        function get() {
            return array('author'=> array(
                        array('id'=>1,  'name'=>'Jac Wright1', 'email'=>'jacwright@gmail.com'),
                        array('id'=>2,  'name'=>'Arul Kumaran3','email'=>'arul@luracast.com')
                   ));
        }
    }
    

    这将为 jsonxml

    获得您想要的确切结果

    作者.xml:

    <?xml version="1.0"?>
    <response>
      <author>
        <id>1</id>
        <name>Jac Wright1</name>
        <email>jacwright@gmail.com</email>
      </author>
      <author>
        <id>2</id>
        <name>Arul Kumaran3</name>
        <email>arul@luracast.com</email>
      </author>
    </response>
    

    作者.json:

    {
      "author": [
        {
          "id": 1,
          "name": "Jac Wright1",
          "email": "jacwright@gmail.com"
        },
        {
          "id": 2,
          "name": "Arul Kumaran3",
          "email": "arul@luracast.com"
        }
      ]
    }
    

    让我解释一下我使用的技术,我使用了上面的 post 方法并发布了我想要的确切 xml 结构并使用 print_r 找到对应的php 结构:)

    这是我在命令行中尝试的 cURL

    curl -X POST http://restler2.dev/test/naming_returned/author.xml -H "Content-Type: text/xml" -d '<response><author><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email></author><author><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email></author></response>'
    

    和回应

    Array
    (
        [author] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [name] => Jac Wright
                        [email] => jacwright@gmail.com
                    )
    
                [1] => Array
                    (
                        [id] => 1
                        [name] => Jac Wright
                        [email] => jacwright@gmail.com
                    )
    
            )
    
    )
    <?xml version="1.0"?>
    <response>
      <author>
        <id>1</id>
        <name>Jac Wright</name>
        <email>jacwright@gmail.com</email>
      </author>
      <author>
        <id>1</id>
        <name>Jac Wright</name>
        <email>jacwright@gmail.com</email>
      </author>
    </response>
    

    为了完整起见,让我把网关 index.php 也放在这里

    <?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->addAPIClass('Author');
    $r->setSupportedFormats('JsonFormat','XmlFormat');
    $r->handle();
    

    【讨论】:

    • 感谢您的回复,很抱歉我的回复晚了(一直在旅行)。
    • 我已经尝试过您之前的建议,这确实将对象包装在具有正确名称的标签中,但它只是将其作为额外级别添加到结构中。我仍然得到 标签,以及包装这些对象的匿名 json 块..?
    • @rogerkk 你是对的,我已经更正了示例并添加了有关我使用的技术的信息
    • 就是这样,你更正的例子就像一个魅力。感谢分享技术!
    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多