【问题标题】:Cannot get PUT parameter Phalcon Oauth2 server无法获取 PUT 参数 Phalcon Oauth2 服务器
【发布时间】: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() 函数的真正价值。

标签: php api rest oauth phalcon


【解决方案1】:

您能否更改PUT 请求的Content-Type

PUT 参数有类似的问题,使用Content-Type: application/x-www-form-urlencoded 解决了这个问题。尝试将其添加到 Request 类的 headers 数组中。

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 2015-11-02
    • 2015-06-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多