I'm creating a RESTful webservice, right now I'm facing the insertion of the new resource (the Season
resource). This is the body of the POST request:
<request>
<Season>
<title>new title</title>
</Season>
</request>
and this is the controller that effectively perform the insertion:
public function add() {
// i feel shame for this line
$request = json_decode(json_encode((array) simplexml_load_string($this->request->input())), 1);
if (!empty($request)) {
$obj = compact("request");
if ($this->Season->save($obj['request'])) {
$output['status'] = Configure::read('WS_SUCCESS');
$output['message'] = 'OK';
} else {
$output['status'] = Configure::read('WS_GENERIC_ERROR');
$output['message'] = 'KO';
}
$this->set('output', $output);
}
$this->render('generic_response');
}
The code works pretty well, but as I wrote in the snippet above I consider the first line of the controller really ugly, so, the question is: How can I parse XML string as PHP Array?
xml_parse_into_struct()
– clover Jan 24 '13 at 23:43compact("request")
then$obj['request']
?? – nickb Jan 24 '13 at 23:44