61

I am having problem with PHP curl request with basic authorization.

Here is the command line curl:

curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"

I have tried by setting curl header in following ways but it's not working

Authorization: Basic id:api_key
or 
Authorization: Basic {id}:{api_key}

I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)

Please help me.

SolarBear
  • 4,534
  • 4
  • 37
  • 53
Al Amin
  • 631
  • 1
  • 5
  • 9

4 Answers4

165

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);
Thew
  • 15,789
  • 18
  • 59
  • 100
Suhel Meman
  • 3,702
  • 1
  • 18
  • 26
  • Nice one! I had `curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');` but that wasn't working. Used your `CURLAUTH_ANY` and we're golden! – Joshua Pinter Apr 17 '14 at 15:35
  • 4
    I also was able to remove the `CURLOPT_HTTPAUTH` line and have it work properly. – Joshua Pinter Apr 17 '14 at 15:36
  • 7
    @JoshPinter, you were using the string `'CURLAUTH_BASIC'` instead of the constant `CURLAUTH_BASIC` – Walter Tross May 07 '14 at 13:17
  • 2
    This worked for me `curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Basic ".base64_encode($this->username.":".$this->password), ]);` – FosAvance May 03 '16 at 13:20
  • do i still need to secure my php file when i use 'CURLOPT_USERPWD` so hackers won't know the username and password? – Zame Aug 16 '16 at 06:26
  • 2
    Shouldn't curl_getinfo() come after curl_exec()? – Alien Technology Aug 27 '16 at 17:24
  • It works for me except when I click on a link on the result page. How can I make another call to this script on clicking a link on the result page? – Sadık May 22 '17 at 21:49
9

Can you try this,

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

REF: http://php.net/manual/en/function.curl-setopt.php

Krish R
  • 22,583
  • 7
  • 50
  • 59
7
$headers = array(
    'Authorization: Basic '. base64_encode($username.':'.$password),
);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Danoosh
  • 169
  • 2
  • 7
  • 4
    When answering a six year old question with three existing answers it is important to explain what new aspect of the question your answer addresses. Code only answers can almost always be improved by adding explanation. – Jason Aller May 12 '20 at 23:53
  • @JasonAller I'd tested all of them but wasn't working. There is a comment like my answer but I think this solution must be an answer. – Danoosh May 14 '20 at 02:15
  • @Danoosh your code is redundant. You manually construct the AUTH header specifying you want Basic and base64-encoding the user/pwd + instruct cURL to itself create an AUTH header using Basic auth, but didn't provide cURL any user/pwd to use, so it can't do it. Choose only 1 method - the 2nd one - why manully implement the encoding when cURL can do it for you. – Marki555 Feb 10 '23 at 10:07
1

Its Simple Way To Pass Header

function get_data($url) {

$ch = curl_init();
$timeout = 5;
$username = 'c4f727b9646045e58508b20ac08229e6';        // Put Username 
$password = '';                                        // Put Password
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");    // Add This Line
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "https://storage.scrapinghub.com/items/397187/2/127";
$data = get_data($url);
echo '<pre>';`print_r($data_json);`die;    // For Print Value

Check My JSON Value

Paresh Shiyal
  • 534
  • 4
  • 15