Magento:接收 XML Post 数据
Magento: Receive XML Post Data
在 Magento 中,我正在尝试设置一个路由/控制器,它将接收 XML POST 数据、处理数据并 return 响应。
我的路线设置正确,我的索引控制器设置有 indexAction()。但是,使用 Postman,当我尝试将 POST XML 数据发送到路由时,Mage::app()->getRequest()->getPost()
return 是空的。我也试过 $this->getRequest()->getParams()
也得到了相同的结果。
有什么我遗漏的吗?
getRequest()->getPost()
是 $_POST 变量的包装器
$_POST 设置为:
Content-Type: application/x-www-form-urlencoded
换句话说,对于标准的 Web 表单(发送参数,如 username=admin&pass=mypass)
$_POST 未设置为:
Content-Type:text/xml
所以你不会在 $_POST 中得到你的 xml。
getRequest()->getParams()
包含 $_POST、$_GET 和路由参数,同样你不会在这里得到你的 xml。
您可以检查 Zend_Controller_Request_Http
class 以了解这些方法。
您必须自己解析发布的 xml。你可以像这样检索它
if ($this->getRequest()->isPost() && $this->getRequest()->getHeader('Content-Type') == 'text/xml') { // don't forget to set proper content-type header when making the request
$postedXml = $this->getRequest()->getRawBody();
if (false !== $postedXml) {
// process xml
}
}
在 Magento 中,我正在尝试设置一个路由/控制器,它将接收 XML POST 数据、处理数据并 return 响应。
我的路线设置正确,我的索引控制器设置有 indexAction()。但是,使用 Postman,当我尝试将 POST XML 数据发送到路由时,Mage::app()->getRequest()->getPost()
return 是空的。我也试过 $this->getRequest()->getParams()
也得到了相同的结果。
有什么我遗漏的吗?
getRequest()->getPost()
是 $_POST 变量的包装器
$_POST 设置为:
Content-Type: application/x-www-form-urlencoded
换句话说,对于标准的 Web 表单(发送参数,如 username=admin&pass=mypass)
$_POST 未设置为:
Content-Type:text/xml
所以你不会在 $_POST 中得到你的 xml。
getRequest()->getParams()
包含 $_POST、$_GET 和路由参数,同样你不会在这里得到你的 xml。
您可以检查 Zend_Controller_Request_Http
class 以了解这些方法。
您必须自己解析发布的 xml。你可以像这样检索它
if ($this->getRequest()->isPost() && $this->getRequest()->getHeader('Content-Type') == 'text/xml') { // don't forget to set proper content-type header when making the request
$postedXml = $this->getRequest()->getRawBody();
if (false !== $postedXml) {
// process xml
}
}