What are the considerations for error handling and fallback options when using cURL for extracting HTML code in PHP, especially on servers where cURL may not be available?

When using cURL for extracting HTML code in PHP, it's important to consider error handling and fallback options, especially on servers where cURL may not be available. One approach is to check if cURL is enabled on the server and if not, use alternative methods such as file_get_contents or other libraries to fetch the HTML content.

// Check if cURL is available on the server
if (function_exists('curl_init')) {
    // cURL is available, use it to fetch HTML content
    $url = 'https://example.com';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $html = curl_exec($ch);
    
    if ($html === false) {
        // Handle cURL error
        echo 'cURL error: ' . curl_error($ch);
    }
    
    curl_close($ch);
} else {
    // cURL is not available, use alternative method like file_get_contents
    $html = file_get_contents($url);
    
    if ($html === false) {
        // Handle error when using file_get_contents
        echo 'Error fetching HTML content';
    }
}

// Use $html variable containing the fetched HTML content
echo $html;