How to Use Basic Authorization in PHP Curl

How do I make a request using HTTP basic authentication with PHP curl?

You want this:

curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  

Zend has a REST client and zend_http_client and I'm sure PEAR has some sort of wrapper.
But its easy enough to do on your own.

So the entire request might look something like this:

$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
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);

How to use basic authorization in PHP curl

Try the following code :

$username='ABC';
$password='XYZ';
$URL='<URL>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);

PHP cURL - HTTP GET with basic authentication 403

As per the documentation at the link you provided, the URL is supposed to be in the form

https://api.seneye.com/v1/devices/3442?user=***&pwd=***

where the username and password are sent as querystring parameters, rather than in an authorisation header.

So I think your PHP code should look like this:

<?php

$username = 'addusername'; //Example entry
$password = 'addpassword'; //Example entry
$URL = 'https://api.seneye.com/v1/devices/3442?IncludeState=1&user='.$username.'&pwd='.$password; //Actual ID code different

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$result=curl_exec($ch);

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

echo "HTTP CODE:: " . $status_code;

curl_close ($ch);

$json = json_decode($result, true);

echo "<br><br>";
print_r($json);

?>

Note that technically this is not Basic Authentication as officially defined, it's a custom scheme. It's also not ideal from a security point of view because browser and servers often log the URLs which are visited, which means that the credentials end up being stored, often in plain-text, in many different places.

How to use basic auth in cURL Laravel using guzzle

by using the code below you can build the Needed cURL

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;

class PayController extends Controller

public function order(Request $request)

{

$key = "YOUR API KEY";
$secret = "YOUR API SECRET";
$receipt = "unique recipt no"
$amount = "1000"
$currency = "INR"

$client = new Client();
$response = Http::withBasicAuth($key,$secret)
->post('https://api.razorpay.com/v1/orders',
[
'receipt'=> $receipt,
'amount'=> $amount,
'currency'=> $currency
]
);


Related Topics



Leave a reply



Submit