php - Restful API for CakePHP 2 -
i creating restful webservice cakephp 2 however, getting 500 internal server error since not able capture post data. rest server below:
app::import ( 'vendor', 'exchangefunctions', array ('file'=> 'exchange/exchangefunctions.php')); class exchangecontroller extends appcontroller { public $components = array('requesthandler'); public function index() { $exchange = new exchangefunctions(); $data = $this->request->data('json_decode'); $exchange->username = $_post['username']; $exchange->password = $_post['password']; $emaillist = $exchange->listemails(); $response = new stdclass(); $response->emaillist = $emaillist; foreach($emaillist->messages $listid => $email) { $tempemail = $exchange->getemailcontent( $email->id, $email->changekey, true, $_post['attachmentpath'] ); $response->emails[$tempemail['attachmentcode']] = $tempemail; } $this->set('response', $response); $this->set('_serialize','response'); } }
and client goes as:
class apitestcontroller extends appcontroller { public function index() { $this->layout = 'ajax'; $jrequesturlprefix = 'http://localhost/ewsapi/'; $posturl = $jrequesturlprefix."exchange/index.json"; $postdata = array( 'username' => 'username', 'password' => 'password', 'attachmentpath'=> $_server['document_root'] . $this->base . directory_separator . 'emaildownloads' . directory_separator . 'attachments' ); $postdata = json_encode($postdata); pr($postdata); $ch = curl_init( $posturl ); $options = array( curlopt_returntransfer=> true, curlopt_httpheader => array( 'content-type: application/json', 'content-length: ' . strlen($postdata) ), curlopt_customrequest => 'get', curlopt_postfields => $postdata, ); curl_setopt_array( $ch, $options ); $jsonstring = curl_exec($ch); curl_close($ch); $data = json_decode($jsonstring, false); echo $jsonstring; } }
not sure messing up! please help!
ok, after second there more suspicious things. mentioned, curl
request uses get
instead of post
.
$options = array( ... curlopt_customrequest => 'post', curlopt_postfields => $postdata, );
another thing encoding post
data curl
call json
, trying access on other side using $_post
, there won't anything, post
data have key/value query string formatted in order appear in $_post
. have read php://input
instead, may trying with
$data = $this->request->data('json_decode');
however must use cakerequest::input()
purpose, , of course must use $data
variable instead of $_post
$data = $this->request->input('json_decode'); $exchange->username = $data['username']; $exchange->password = $data['password']; .... $tempemail = $exchange->getemailcontent( $email->id, $email->changekey, true, $data['attachmentpath'] );
also make double sure curl
request looks expected:
$options = array( ... curlopt_postfields => $postdata, curlinfo_header_out => true // supported of php 5.1.3 ); curl_setopt_array($ch, $options); $result = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); echo '<pre>'; print_r($info); echo '</pre>';
Comments
Post a Comment