Search code examples
laravelphp-curllaravel-api

Restful API with Laravel


I'm developing a restful API with laravel. Authorization is done using a SECRET_KEY & PUBLIC_KEY as shown in the sample request code below.

How do i retrieve the Authorization header key value from the Request?

I would also appreciate links to any resources that would guide me through better implementation & best practice in creating a Restful API with Laravel.


 $curl = curl_init();
           $url = "http://api.example.com/v1/users";

           $secretKey = env('SECRET_KEY');
           $publicKey = env('PUBLIC_KEY');

           $firstName = "John";
           $lastName = "Doe";
           $email = "[email protected]";


            curl_setopt_array($curl, array(
               CURLOPT_URL => $url,
               CURLOPT_RETURNTRANSFER => true,
               CURLOPT_ENCODING => "",
               CURLOPT_MAXREDIRS => 10,
               CURLOPT_TIMEOUT => 0,
               CURLOPT_FOLLOWLOCATION => true,
               CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
               CURLOPT_CUSTOMREQUEST => "POST",
               CURLOPT_POSTFIELDS => "{\r\n  \"firstName\": \"$firstName\",\r\n  \"lastName\": \"$lastName\",\r\n  \"email\": \"$email\",\r\n  \"secretKey\": \"$secretKey\",\r\n}",
               CURLOPT_HTTPHEADER => array(
                   "Content-Type: application/json",
                   'Authorization: Bearer ' . $publicKey,
               ),
           ));

           $response = curl_exec($curl);

           curl_close($curl);

Solution

  • The request() helper contains everything from the request. Just dd(request()->header()) to see the full list of the request headers.