How to Make a Request Using Http Basic Authentication With 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 define the basic HTTP authentication using cURL correctly?

curl -u username:password http://
curl -u username http://

From the documentation page:

-u, --user <user:password>

Specify the user name and password to use for server authentication.
Overrides -n, --netrc and --netrc-optional.

If you simply specify the user name, curl will prompt for a password.

The user name and passwords are split up on the first colon, which
makes it impossible to use a colon in the user name with this option.
The password can, still.

When using Kerberos V5 with a Windows based server you should include
the Windows domain name in the user name, in order for the server to
succesfully obtain a Kerberos Ticket. If you don't then the initial
authentication handshake may fail.

When using NTLM, the user name can be specified simply as the user
name, without the domain, if there is a single domain and forest in
your setup for example.

To specify the domain name use either Down-Level Logon Name or UPN
(User Principal Name) formats. For example, EXAMPLE\user and
user@example.com respectively.

If you use a Windows SSPI-enabled curl binary and perform Kerberos V5,
Negotiate, NTLM or Digest authentication then you can tell curl to
select the user name and password from your environment by specifying
a single colon with this option: "-u :".

If this option is used several times, the last one will be used.

http://curl.haxx.se/docs/manpage.html#-u

Note that you do not need --basic flag as it is the default.

Making a HTTP GET request with HTTP-Basic authentication

Using file_get_contents() with a stream to specify the HTTP credentials, or use curl and the CURLOPT_USERPWD option.



Related Topics



Leave a reply



Submit