Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afabris's avatar

How to appraoch API call from a external site - Issue with Invalid credentials. "Invalid credentials."

Hello

I have an issue that I cannot resolve, so I hope community might share some light. I have Laravel App, and it has APIs. Mostly they work fine, except when I get external request and that does not work - I get Invalid credentials response

So my kernel.php has this

protected $middlewareGroups = [
         'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\Language::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

        'api' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class
        ],
    ];

my RouteServiceProvide.php

protected function mapWebRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('Http/routes.php');
        });
    }
    
    protected function mapApiRoutes(Router $router){
        $router->group([
            'middleware' => 'api', 'namespace' => $this->namespace.'\API', 'prefix' => 'api'
        ], function ($router) {
            $router->group([
                'prefix' => 'v1'
            ], function(){
                require app_path('Http/api_routes.php');
            });
        });
    }

my api_routes.php

Route::get('init/{email}', 'BackAPIs@init');

And BackAPIs controller has

public function __construct() {
        $this->middleware('auth.basic');
    }

and

 function init(Request $request, $email) {

        try {
            $decrypted = decrypt($email);

.......

My external application is using the code straight from Postman (which seems to work fine and I get required responses)

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://appsforce.front/api/v1/init/eyJpdiI6ImN5Y2hteTJtbGFXUjZoMkJNU3I4RHc9PSIsInZhbHVlIjoiZDJEVWRwVHJFazE2SldkSzIwMGVha1V0aEdQVDJQWjdjb2RLZ1RcL1VHQU09IiwibWFjIjoiZjM2NzA3NzIzZjc5NjkyNmRlZTgwMzAyZmEyMDQ1OTE0YTYwYTlhNGJmN2MwNzc3YTI5OGQ4NDZhNjEzYWU5YyJ9",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic YWZAYXBwc2ZvcmNlLmNvLnVrOmFwcHNmb3JjZQ==",
    "cache-control: no-cache",
    "postman-token: 6ee8747a-0d3a-3660-09d9-c0449f1bc877"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
dd($response);

My problem is that I always get Invalid credentials. "Invalid credentials."

I tried many things, but it just does not want to play game. Any ideas are welcome.

Many thanks in advance.

0 likes
1 reply
afabris's avatar
afabris
OP
Best Answer
Level 1

I have this resolved. Due to multiple bugs, it did not work. One of the things were issues with .htaccess files, etc. Ended up using token authentication, instead of basic.auth as well

Please or to participate in this conversation.