0

我正在使用 CakePHP 2 创建一个 Restful WebService,但是,由于我无法捕获发布数据,我收到 500 内部服务器错误。休息服务器如下:

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

}

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

4

1 回答 1

2

好的,经过第二次查看后,还有一些可疑的事情。如前所述,您的CURL请求使用GET而不是POST.

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

另一件事是,您正在对调用POST数据进行编码,但随后您尝试在另一端使用 访问它,但是不会有任何内容,数据必须是键/值查询字符串格式化才能出现在. 你必须改为阅读,这可能是你试图做的CURLJSON$_POSTPOST$_POSTphp://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>';
于 2013-08-26T13:06:51.037 回答