curl for authentication php

Solutions on MaxInterview for curl for authentication php by the best coders in the world

showing results for - "curl for authentication php"
Gianluca
20 May 2020
1<?php 
2define('OAUTH_TOKEN_URL','https://api.example.com/oauth/token'); // replace with according to documentation
3$url ='' ; // your redirect url
4$client_id = ''; // your client id from api
5$client_secret = ''; /your client secret from api
6// if api require client id and client secret in paramters then below other wise
7$data = "grant_type=authorization_code&code=".$_GET['code']."&client_id=".$client_id."&client_secret=".$client_secret."&redirect_uri=".$url;
8
9$headers = array(
10        'Content-Type:application/x-www-form-urlencoded',
11);
12
13// if api requires base64 and into -H header than 
14
15//$data = "grant_type=authorization_code&code=".$_GET['code']."&redirect_uri=".$url;
16//$base64 = base64_encode($client_id.":".$client_secret);
17
18//$headers = array(
19 //   'Content-Type:application/x-www-form-urlencoded',
20  //  'Content-Type:application/json',
21   // 'Authorization: Basic '. $base64
22// );
23
24$response = curlPost($data,$headers);
25print_r($response);
26
27function curlPost($data,$headers){
28    
29    $ch = curl_init();
30    curl_setopt($ch, CURLOPT_URL,OAUTH_TOKEN_URL); 
31    curl_setopt($ch, CURLOPT_POST, 1);
32    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);  //Post Fields
33    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
34    
35    
36    
37    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
38    
39    $server_output = curl_exec ($ch);
40    
41    return $server_output;
42    curl_close ($ch);
43    
44}