How can you optimize the code provided to make it more efficient for posting to multiple directories?

The issue with the current code is that it makes separate requests to post the same data to multiple directories, which can be inefficient. To optimize the code, you can create an array of directory URLs and then loop through them to post the data to each directory in a single request.

<?php

// Array of directory URLs
$directoryUrls = [
    'http://directory1.com/post',
    'http://directory2.com/post',
    'http://directory3.com/post'
];

// Data to post
$data = [
    'title' => 'Example Post',
    'content' => 'This is the content of the post.'
];

// Loop through directory URLs and post data
foreach ($directoryUrls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // Handle response as needed
    echo "Posted to $url: $response\n";
}

?>