2
此卷曲示例适用。散列是$ username的base64编码。 ':'。 $密码。适当的卷曲方式授权:基本散列
curl -H "Authorization: Basic b2ZmZXJib3NzqGdtYxlsLmNvbupHcmVtbdFuJA==" https://somedomain.com/login
下面的PHP代码无法正常工作,并返回 “状态”: “未授权”, “的typeName”: “badCredentials”, “TYPECODE”:401,0]
$hash = base64_encode($username . ":" . $password);
echo '<p>' . $hash . '</p>'; //hash works correct
$URL='https://somedomain.com/login';
$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_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $hash);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec ($ch);
curl_close ($ch);
虽然它可能不是“正确”的方式,但通过基本身份验证,您通常可以在URL中指定用户名/密码,如“http:// username:password @ www.whatever.com/path / – 2014-10-09 18:08:26