xmlとjson形式で値を返す

PEAR::XML_Serializerを使う

yum -y install php-pear

pear install --alldeps xml_serializer-beta

<?php

require_once("/usr/share/pear/XML/Serializer.php");

// 出力したい内容

$data = array( 0 => array('_attributes' => array('id'=> '1'),

'name' => 'SmartPhone',

0 => 'iPhone',

1 => 'Android'

),

1 => array('_attributes' => array('id'=> '2'),

'name' => 'ボード',

0 => 'チェス',

1 => 'リバーシ'

)

);

// XML生成時のオプション

$options = array(

"indent" => "\t",

"linebreak" => "\n",

"typeHints" => false,

"addDecl" => true,

"encoding" => "UTF-8",

"rootName" => "data",

"rootAttributes" => array(),

"defaultTagName" => array('data'=> 'drink',

'drink' => 'item'

),

"attributesArray" => "_attributes"

);

$serializer = new XML_Serializer($options);

$serializer->serialize($data);

$result = $serializer->getSerializedData();

header("Content-Type: text/xml; charset=utf-8");

echo $result;

スクリーンショット 2014-06-30 17.34.27

参考サイト

http://junqi.hatenablog.com/entry/20101128/1290960684