dtxob80644 2013-08-26 11:25
浏览 52
已采纳

CakePHP 2的Restful API

I am creating a Restful WebService with CakePHP 2 however, i am getting 500 Internal Server Error since i am not able to capture Post Data. The Rest Server is as 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 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');
    }
}

and the 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 where i am messing up! Please help!

  • 写回答

1条回答 默认 最新

  • dongyiba8082 2013-08-26 13:06
    关注

    Ok, after a second look there are some more suspicious things. As already mentioned, your CURL request uses GET instead of POST.

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

    Another thing is that you are encoding the POST data for your CURL call to JSON, but then you are trying to access it on the other side using $_POST, however there won't be anything, POST data would have to be key/value query string formatted in order to appear in $_POST. You have to read php://input instead, which may be what you were trying to do with

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

    However you must use CakeRequest::input() for that purpose, and of course you must then use the $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 that your CURL request looks like expected:

    $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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?