Answers for "php curl basic auth"

PHP
0

php curl basic auth

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$host = 'http://google.com';
$message = 'Test message';
$phone = '998999999';
$username = 'username';
$password = 'password';
$post = [
    "messages" => [
        [
            "recipient" => $phone,
            "message-id" => "itrust" . strval(time()),

            "sms" => [
                "originator" => "3700",
                "content" => [
                    "text" => $message
                ]
            ]
        ]
    ]
];
$payloadName = json_encode($post);

$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);

echo '<pre>';
var_dump($return);
echo '</pre>';
Posted by: Guest on July-30-2021
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