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
MySQLi Query Returns Only One Row
PHP Json_Decode() Returns Null With Valid Json
How to Validate Array in Laravel
How to Call a MySQL Stored Procedure from Within PHP Code
Woocommerce: Which Hook to Replace Deprecated "Woocommerce_Add_Order_Item_Meta"
Paypal Access - Ssl Certificate: Unable to Get Local Issuer Certificate
Accessing Dates in PHP Beyond 2038
Count How Many Files in Directory PHP
PHP List All Files in Directory
PHP Array to Json Array Using Json_Encode();
How to Iterate Over the Results in a MySQLi Result Set
Does File_Get_Contents() Have a Timeout Setting
Downloading Large Files Reliably in PHP
Implementing Condition in Xpath