How to use basic authorization in PHP curl

0 votes

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)

Nov 8, 2020 in PHP by kartik
• 37,520 points
35,892 views

2 answers to this question.

0 votes

Hello @kartik,

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);
answered Nov 8, 2020 by Niroj
• 82,880 points
0 votes

If you are saying Basic authentication, then the syntax is like below. In this code, I have created a basic authentication header and pass it to the curl request.

$host = 'https://www.discussdesk.com/';
$user_name = 'set_user_name';
$password = 'set_password';
$ch = curl_init($host);
$headers = array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode("$user_name:$password")
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if(curl_errno($ch)){
// throw the an Exception.
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
answered Jan 3, 2021 by Manas
• 140 points

Related Questions In PHP

0 votes
2 answers

How to catch curl errors in PHP?

For (PHP 4 >= 4.0.3, PHP 5, ...READ MORE

answered Nov 9, 2020 in PHP by anonymous
• 160 points
19,497 views
0 votes
1 answer

How to get response using cURL in PHP?

Hello @kartik, Use the below piece of code ...READ MORE

answered Oct 19, 2020 in PHP by Niroj
• 82,880 points
6,970 views
0 votes
0 answers

How to efficiently use try...catch blocks in PHP

I was using try..catch blocks in my ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,050 points
566 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,880 points
2,958 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,938 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,960 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,894 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,358 views
0 votes
1 answer

How can I connect to a Tor hidden service using CURL in PHP?

Hello @kartik, I use Privoxy and cURL to scrape Tor ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,880 points
5,287 views
0 votes
1 answer

How to enable cURL in PHP / XAMPP?

Hello @kartik, Since you're using XAMPP, uncomment the ...READ MORE

answered Oct 1, 2020 in PHP by Niroj
• 82,880 points
4,602 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP