Thursday, January 24, 2013

Parse XML string as PHP Array

I'm creating a RESTful webservice, right now I'm facing the insertion of the new resource (the Seasonresource). 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?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: