所以在我做完博客教程后我开始玩cakePHP(是的,我是新的),我想做一些复杂的事情。就像,这是一个评论表单,它很简单,它包含Name和Comment。我想将数据发送到同一主机中的另一个应用程序,这将在DB中保存此注释。目前,评论/ add.ctp保存它。
感谢您的任何建议!
所以theresControlControll.php
<?php
class CommentsController extends AppController{
public $components = 'Session'
public function add(){
if($this->request->is('POST')){
$this->Comment->create();
if($this->Comment->save($this->request->data)){
$this->Session->setFlash('Your comment is saved!')
}
}
}
}
?>
还有评论/ add.ctp文件
<?php
echo $this->Form->create('Comment');
echo $this->Form->input('name', array(
'label' => 'Your Name'
));
echo $this->Form->input('commenttext',array(
'label' => 'Your Comment'
));
echo $this->Form->end('Submit');
?>
答案 0 :(得分:0)
解决方案 HttpSocket
CakePHP包含一个HttpSocket
类,可以轻松地用于发出请求。这是与外部Web服务或远程apis通信的好方法。
// in your controller
App::uses('HttpSocket', 'Network/Http'); // This should be at the top of your Controller
class CommentsController extends AppController{
public $components = 'Session'
public function add(){
if($this->request->is('POST')){
$this->Comment->create();
$HttpSocket = new HttpSocket();
$response = $HttpSocket->post('http://example.com/add', $this->request->data));
// Get the status code for the response.
$code = $results->code;
if($code == 200) {
$this->Session->setFlash('Your comment is saved!');
} else {
$this->Session->setFlash('Opps! Somthing is wrong!');
}
}
}
}