Restful API for CakePHP 2



我正在用CakePHP 2创建一个Restful WebService,但是,由于无法捕获Post Data,我收到了500内部服务器错误。Rest服务器如下:

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

不知道我在哪里搞砸了!请帮忙!

好吧,再看一眼,还有一些可疑的东西。如前所述,您的CURL请求使用GET而不是POST

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

另一件事是,您正在为CURLJSON的调用编码POST数据,但随后您试图使用$_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>';

最新更新