Are there any best practices for querying search engines like Google using curl in PHP?

When querying search engines like Google using curl in PHP, it is important to follow best practices to ensure successful requests. One key practice is to set a user-agent header in the curl request to mimic a real browser and avoid being blocked by the search engine. Additionally, it's recommended to handle any potential errors or exceptions that may occur during the request.

<?php

$query = "example query";
$url = "https://www.google.com/search?q=" . urlencode($query);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
$response = curl_exec($ch);

if($response === false){
    echo 'Curl error: ' . curl_error($ch);
} else {
    echo $response;
}

curl_close($ch);

?>