How can a PHP script effectively loop through multiple URLs and send variable values to each one in a controlled manner?
To loop through multiple URLs and send variable values to each one in a controlled manner, you can use a combination of an array of URLs and a loop to iterate through them. Within the loop, you can use cURL or file_get_contents to send the variable values to each URL and retrieve the response.
<?php
// Array of URLs to loop through
$urls = array(
'http://example.com/page1.php',
'http://example.com/page2.php',
'http://example.com/page3.php'
);
// Variable values to send
$variable1 = 'value1';
$variable2 = 'value2';
foreach ($urls as $url) {
$data = array(
'variable1' => $variable1,
'variable2' => $variable2
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
// Process the response as needed
echo $response;
}
?>