Answers for "curl for authentication php"

PHP
0

curl for authentication php

<?php 
define('OAUTH_TOKEN_URL','https://api.example.com/oauth/token'); // replace with according to documentation
$url ='' ; // your redirect url
$client_id = ''; // your client id from api
$client_secret = ''; /your client secret from api
// if api require client id and client secret in paramters then below other wise
$data = "grant_type=authorization_code&code=".$_GET['code']."&client_id=".$client_id."&client_secret=".$client_secret."&redirect_uri=".$url;

$headers = array(
        'Content-Type:application/x-www-form-urlencoded',
);

// if api requires base64 and into -H header than 

//$data = "grant_type=authorization_code&code=".$_GET['code']."&redirect_uri=".$url;
//$base64 = base64_encode($client_id.":".$client_secret);

//$headers = array(
 //   'Content-Type:application/x-www-form-urlencoded',
  //  'Content-Type:application/json',
   // 'Authorization: Basic '. $base64
// );

$response = curlPost($data,$headers);
print_r($response);

function curlPost($data,$headers){
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,OAUTH_TOKEN_URL); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $server_output = curl_exec ($ch);
    
    return $server_output;
    curl_close ($ch);
    
}
Posted by: Guest on August-24-2021

Browse Popular Code Answers by Language