首页 > 解决方案 > 调用 PHP curl GET 请求导致“curl 执行期间发生错误”

问题描述

我正在尝试连接到 haveibeenpwned 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);
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'hibp-api-key: 000000000000000000000',
                    'user-agent: FAUstudentproject'
                    ));

                $curl = curl_init($service_url);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                $curl_response = curl_exec($curl);
                if ($curl_response === false) {
                    $info = curl_getinfo($curl);
                    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') {
                    die('error occured: ' . $decoded1->response->errormessage);
                }
                echo 'response ok!';
                var_export($decoded1->response);

            ?>

在 MAMP 窗口上运行的输出如下(我省略了一堆以阻止上传限制:


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.0940000000000000002220446049250313080847263336181640625, 'namelookup_time' => 
0.061999999999999999555910790149937383830547332763671875, 'connect_time' => 
0.07799999999999999988897769753748434595763683319091796875, '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' => '[my ip]',
 'local_port' => 60511, 'http_version' => 0, 'protocol' => 2, 'ssl_verifyresult' 
=> 0, 'scheme' => 'HTTPS', )error occured during curl exec. Additioanl info:

但是,在我学校的灯服务器上运行相同的 php,我得到了响应。

response ok! Notice: Trying to get property of non-object in /home/[my username]/public_html/cyber/part5.php on line 156 NULL

标签: phpapimampphp-curl

解决方案


推荐阅读