Php 在magento 2 api的控制器中使用post方法获取发送的值
我无法使用http请求从post方法获取发送的值 我使用get方法获取值,但我需要使用post方法获取它 我没有使用任何视图,我想调用HTTPURL,并使用post方法在控制器中发送一些数据 这就是我的控制器的样子Php 在magento 2 api的控制器中使用post方法获取发送的值,php,api,magento2,Php,Api,Magento2,我无法使用http请求从post方法获取发送的值 我使用get方法获取值,但我需要使用post方法获取它 我没有使用任何视图,我想调用HTTPURL,并使用post方法在控制器中发送一些数据 这就是我的控制器的样子 namespace Spaarg\eMenuApi\Controller\Index; class Products extends \Magento\Framework\App\Action\Action { public function __construct(\Ma
namespace Spaarg\eMenuApi\Controller\Index;
class Products extends \Magento\Framework\App\Action\Action
{
public function __construct(\Magento\Framework\App\Action\Context $context)
{
return parent::__construct($context);
}
public function execute()
{
//$token = $this->getRequest()->getPostValue();
$token = $this->getRequest()->getPost();
}
}
我是magento 2的新手,我不明白问题出在哪里。
如果有人能帮上忙,那就太好了。这可能与http请求的
内容类型有关,其中Magento只理解Json和Xml(这是非常重要的)。如果您在请求中使用了不同的内容类型
,或者您的数据与标题中声明的类型不匹配,则getPost()
将不起作用
作为备用方案,您始终可以通过以下方式获取所有POST数据:
public function execute()
{
$postData = file_get_contents("php://input");
}
请记住,这将获得原始字符串,因此您可能需要在使用它之前对其进行相应的处理(例如使用json\u decode()
或类似的东西)
有关此操作的更多信息,请检查