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;
}
}
不知道我在哪裏搞亂了!請幫忙!
「_...我得到500內部服務器錯誤,因爲我無法捕獲發佈數據_「你應該更詳細地解釋一下究竟發生了什麼。我可以看到,與'POST'數據問題有關的唯一明顯的事情是您的CURL請求使用'GET'而不是'POST':'CURLOPT_CUSTOMREQUEST =>'GET'' – ndm
對不起,缺乏清晰度!後變量$ _POST ['username']返回。我曾嘗試將CURLOPT_CUSTOMREQUEST =>'GET'更改爲'POST',但是,沒有運氣!在這裏,我的意思是說,從客戶端,而我POST一個用戶名,這不會到達其餘的服務器,即其餘的服務器不會讀取$ _POST – Guns