What are the advantages of using cURL in PHP for fetching webpages over fsockopen?
cURL in PHP offers several advantages over fsockopen for fetching webpages, including built-in support for various protocols (HTTP, HTTPS, FTP, etc.), easier handling of cookies and headers, support for authentication, and the ability to follow redirects automatically. Using cURL can simplify the process of making HTTP requests and handling responses in PHP.
// Example code snippet using cURL in PHP to fetch a webpage
$url = 'https://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if($response === false){
echo 'Error: ' . curl_error($ch);
} else {
echo $response;
}
curl_close($ch);