PHP curl POST headers returns null on Laravel middleware with HTTPS

Viewed 36

with http domain, it works, returning the expected output but with https, headers are null. Below is my attempt.

POST CURL

$fields = [];
$headers = ['_token: 58SkLjsNMk0', 'Authorization: 6059DLaf39d4141' ];

$cURLConnection = curl_init('<https domain>/api/auth/check');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $fields);
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER,$headers);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$res = curl_exec($cURLConnection);
curl_close($cURLConnection);

dd($res);

Middleware

<?php

namespace App\Http\Middleware;

use Closure;

class Api
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next){
        return response()->json($request->header('Authorization'));
    }
}

Any help, suggestions is greatly appreciated. Thanks in advance.

1 Answers

Maybe the HTTPS version has some 30x redirect in it? Try adding curl_setopt($cURLConnection, CURLOPT_FOLLOWLOCATION, true); to make cURL follow the redirection.

Also, just for debugging purposes(!), please try to add -

curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYPEER, false); 

These well disable the verification of the certificate's name against host / the peer's SSL certificate. (Add them all before curl_exec())

Related