How do I make a request using HTTP basic authentication with PHP curl?
Solution 1
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);
Solution 2
CURLOPT_USERPWD
basically sends the base64 of the user:password
string with http header like below:
Authorization: Basic dXNlcjpwYXNzd29yZA==
So apart from the CURLOPT_USERPWD
you can also use the HTTP-Request
header option as well like below with other headers:
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("user:password") // <---
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Solution 3
The most simple and native way it's to use CURL directly.
This works for me :
<?php
$login = 'login';
$password = 'password';
$url = 'http://your.url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
echo($result);
Solution 4
You just need to specify CURLOPT_HTTPAUTH and CURLOPT_USERPWD options:
$curlHandler = curl_init();
$userName = 'postman';
$password = 'password';
curl_setopt_array($curlHandler, [
CURLOPT_URL => 'https://postman-echo.com/basic-auth',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $userName . ':' . $password,
]);
$response = curl_exec($curlHandler);
curl_close($curlHandler);
Or specify header:
$curlSecondHandler = curl_init();
curl_setopt_array($curlSecondHandler, [
CURLOPT_URL => 'https://postman-echo.com/basic-auth',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Basic ' . base64_encode($userName . ':' . $password)
],
]);
$response = curl_exec($curlSecondHandler);
curl_close($curlSecondHandler);
Guzzle example:
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$userName = 'postman';
$password = 'password';
$httpClient = new Client();
$response = $httpClient->get(
'https://postman-echo.com/basic-auth',
[
RequestOptions::AUTH => [$userName, $password]
]
);
print_r($response->getBody()->getContents());
See https://github.com/andriichuk/php-curl-cookbook#basic-auth
Solution 5
Other way with Basic method is:
$curl = curl_init();
$public_key = "public_key";
$private_key = "private_key";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://url.endpoint.co/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Basic ".base64_encode($public_key.":".$private_key)
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Related videos on Youtube
![blank](https://i.stack.imgur.com/t6UY6.png?s=256&g=1)
Comments
-
blank almost 4 years
I'm building a REST web service client in PHP and at the moment I'm using curl to make requests to the service.
How do I use curl to make authenticated (http basic) requests? Do I have to add the headers myself?
-
blank over 14 yearsBy REST client I mean something that abstracts away some of the low level details of using curl for http get, post, put, delete etc. which is what I'm doing by building my own php class to do this; I'm wondering if someone has already done this.
-
nategood over 14 yearsYes, then HTTP_Request_2 may be of interest to you. It abstracts away a lot of the ugliest of cUrl in PHP. To set the method you use, setMethod(HTTP_Request2::METHOD_*). With PUT and POSTs, to set the body of the request you just setBody(<<your xml,json,etc. representation here>>). Authentication described above. It also has abstractions for the HTTP Response (something that cUrl really lacks).
-
aalaap about 8 yearsThis method of passing a custom auth header instead of using
CURLOPT_USERPWD
worked for me. -
Kit Ramos over 7 yearsThis worked better then setting the user and password separately
-
Amrish Kakadiya over 3 yearsYes this is right for sending basic authentication header with username and password.