我正在使用Haveibeenpwned API,但是遇到连续403错误的麻烦。我尝试使用Curl和file_get_contents,但结果相同...
function Conn_mail($email){
$headers = [
'User-Agent: Meu user agent'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://haveibeenpwned.com/api/v2/breachedaccount/{$email}");
curl_setopt($ch, CURLOPT_HEADER,true); //Retorna o Header na saída
//if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//}
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$response = curl_exec($ch);
return $response;
/*var_dump($response);
if (!$response){
echo curl_error($ch);
}*/
}
$json_hibp = Conn_mail('whatever@email.com');
echo $json_hibp;
和
function Conn_mail($email){
$context_options = array(
'http' => array('user_agent' => 'custom user agent string')
);
$context = stream_context_create($context_options);
$con = file_get_contents("https://haveibeenpwned.com/api/v2/breachedaccount/{$email}",false,$context);
echo "<pre>";
print_r($http_response_header);
echo "</pre>";
$retorno = $http_response_header[0];
if($retorno == 'HTTP/1.1 200 OK'){
$ret = '200';
}
elseif($retorno == ' HTTP/1.1 404 Not Found'){
$ret = '404';
}
else{
$ret = $retorno;
}
return $ret;
}
$json_hibp = Conn_mail('whatever@email.com');
echo $json_hibp;
两个代码都包含“用户代理”标头,但是Haveibeenpwned API返回403错误(仅当缺少用户代理时才返回…)
怎么了?
谢谢
答案 0 :(得分:0)
经过几次测试,CURLOPT_SSL_VERIFYHOST = 0
(在Raymond解决方案中有注释)起作用。 file_get_contents
在使用Haveibeenpwned API时不能很好地处理,甚至添加了强制性的“ User-Agent”标头...