ItSolutionStuff.com

PHP Curl Request With Bearer Token Authorization Header Example

By Hardik Savani β€’ May 14, 2024
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:

Example:

<?php

/* API URL */

$url = 'http://www.mysite.com/api';

/* Init cURL resource */

$ch = curl_init($url);

/* Array Parameter Data */

$data = ['name'=>'Hardik', 'email'=>'itsolutionstuff@gmail.com'];

/* 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 */

curl_close($ch);

?>

i hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

PHP Curl Delete Request Example Code

Read Now β†’
β˜…

PHP Curl POST Request with Headers Example

Read Now β†’
β˜…

PHP Curl Get Request with Parameters Example

Read Now β†’
β˜…

PHP Curl Request with Certificate (cert pem file option) Example

Read Now β†’
β˜…

How to Generate 4,6,8,10 Digit Random number in PHP?

Read Now β†’
β˜…

PHP Get All Array Keys Starting with Certain String Example

Read Now β†’
β˜…

PHP Convert XML to JSON Example

Read Now β†’
β˜…

Codeigniter Curl Post Request with Parameters Example

Read Now β†’
β˜…

PHP CURL Post Request with Parameters Example

Read Now β†’
β˜…

Laravel CURL Request Example using Ixudra/curl

Read Now β†’
β˜…

PHP Download File from URL using CURL Request Example

Read Now β†’