调用 haveibeenpwned 时出现意外的 401 响应 API

Unexpected 401 response when calling haveibeenpwned API

我一直卡在 401 响应中,另一位用户建议我的 header 没有在 API 键字段中包含前导白色 space。我更正了它,但仍然收到 401。API 密钥应该可以用,我昨晚付了钱,并多次重新生成一个新密钥

我的代码:

<?php

                $service_url = 'https://haveibeenpwned.com/api/v3/breachedaccount/?account=emailtest123@gmail.com';
                $curl = curl_init($service_url);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);


                $ch = curl_init ($service_url);
                curl_setopt ($ch, CURLOPT_HTTPGET, true);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                  'hibp-api-key: 8bcxxxxxxxxxxxxxx1bc11',
                  'user-agent:  FAUstudentproject'
                  ));
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $returndata = curl_exec ($ch);
                $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); 
                print_r($status_code);
                print_r($returndata);

                $curl_response = curl_exec($curl);
                if ($curl_response === false) {
                    $info = curl_getinfo($curl);
                    $json = curl_close($curl);

                    die('error occured during curl exec. Additioanl info: ' . var_export($info));
                }

                curl_close($curl);
                $decoded1 = json_decode($curl_response,true);

                if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') {

                  echo "hello";
                    echo curl_getinfo($ch);

                    die('error occured: ' . $decoded1->response->errormessage);
                }
                echo 'response ok!';

                print_r(json_decode($json));

                var_export($decoded1->response);

            ?>

回复:

401{ "statusCode": 401, "message": "Access denied due to missing hibp-api-key." }array ( 'url' => 'https://haveibeenpwned.com/api/v3/breachedaccount/?account=emailtest123@gmail.com', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 20, 'redirect_count' => 0, 'total_time' => 0.04700000000000000011102230246251565404236316680908203125, 'namelookup_time' => 0.01600000000000000033306690738754696212708950042724609375, 'connect_time' => 0.0309999999999999997779553950749686919152736663818359375, 'pretransfer_time' => 0.0, 'size_upload' => 0.0, 'size_download' => 0.0, 'speed_download' => 0.0, 'speed_upload' => 0.0, 'download_content_length' => -1.0, 'upload_content_length' => -1.0, 'starttransfer_time' => 0.0, 'redirect_time' => 0.0, 'redirect_url' => '', 'primary_ip' => '2606:4700::6812:ad0d', 'certinfo' => array ( ), 'primary_port' => 443, 'local_ip' => '2601xxxxxxxxxxxxxxxxx21d3', 'local_port' => 59096, 'http_version' => 0, 'protocol' => 2, 'ssl_verifyresult' => 0, 'scheme' => 'HTTPS', )error occured during curl exec. Additioanl info:

您使用了错误的变量:将 curl_setopt($curl, CURLOPT_HTTPHEADER 替换为 curl_setopt($ch, CURLOPT_HTTPHEADER。您在代码中使用了两次 curl_init

检查您的代码以修复它,仅使用 $ch$curl,而不是同时使用两者。