What steps can be taken to ensure that a PHP script accurately retrieves and stores all search results from multiple pages in a CSV file?

To ensure that a PHP script accurately retrieves and stores all search results from multiple pages in a CSV file, you can implement a loop that iterates through each page of search results, extracts the relevant data, and appends it to the CSV file. Additionally, you can use libraries like cURL to handle HTTP requests and DOMDocument to parse HTML content efficiently.

<?php
// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'https://example.com/search?page=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Parse HTML content
$dom = new DOMDocument();
$dom->loadHTML($response);

// Extract search results and store in CSV file
$csvFile = fopen('search_results.csv', 'w');
foreach ($dom->getElementsByTagName('div') as $div) {
    // Extract data and write to CSV file
    fputcsv($csvFile, array($div->textContent));
}

// Loop through additional pages if needed
for ($page = 2; $page <= 5; $page++) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://example.com/search?page=' . $page);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $dom->loadHTML($response);
    foreach ($dom->getElementsByTagName('div') as $div) {
        fputcsv($csvFile, array($div->textContent));
    }
}

// Close CSV file
fclose($csvFile);
?>