PHP Curl Request With Bearer Token Authorization Header Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- PHP Curl Delete Request Example Code
- PHP Curl POST Request with Headers Example
- PHP Curl Get Request with Parameters Example
- PHP Curl Request with Certificate (cert pem file option) Example
- How to Generate 4,6,8,10 Digit Random number in PHP?
- PHP Get All Array Keys Starting with Certain String Example
- PHP Convert XML to JSON Example
- Codeigniter Curl Post Request with Parameters Example
- PHP CURL Post Request with Parameters Example
- Laravel CURL Request Example using Ixudra/curl
- PHP Download File from URL using CURL Request Example