使用CakePHP发布请求XML
我试图用CakePHP2.3.8对XML数据进行POST请求,但我遇到了一些问题,我认为这是在处理授权问题。首先,我要做的是使用来自控制器中某个操作的XML数据发出POST请求。此post请求执行的操作应在数据库中保存一个条目,以便我知道post请求已成功触发。什么也救不了 以下是两个操作和beforefilter。我确保允许访问beforefilter中的每个操作。xml_post是我访问的触发xml post请求的操作,receive_xml操作是在接收到某个内容时应该在数据库中保存一个条目的操作使用CakePHP发布请求XML,php,xml,cakephp,post,Php,Xml,Cakephp,Post,我试图用CakePHP2.3.8对XML数据进行POST请求,但我遇到了一些问题,我认为这是在处理授权问题。首先,我要做的是使用来自控制器中某个操作的XML数据发出POST请求。此post请求执行的操作应在数据库中保存一个条目,以便我知道post请求已成功触发。什么也救不了 以下是两个操作和beforefilter。我确保允许访问beforefilter中的每个操作。xml_post是我访问的触发xml post请求的操作,receive_xml操作是在接收到某个内容时应该在数据库中保存一个条目
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->authorize = 'Controller';
$this->Auth->allow = array('*');
}
public function xml_post_test(){
$data = array(
'post' => array(
'id' => 1,
'title' => 'This is a test',
'body' => 'Here is something for the body '
)
);
App::uses('HttpSocket', 'Network/Http');
$http = new HttpSocket();
$xml_data = Xml::fromArray($data);
$xml_string = $xml_data->asXML();
$response = $http->post('http://localhost/tests/receive_xml', $xml_string);
debug($response);
}
//save an arbitrary entry in the database a POST request is received (just checking that the xml_post_test method is working
public function receive_xml(){
if($this->request->is('post')){
$this->loadmodel('TestTable');
$this->TestTable->create();
$this->TestTable->save(array('id' => '','data' => 'abc 123'));
}
}
下面是响应(调试)
我不看细节就能看到一个问题-- 应该是
$this->Auth->allow = array();
您需要做的主要事情是将
Content-Type
标题设置为application/xml
。您可能还希望将Accept Type
头设置为相同的值(如果您希望XML响应而不使用Router::parseExtensions()
方法,其中响应类型是从URL中传递的扩展推断出来的)
$this->Auth->allow = array('*');
$this->Auth->allow = array();