php - Phil Sturgeon 的 REST 服务器,Codeigniter3,错误消息在 PUT 上没有返回

标签 php codeigniter rest codeigniter-restserver

我正在使用 Phil Sturgeon 的 REST 服务器、CI3 和 POSTMAN 进行调试。我发送了包含以下信息的 PUT,但是,我没有收到预期的错误消息。

这是我的 form_validation.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
  'student_put' => array(
    array('field' => 'email_address', 'label' => 'email_address', 'rules' => 'trim|required|valid_email'),
    array('field' => 'password', 'label' => 'password', 'rules' => 'trim|required|min_length[8]|max_length[16]'),
    array('field' => 'first_name', 'label' => 'first_name', 'rules' => 'trim|required|max_length[50]'),
    array('field' => 'last_name', 'label' => 'last_name', 'rules' => 'trim|required|max_length[50]'),
    array('field' => 'phone_number', 'label' => 'phone_number', 'rules' => 'trim|required|alpha_dash'),
  )
);

?>

这是我的 Controller Api.php 中的方法:

function student_put(){
    $this->form_validation->set_data($this->put());
    // these are the rules set in config/form_validation.php
    if ($this->form_validation->run('student_put') != FALSE) {
        die('good data');
    } else { 
        $this->response( 
            array(
                'status'=> 'failure', 
                'message'=> $this->form_validation->get_errors_as_array(),
                ), 
            REST_Controller::HTTP_BAD_REQUEST  
        );
    }
}

这是在我的库文件夹中的 MY_Form_validation.php:

<?php

class MY_Form_validation extends CI_Form_validation {

  function __construct($rules = array()) {
      parent::__construct($rules);
      $this->ci =& get_instance();
  }

  public function get_errors_as_array() {
      return $this->_error_array;
  }

  public function get_config_rules() {
      return $this->_config_rules;
  }

  public function get_field_names($form) {
      $field_names = array();
      $rules = $this->get_config_rules();
      $rules = $rules[$form];
      foreach ($rules as $index=> $info) {
          $field_names[] = $info['field'];
      }
      return $field_names;
  }
}

当我在 POSTMAN 中添加以下内容时:

X-API-KEY          123456
first_name         test
email_address      abc

这是我得到的结果:

{
  "status": "failure",
  "message": []
}

但我应该收到验证错误。

作为调试步骤,我已经确认: - 没有授权错误 - 正在读取 form_validation.php - 如果我改变:

'message'=> $this->form_validation->get_errors_as_array(),

'message'=> 'test',

postman 返回:

{
"status": "failure",
"message": "test"
}

非常感谢任何帮助。

最佳答案

你必须阅读这个链接,

http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814

如果使用apikey,则必须设置

    $config['rest_auth'] = 'basic'
    $config['rest_enable_keys'] = TRUE;

同时在数据库中创建一个表用于存储 api key

CREATE TABLE `keys` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) NOT NULL,
    `key` VARCHAR(40) NOT NULL,
    `level` INT(2) NOT NULL,
    `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
    `is_private_key` TINYINT(1)  NOT NULL DEFAULT '0',
    `ip_addresses` TEXT NULL DEFAULT NULL,
    `date_created` INT(11) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入那个数据库最少1行,重要的列唯一键,就是apikey

出于安全原因,apikey 必须包含 40 位字母数字

再一次,你必须阅读文档,以及 application/config 中的 rest.php

    $config['rest_valid_logins'] = ['admin' => '1234'];

该登录名是默认设置的,因此您必须将该登录名插入客户端请求的 header 等

    http_user           'admin'
    http_pass           '1234'
    X-API-KEY           '123456'
    first_name          test
    email_address       abc

如果那个标题不起作用,试试这个

    http_user           'admin'
    http_pass           '1234'
    api_name            'X-API-KEY'
    api_key             '123456'
    first_name          test
    email_address       abc

如果你之前尝试过这样的请求

    $config['rest_auth'] = FALSE

实际上你还没有保护你的 api 网络服务

关于php - Phil Sturgeon 的 REST 服务器,Codeigniter3,错误消息在 PUT 上没有返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671576/

相关文章:

php - 计算单个查询中另一个表的行数

codeigniter - 如何构建具有层次结构的 CodeIgniter URL?

javascript - 原生 javascript 休息 api 调用

api - 进行 REST API 调用时,您是否可以在 url 中包含授权 key ,就像在 GET 请求中一样?

php - 无法让 SSL 站点正确加载 JS + CSS。 throw 409(冲突)

java - Spring REST Controller 在具有空格或逗号等特殊字符时理解字符串数组

Php str_replace 不适用于特殊字符

php - 选择时间与 future 日期相同的数据

php - Jquery 在提交后显示一次

php - Codeigniter 当前登录用户