0
votes

so I've created 2 CodeIgniter applications; one app is API and the other is the client app. I've deployed both of the apps in cPanel (before deployment I've tested both of the apps and both of them works fine). I've tested the API using Postman and it works just fine, but when I tried to access the API using my client app (using GuzzleHttp v6.5.5), I keep getting 403 Forbidden error.

Controller:

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'));
        }
    }

Model:

<?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); 
    }
?>

Error Message:

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

I've tried using User-Agent, Accept, and Accept-Encoding in the headers but I still get the 403 Forbidden error. What I've done wrong in the code? Thanks in advance.

Update: In response to @Ivan Ivanov's comment; after I changed 'body' to 'json', when I run it instead of 403 the system is now giving me 500 Internal Server Error. But then again, if I tried to login through postman it works just fine.

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

Also, it seems that $this->_client isn't null.

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

1 Answers

1
votes

You should change this line:

'body' => json_encode($params)

with this:

'json' => json_encode($params)

Since Guzzle expects 'json' key in the POST array.

And also var_dump( $this->_client ) to see if you have any client at all.