的example you have pointed out返回以下
return array(
array('id'=>1, 'name'=>'Jac Wright', 'email'=>'[email protected]'),
array('id'=>2, 'name'=>'Arul Kumaran', 'email'=>'[email protected]' ),
);
所有我們需要做的是包裹單獨的陣列中的關鍵「作者」下的另一個數組。例如,在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'=>'[email protected]'),
array('id'=>2, 'name'=>'Arul Kumaran3','email'=>'[email protected]')
));
}
}
這得到你想要兩個JSON和XML確切的結果
author.xml:
<?xml version="1.0"?>
<response>
<author>
<id>1</id>
<name>Jac Wright1</name>
<email>[email protected]</email>
</author>
<author>
<id>2</id>
<name>Arul Kumaran3</name>
<email>[email protected]</email>
</author>
</response>
author.json:
{
"author": [
{
"id": 1,
"name": "Jac Wright1",
"email": "[email protected]"
},
{
"id": 2,
"name": "Arul Kumaran3",
"email": "[email protected]"
}
]
}
讓我解釋一下,我用以及技術,我用上述方法後,並張貼確切xml
結構我想和使用print_r
找到相應的php
結構:)
這裏是我的命令試圖捲曲線
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>[email protected]</email></author><author><id>1</id><name>Jac Wright</name><email>[email protected]</email></author></response>'
和響應
Array
(
[author] => Array
(
[0] => Array
(
[id] => 1
[name] => Jac Wright
[email] => [email protected]
)
[1] => Array
(
[id] => 1
[name] => Jac Wright
[email] => [email protected]
)
)
)
<?xml version="1.0"?>
<response>
<author>
<id>1</id>
<name>Jac Wright</name>
<email>[email protected]</email>
</author>
<author>
<id>1</id>
<name>Jac Wright</name>
<email>[email protected]</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();
感謝您的回覆,並對我遲到的回覆感到遺憾(外出旅行)。 – rogerkk
我已經嘗試了你之前提出的建議,並且確實將對象包裝在標籤中,但名稱正確,但它只是將其作爲額外的級別添加到結構中。我仍然得到- 標籤,匿名json塊封裝這些對象..? –
rogerkk
@rogerkk你說得對,我已經修正了這個例子,並且增加了我使用的技術的相關信息 – Arul