无法获取 PUT 参数 Phalcon Oauth2 服务器
Posted
技术标签:
【中文标题】无法获取 PUT 参数 Phalcon Oauth2 服务器【英文标题】:Cannot get PUT parameter Phalcon Oauth2 server 【发布时间】:2016-01-11 18:48:52 【问题描述】:我在获取 PUT 请求数据时遇到问题。
我关注这个骨架项目: Phalcon-api-oauth2
当我发送 PUT 请求时, 结果总是空数组()
我已经尝试更改了几个代码:
微.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;
我在 vendor/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);
也加入了 vendor/league/oauth2-server/src/League/OAuth2/Server/Util/RequestInterface.php
public function put($index = null);
然后修改了这个类 vendor/league/oauth2-server/src/League/OAuth2/Server/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);
....
谁能告诉我代码有什么问题?
干杯。
【问题讨论】:
是区分大小写的问题吗? @JamesFenwick 我不这么认为,似乎 oauth 服务器的库取代了 getPut() 函数的真正价值。 【参考方案1】:您能否更改PUT
请求的Content-Type
?
PUT
参数有类似的问题,使用Content-Type: application/x-www-form-urlencoded
解决了这个问题。尝试将其添加到 Request
类的 headers 数组中。
【讨论】:
以上是关于无法获取 PUT 参数 Phalcon Oauth2 服务器的主要内容,如果未能解决你的问题,请参考以下文章
使用 PUT http 动词时通过查询参数传递 Oauth2 令牌?
Passport Laravel Exception file_put_contents(/secret-keys/oauth\oauth-public.key): 无法打开流