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!

dpo69086
dpo69086 抱歉缺乏清晰度!post变量$_POST['username']返回。我确实尝试将CURLOPT_CUSTOMREQUEST=>'GET'更改为'POST'但是,没有运气!这里我的意思是说,从客户端,当我POST一个用户名,但没有到达其余服务器,即其余服务器不读取$_POST
接近 7 年之前 回复
douguizhuang8276
douguizhuang8276 “...我得到500内部服务器错误,因为我无法捕获发布数据”你应该更准确地解释一下究竟/究竟发生了什么。我可以看到与POST数据问题相关的唯一明显的事情是你的CURL请求使用GET而不是POST:CURLOPT_CUSTOMREQUEST=>'GET'
接近 7 年之前 回复

1个回答



好的,经过第二次看,还有一些可疑的东西。 如前所述,您的 CURL </ code>请求使用 GET </ code>而不是 POST </ code>。</ p>

  $ options  = array(
...
CURLOPT_CUSTOMREQUEST =&gt;'POST',
CURLOPT_POSTFIELDS =&gt; $ postData,
);
</ code> </ pre>

另一件事 是你正在为 CURL </ code>调用 JSON </ code>编码 POST </ code>数据,但是你试图在另一边使用< 代码> $ _ POST </ code>,但是不会有任何内容, POST </ code>数据必须是键/值查询字符串格式化才能出现在 $ _ POST </ code>中 。 您必须阅读 php:// input </ code>,这可能是您尝试使用</ p>

  $ data = $ this-&gt;  request-&gt; data('json_decode'); 
</ code> </ pre>

但是你必须使用 CakeRequest :: input()</ code> 为此目的,当然你必须使用 $ data </ code>变量而不是 $ _ POST </ code> </ p>

  $ data = $ this-&gt; request-&gt; input('  json_decode'); 
$ exchange-&gt; username = $ data ['username'];
$ exchange-&gt; password = $ data ['password'];

....

$ tempEmail = $ exchange-&gt; getEmailContent(
$ email-&gt; Id,
$ email-&gt; ChangeKey,
TRUE,
$ data ['attachmentPath']
);
</ code > </ pre>

还要确保您的 CURL </ code>请求看起来像预期的那样:</ p>

  $ options = array(

...
CURLOPT_POSTFIELDS =&gt; $ postData,
CURLINFO_HEADER_OUT =&gt; true //从PHP 5.1.3开始支持
);
curl_setopt_array($ ch,$ options);
$ result = curl_exec($ ch);
$ info = curl_getinfo($ ch);
curl_close($ ch );

echo'&lt; pre&gt;';
print_r($ info);
echo'&lt; / pre&gt;';
</ code> </ pre>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐