Home > front end >  CURL request at No Internet in PHP
CURL request at No Internet in PHP

Time:09-09

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 ?

CodePudding user response:

Looking at the CURL Error List you will face CURLE_COULDNT_CONNECT (7).

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