CodeIgniter GuzzleHttp 403 禁止
Posted
技术标签:
【中文标题】CodeIgniter GuzzleHttp 403 禁止【英文标题】:CodeIgniter GuzzleHttp 403 Forbidden 【发布时间】:2020-12-12 22:16:11 【问题描述】:所以我创建了 2 个 CodeIgniter 应用程序;一个应用程序是 API,另一个是客户端应用程序。我已经在 cPanel 中部署了这两个应用程序(在部署之前我已经测试了这两个应用程序并且它们都运行良好)。我已经使用 Postman 测试了 API,它工作得很好,但是当我尝试使用我的客户端应用程序(使用 GuzzleHttp v6.5.5)访问 API 时,我不断收到 403 Forbidden 错误。
控制器:
public function login()
if (isset($this->session->token))
$this->session->set_flashdata('danger', 'You are logged in.');
redirect(base_url('main/page_login'));
$input = $this->input->post(null, TRUE);
$response = $this->main_model->login($input);
if ($response['status'] != 200)
$this->session->set_flashdata('danger', $response['message']);
redirect(base_url('main/page_login'));
$response['user']['full_name'] = $response['user']['first_name'] . ' ' . $response['user']['last_name'];
$this->session->set_userdata('user', $response['user']);
$this->session->set_userdata('roles', $response['id_roles']);
$this->session->set_userdata('token', $response['token']);
$this->session->set_userdata('is_admin', FALSE);
foreach ($this->session->roles as $roles)
if (in_array($roles['id_role'], array(1, 2)))
$this->session->is_admin = TRUE;
$this->session->set_flashdata('login', 'Logged in');
if ($this->session->flashdata('referrer') != '')
redirect(base_url($this->session->flashdata('referrer')));
else
redirect(base_url('main'));
型号:
<?php
//Guzzle configuration
use GuzzleHttp\Client;
class Main_model extends CI_Model
private $_client;
private $_base_uri;
public function __construct()
$this->_client = new Client();
$this->_client_service = 'frontend-client';
$this->_auth_key = 'rest-api';
$this->_base_uri = 'https://myapp.com/api';
function login($params)
$response = $this->_client->request('POST', $this->_base_uri . '/auth/login', [
'headers' => [
'Client-Service' => $this->_client_service,
'Auth-Key' => $this->_auth_key
],
'body' => json_encode($params)
]);
return json_decode($response->getBody()->getContents(), true);
?>
错误信息:
An uncaught Exception was encountered
Type: GuzzleHttp\Exception\ClientException
Message: Client error: `POST https://myapp.com/api/auth/login` resulted in a `403 Forbidden` response: File: /home/u61800/public_html/client/vendor/guzzlehttp/guzzle/src/Middleware.php
Line: 65
Function: create
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 203
Function: GuzzleHttp\closure
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 156
Function: callHandler
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/TaskQueue.php
Line: 47
Function: GuzzleHttp\Promise\closure
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 246
Function: run
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 223
Function: invokeWaitFn
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 267
Function: waitIfPending
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 225
Function: invokeWaitList
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 62
Function: waitIfPending
File: /home/u61800/public_html/client/vendor/guzzlehttp/guzzle/src/Client.php
Line: 182
Function: wait
File: /home/u61800/public_html/client/application/models/Main_model.php
Line: 31
Function: request
File: /home/u61800/public_html/client/application/controllers/Main.php
Line: 92
Function: login
File: /home/u61800/public_html/client/index.php
Line: 315
Function: require_once
我尝试在标头中使用 User-Agent、Accept 和 Accept-Encoding,但仍然收到 403 Forbidden 错误。我在代码中做错了什么?提前致谢。
更新: 回应@Ivan Ivanov 的评论;在我将“body”更改为“json”之后,当我运行它而不是 403 时,系统现在给我 500 内部服务器错误。但是话又说回来,如果我尝试通过邮递员登录它就可以了。
An uncaught Exception was encountered
Type: GuzzleHttp\Exception\ServerException
Message: Server error: `POST https://myapp.com/api/auth/login` resulted in a `500 Internal Server Error` response:
An uncaught Exception was encountered< (truncated...)
Filename: /home/u61800/public_html/client/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php
Line Number: 113
Backtrace:
File: /home/u61800/public_html/client/vendor/guzzlehttp/guzzle/src/Middleware.php
Line: 65
Function: create
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 203
Function: GuzzleHttp\closure
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 156
Function: callHandler
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/TaskQueue.php
Line: 47
Function: GuzzleHttp\Promise\closure
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 246
Function: run
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 223
Function: invokeWaitFn
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 267
Function: waitIfPending
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 225
Function: invokeWaitList
File: /home/u61800/public_html/client/vendor/guzzlehttp/promises/src/Promise.php
Line: 62
Function: waitIfPending
File: /home/u61800/public_html/client/vendor/guzzlehttp/guzzle/src/Client.php
Line: 182
Function: wait
File: /home/u61800/public_html/client/application/models/Main_model.php
Line: 31
Function: request
File: /home/u61800/public_html/client/application/controllers/Main.php
Line: 92
Function: login
File: /home/u61800/public_html/client/index.php
Line: 315
Function: require_once
另外,$this->_client 似乎不为空。
object(GuzzleHttp\Client)#21 (1) ["config":"GuzzleHttp\Client":private]=> array(8) ["handler"]=> object(GuzzleHttp\HandlerStack)#22 (3) ["handler":"GuzzleHttp\HandlerStack":private]=> object(Closure)#29 (2) ["static"]=> array(2) ["default"]=> object(Closure)#27 (2) ["static"]=> array(2) ["default"]=> object(GuzzleHttp\Handler\CurlMultiHandler)#23 (6) ["factory":"GuzzleHttp\Handler\CurlMultiHandler":private]=> object(GuzzleHttp\Handler\CurlFactory)#24 (2) ["handles":"GuzzleHttp\Handler\CurlFactory":private]=> array(0) ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=> int(50) ["selectTimeout":"GuzzleHttp\Handler\CurlMultiHandler":private]=> int(1) ["active":"GuzzleHttp\Handler\CurlMultiHandler":private]=> NULL ["handles":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) ["delays":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) ["options":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) ["sync"]=> object(GuzzleHttp\Handler\CurlHandler)#25 (1) ["factory":"GuzzleHttp\Handler\CurlHandler":private]=> object(GuzzleHttp\Handler\CurlFactory)#26 (2) ["handles":"GuzzleHttp\Handler\CurlFactory":private]=> array(0) ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=> int(3) ["parameter"]=> array(2) ["$request"]=> string(10) "" ["$options"]=> string(10) "" ["streaming"]=> object(GuzzleHttp\Handler\StreamHandler)#28 (1) ["lastHeaders":"GuzzleHttp\Handler\StreamHandler":private]=> array(0) ["parameter"]=> array(2) ["$request"]=> string(10) "" ["$options"]=> string(10) "" ["stack":"GuzzleHttp\HandlerStack":private]=> array(4) [0]=> array(2) [0]=> object(Closure)#30 (1) ["parameter"]=> array(1) ["$handler"]=> string(10) "" [1]=> string(11) "http_errors" [1]=> array(2) [0]=> object(Closure)#31 (1) ["parameter"]=> array(1) ["$handler"]=> string(10) "" [1]=> string(15) "allow_redirects" [2]=> array(2) [0]=> object(Closure)#32 (1) ["parameter"]=> array(1) ["$handler"]=> string(10) "" [1]=> string(7) "cookies" [3]=> array(2) [0]=> object(Closure)#33 (1) ["parameter"]=> array(1) ["$handler"]=> string(10) "" [1]=> string(12) "prepare_body" ["cached":"GuzzleHttp\HandlerStack":private]=> NULL ["allow_redirects"]=> array(5) ["max"]=> int(5) ["protocols"]=> array(2) [0]=> string(4) "http" [1]=> string(5) "https" ["strict"]=> bool(false) ["referer"]=> bool(false) ["track_redirects"]=> bool(false) ["http_errors"]=> bool(true) ["decode_content"]=> bool(true) ["verify"]=> bool(true) ["cookies"]=> bool(false) ["idn_conversion"]=> bool(true) ["headers"]=> array(1) ["User-Agent"]=> string(38) "GuzzleHttp/6.5.5 curl/7.62.0 PHP/7.4.8"
【问题讨论】:
【参考方案1】:你应该改变这一行:
'body' => json_encode($params)
用这个:
'json' => json_encode($params)
因为Guzzle
期望'json' key
在POST array
中。
还有var_dump( $this->_client )
看看你有没有客户。
【讨论】:
我已经根据您的评论更新了我的帖子。以上是关于CodeIgniter GuzzleHttp 403 禁止的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PHP Codeigniter 中获取 40 岁以上的人员列表
laravel 类 'GuzzleHttp\Client' 未找到
ovh/ovh 需要 guzzlehttp^6.0 而 therobfonz/laravel-mandrill-driver 需要 guzzlehttp^7.0:有啥解决方案吗?