Saturday, February 2, 2013

Re: Parse XML string as PHP Array

Try http://book.cakephp.org/2.0/en/development/rest.html#accepting-input-in-other-formats

So, by following http://book.cakephp.org/2.0/en/development/rest.html#the-simple-setup

    //In app/Config/routes.php...      Router::mapResources('seasons');      Router::parseExtensions(array('xml'));

and by adding RequestHandler component to AppController, XML input will be auto-converted into $this->request->data --ready for saving.

Anyway, to convert XML to an array see
http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html#xml-api


On Friday, January 25, 2013 8:19:00 AM UTC+1, Mariano C. wrote:

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 unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: