PHP CURL 扩展基本访问身份验证标头

问题描述 投票:0回答:1

如何将 ApiKey 与基本身份验证结合起来? API 需要通过扩展基本身份验证标头来传递 ApiKey。 实际上在基本身份验证中传递了另外 2 个标头(apiVersion 和 apiKey)

手册中有以下示例(cURL 命令行)

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

如何使用 PHP cURL 做到这一点?

谢谢

php curl basic-authentication extend
1个回答
0
投票

请阅读文档:

//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);
© www.soinside.com 2019 - 2024. All rights reserved.