By Al Amin

2013-11-19 05:58:35 8 Comments

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}"

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

Authorization: Basic id:api_key
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.


@Paresh Shiyal 2019-10-02 04:35:03

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);
return $data;
$url = "";
$data = get_data($url);
echo '<pre>';`print_r($data_json);`die;    // For Print Value

Check My JSON Value

@Suhel Meman 2013-11-19 06:05:31

Try the following code :


$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_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
curl_close ($ch);

@Joshua Pinter 2014-04-17 15:35:02

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 2014-04-17 15:36:13

I also was able to remove the CURLOPT_HTTPAUTH line and have it work properly.

@Walter Tross 2014-05-07 13:17:52

@JoshPinter, you were using the string 'CURLAUTH_BASIC' instead of the constant CURLAUTH_BASIC

@FosAvance 2016-05-03 13:20:14

This worked for me curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Basic ".base64_encode($this->username.":".$this->password), ]);

@Zame 2016-08-16 06:26:44

do i still need to secure my php file when i use 'CURLOPT_USERPWD` so hackers won't know the username and password?

@Alien Technology 2016-08-27 17:24:21

Shouldn't curl_getinfo() come after curl_exec()?

@Sadik 2017-05-22 21:49:20

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?

@Krish R 2013-11-19 06:01:42

Can you try this,

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


@Harish Prasanna 2015-05-13 16:29:53

This actually worked for me instead of curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

36 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

37 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2564642 View
  • 2455 Score
  • 37 Answer
  • Tags:   php arrays unset

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

32 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

10 Answered Questions

[SOLVED] How to send a header using a HTTP request through a curl call?

  • 2008-12-10 16:38:57
  • gagneet
  • 1619531 View
  • 1395 Score
  • 10 Answer
  • Tags:   curl http-headers

23 Answered Questions

[SOLVED] How do I POST JSON data with cURL?

4 Answered Questions

[SOLVED] How can I connect to a Tor hidden service using cURL in PHP?

  • 2013-03-16 03:39:25
  • frosty
  • 236687 View
  • 361 Score
  • 4 Answer
  • Tags:   php curl proxy tor

10 Answered Questions

Sponsored Content