Codeigniter Rest库中不支持的协议

e0bqpujr  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(233)

我正在尝试为现有数据库构建一个REST API。有人建议我使用Chris Kacerguis的Rest API,但他的最新版本出现了一些持续的错误。然后我决定使用一个以前的版本,它仍然可以工作,但有一个“不支持的协议”错误。我从他的基本GET示例中复制粘贴了代码,仍然有相同的不支持的协议错误。

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

use chriskacerguis\RestServer\RestController;

class Api extends RestController {

    function __construct()
    {
        // Construct the parent class
        parent::__construct();
    }

    public function users_get()
    {
        // Users from a data store e.g. database
        $users = [
            ['id' => 0, 'name' => 'John', 'email' => 'john@example.com'],
            ['id' => 1, 'name' => 'Jim', 'email' => 'jim@example.com'],
        ];

        $id = $this->get( 'id' );

        if ( $id === null )
        {
            // Check if the users data store contains users
            if ( $users )
            {
                // Set the response and exit
                $this->response( $users, 200 );
            }
            else
            {
                // Set the response and exit
                $this->response( [
                    'status' => false,
                    'message' => 'No users were found'
                ], 404 );
            }
        }
        else
        {
            if ( array_key_exists( $id, $users ) )
            {
                $this->response( $users[$id], 200 );
            }
            else
            {
                $this->response( [
                    'status' => false,
                    'message' => 'No such user found'
                ], 404 );
            }
        }
    }
}

https://github.com/chriskacerguis/codeigniter-restserver

hjzp0vay

hjzp0vay1#

在REST配置文件中选中此选项

$config['force_https'] = TRUE;
lp0sw83n

lp0sw83n2#

我得到这个问题,因为我的网站是在负载均衡器和请求进入API从负载均衡器作为http:(端口80)
解决方案是关闭force_https,因此:

$config['force_https']           = FALSE;
vfwfrxfs

vfwfrxfs3#

如果你在本地运行这个,那么你必须在REST配置文件中更改这个选项:

$config['force_https'] = TRUE;

相关问题