PHP CURL extended Basic Access Authentication Header

25 Views Asked by At

how can I combine ApiKey with Basic Authentication? The API requires to pass ApiKeyby extending the Basic Auth Headers. Actualy pass 2 more headers in basic auth (apiVersion & apiKey)

The manual has the following example (cURL command line)

curl --basic \
    -u "apiVersion=1;apiKey=myapikey;usename:password" \
    -H 'Content-Type: application/json' \
    -i http://someapi.com/rest/getdata?id=123

How can I do that with PHP cURL?

Thanks

1

There are 1 best solutions below

0
Vineet1982 On BEST ANSWER

Kindly read the documentation:

//for starting curl
$ch = curl_init();

//url to send request
curl_setopt($ch, CURLOPT_URL, 'http://someapi.com/rest/getdata?id=123');

//return results
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//post/get method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

//for -u authorisation
curl_setopt($ch, CURLOPT_USERPWD, 'apiVersion=1;apiKey=myapikey;usename' . ':' . 'password');

//for -H headers
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//execution and results    
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);