Al *_*min 47 php curl authorization
我有基本授权的PHP卷曲请求有问题.
这是命令行curl:
curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"
Run Code Online (Sandbox Code Playgroud)
我试过通过以下方式设置curl标题,但它不起作用
Authorization: Basic id:api_key
or
Authorization: Basic {id}:{api_key}
Run Code Online (Sandbox Code Playgroud)
我得到响应"请求中的身份验证参数丢失或无效"但我使用了正确的id和api_key,它在命令行curl中工作(我测试过)
请帮我.
Suh*_*man 127
请尝试以下代码:
$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);
Run Code Online (Sandbox Code Playgroud)
你能试试吗
$ch = curl_init($url);
...
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
...
Run Code Online (Sandbox Code Playgroud)
参考:http://php.net/manual/en/function.curl-setopt.php
小智 7
$headers = array(
'Authorization: Basic '. base64_encode($username.':'.$password),
);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Run Code Online (Sandbox Code Playgroud)