How can the cURL library be effectively used in PHP for accessing and interacting with external websites?
To effectively use the cURL library in PHP for accessing and interacting with external websites, you can send HTTP requests, handle cookies, and customize headers. This can be useful for tasks like web scraping, interacting with APIs, or automating website interactions.
<?php
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL session
$response = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Output the response
echo $response;