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