PHP Curl Request With Bearer Token Authorization Header Example

By Hardik Savani May 14, 2024 Category : PHP

Hi Dev,

This simple article demonstrates of php curl request with bearer token. i explained simply about curl post request with bearer token php. this example will help you rest api token based authentication example php. This tutorial will give you simple example of php curl with authorization header. Alright, let’s dive into the steps.

In this example, we will use CURLOPT_HTTPHEADER to pass authorization: bearer token for authorization. so let's see bellow simple example code here:



/* API URL */

$url = '';

/* Init cURL resource */

$ch = curl_init($url);

/* Array Parameter Data */

$data = ['name'=>'Hardik', 'email'=>''];

/* pass encoded JSON string to the POST fields */

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

/* set the content type json */

$headers = [];

$headers[] = 'Content-Type:application/json';

$token = "your_token";

$headers[] = "Authorization: Bearer ".$token;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

/* set return type json */

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* execute request */

$result = curl_exec($ch);

/* close cURL resource */



i hope it can help you...

Tags :