我有一个问题需要获取PUT请求数据。
我遵循这个骨架项目: Phalcon-api-oauth2
当我发送PUT请求时, 结果总是空的array()
我试过更改了几个代码:
micro.php
之前
public function setRoutes($file) {
if (!file_exists($file)) {
throw new \Exception('Unable to load routes file');
}
$routes = include($file);
if (!empty($routes)) {
foreach($routes as $obj) {
switch($obj['method']) {
case 'get':
$this->get($obj['route'], $obj['handler']);
break;
case 'post':
$this->post($obj['route'], $obj['handler']);
break;
case 'delete':
$this->delete($obj['route'], $obj['handler']);
break;
case 'put':
$this->head($obj['route'], $obj['handler']);
break;
case 'options':
$this->options($obj['route'], $obj['handler']);
break;
case 'patch':
$this->patch($obj['route'], $obj['handler']);
break;
default:
break;
}
}
}
}
之后
public function setRoutes($file) {
if (!file_exists($file)) {
throw new \Exception('Unable to load routes file');
}
$routes = include($file);
if (!empty($routes)) {
foreach($routes as $obj) {
switch($obj['method']) {
case 'get':
$this->get($obj['route'], $obj['handler']);
break;
case 'post':
$this->post($obj['route'], $obj['handler']);
break;
case 'delete':
$this->delete($obj['route'], $obj['handler']);
break;
case 'put':
$this->put($obj['route'], $obj['handler']);
break;
case 'options':
$this->options($obj['route'], $obj['handler']);
break;
case 'patch':
$this->patch($obj['route'], $obj['handler']);
break;
default:
break;
}
}
}
}
我在供应商/ Oauth2 / src / Oauth2 / Server / Storage / Pdo / Mysql / Request.php中为PUT请求添加了新方法
public function put($index = NULL)
{
// print_r($this->request->getPut()); // I can see the PUT request data here
return $this->request->getPut($index);
}
也加入了 供应商/联盟/的oauth2 - 服务器/ SRC /联赛/ OAuth2用户/服务器/的Util / RequestInterface.php
public function put($index = null);
然后修改了这个班级 供应商/联盟/的oauth2 - 服务器/ SRC /联赛/ OAuth2用户/服务器/的Util / Request.php
class Request implements RequestInterface
{
protected $get = array();
protected $post = array();
protected $cookies = array();
protected $files = array();
protected $server = array();
protected $headers = array();
protected $put = array(); // new property added
// new $put parameter added */
public function __construct(array $get = array(), array $post = array(), array $put = array(), array $cookies = array(), array $files = array(), array $server = array(), $headers = array())
{
$this->get = $get;
$this->post = $post;
$this->put = $put;
$this->cookies = $cookies;
$this->files = $files;
$this->server = $server;
if (empty($headers)) {
$this->headers = $this->readHeaders();
} else {
$this->headers = $this->normalizeHeaders($headers);
}
}
/* new method added */
public function put($index = null, $default = null)
{
return $this->getPropertyValue('put', $index, $default);
}
....
任何人都可以让我知道代码有什么问题吗?
干杯。
答案 0 :(得分:0)
您是否可以更改Content-Type
请求的PUT
?
我的PUT
参数存在类似问题,使用Content-Type: application/x-www-form-urlencoded
解决了这个问题。尝试将其添加到Request
类的标题数组中。