在PHP中没有Internet时的CURL请求

lndjwyie  于 2022-12-13  发布在  PHP
关注(0)|答案(1)|浏览(156)

I am working with CURL request in PHP. My code is like below.

// init curl object
        $ch = curl_init();

        // define options
        $option_array = array(
            CURLOPT_URL => $api_url,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HEADER => 0,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_VERBOSE => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_FAILONERROR => 1
        );
        
        // apply options
        curl_setopt_array($ch, $option_array);

        // execute request and get response
        $response = curl_exec($ch);
        
        if(curl_errno($ch)){
            echo 'error:' . curl_error($ch);
        } else {
            return $response;        
        }

        curl_close($ch);

How to know internet is not available at CURL request in PHP ?

kcrjzv8t

kcrjzv8t1#

看着CURL Error List,你将面对CURLE_COULDNT_CONNECT (7)

if (curl_errno($ch) === 7) {
    throw new Exception('No Internet.');
}

相关问题