2013-08-26 41 views
0

我正在用CakePHP 2創建Restful WebService,但是由於我無法捕獲發佈數據,因此我得到500內部服務器錯誤。其餘的服務器是如下:Restful API for CakePHP 2

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 as $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'); 
    } 
} 

和客戶端都按:

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; 
    } 

} 

不知道我在哪裏搞亂了!請幫忙!

+1

「_...我得到500內部服務器錯誤,因爲我無法捕獲發佈數據_「你應該更詳細地解釋一下究竟發生了什麼。我可以看到,與'POST'數據問題有關的唯一明顯的事情是您的CURL請求使用'GET'而不是'POST':'CURLOPT_CUSTOMREQUEST =>'GET'' – ndm

+0

對不起,缺乏清晰度!後變量$ _POST ['username']返回。我曾嘗試將CURLOPT_CUSTOMREQUEST =>'GET'更改爲'POST',但是,沒有運氣!在這裏,我的意思是說,從客戶端,而我POST一個用戶名,這不會到達其餘的服務器,即其餘的服務器不會讀取$ _POST – Guns

回答

2

好的,第二次看後還有一些可疑的事情。如前所述,您的CURL請求使用GET而不是POST

$options = array(
    ... 
    CURLOPT_CUSTOMREQUEST => 'POST', 
    CURLOPT_POSTFIELDS  => $postData, 
); 

另一件事是,你是爲你的CURL呼叫POST數據進行編碼以JSON,但你是在試圖訪問它使用$_POST對方,但不會有任何東西,POST數據將必須是格式化鍵/值查詢字符串才能出現在$_POST中。你必須閱讀php://input相反,這可能是你試圖用

$data = $this->request->data('json_decode'); 

做不過你必須使用CakeRequest::input()爲此目的,當然你必須再使用$data變量而不是$_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'] 
); 

而且使雙確保您CURL請求看起來像預期:

$options = array(
    ... 
    CURLOPT_POSTFIELDS => $postData, 
    CURLINFO_HEADER_OUT => true // supported as 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>';