This post assumes that you have read about Zend_Rest_Server in the the Zend Framework manual and my previous posts regarding Zend_Rest: Here and here.
My EmprestServer class would have something like this:
<?php class EmprestServer { //more code here... public function getEmployeesWithOrgCode($orgcode,$apikey) { $emp = new Employee(); $xmlString = '<?xml version="1.0" standalone="yes"?><getEmployeesWithOrgCode></getEmployeesWithOrgCode>'; $xml = new SimpleXMLElement($xmlString); if(!$this->checkKey($apikey)) { //throw new Exception('Key is invalid.'); $xml->addChild('response', 'Key is invalid.'); $xml->addChild('status', 'failed'); return $xml; } //$emp->getEmployeesWithOrgCode($orgcode) is from my Model if($data = $emp->getEmployeesWithOrgCode($orgcode)) { $data = $data->toArray(); } else { //throw new Exception('Employee ID not found.'); $xml->addChild('response', 'Key is invalid.'); $xml->addChild('status', 'failed'); return $xml; } $dataCount = count($data); for($i=0;$i<$dataCount;++$i) { $emp = $xml->addChild('emp'); foreach($data[$i] AS $key=>$value){ $emp->addChild($key, utf8_encode(htmlspecialchars($value))); } } $xml->addChild('status', 'success'); return $xml; } //more code here... } |
This is because by default, Zend_Rest does not have capability to “parse” multiple requests. Using something like this (next code block) would not work because I have multiple arrays returned from a query.
Also, take note of the $emp->addChild($key, utf8_encode(htmlspecialchars($value))); — the htmlspecialchars() and utf8_encode(). Somehow, things break when you have an ampersand (&) in $value, so to go around this problem you use htmlspecialchars() on it and then encode it in utf using utf8_encode().
/* NOTE: This will only work if one row was returned from my query. If you are handling multiple rows from a query, then use the one above. */ public function getByShortname($shortname,$apikey) { //return $this->getData(array('shortname'=>$shortname)); $emp = new Employee(); $data = array(); if(!$this->checkKey($apikey)) { //if($apikey == '1234') { throw new Exception('Key is invalid.'); } if(!$data = $emp->getEmployeeByShortname($shortname)) throw new Exception('Employee ID not found.'); $data = $data->toArray(); return $data; } |
Using the Rest server with Zend_Rest_Client with multiple returned rows would look something like this:
$rest = new Zend_Rest_Client('http://myhost/api/emp'); $rest->getEmployeesWithOrgCode(); $rest->orgcode('ENGINEERS'); $rest->apikey('1234'); $result = $rest->get(); if($result->status!='failed') { foreach($result AS $row) { echo $row->emp_id.' '.$row->emp_shortname; } } else { echo $result->response().' '.$result->status(); } |
While a single result would only have something like this:
$rest = new Zend_Rest_Client('http://myhost/api/emp'); $rest->getManagerById(); $rest->id('1124921'); $rest->apikey('1234'); $result = $rest->get(); if($result->status()=='success') { echo $result->emp_id() .' '. $result->emp_shortname().' '. $result->status(); } else { echo $result->response().' '.$result->status(); } |
So there you have it. I showed 2 examples. One is building a custom XML response using SimpleXMLElement; while other next one shows the “usual” way of handling a single result from a query.