How to send two request with curl using the same headears and params, only the url change I dont want to duplicate the code two time
$url1 = "www.example1.com"
$url2 = "www.example2.com"
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $config->url1 . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
Thank you
CodePudding user response:
You can put everything into a function and call that:
function get_orders($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Call with
$url1 = get_orders('https://example1.com');
$url2 = get_orders('https://example2.com');
CodePudding user response:
Kind of basic:
$urls[] = "https://www.example1.com";
$urls[] = "https://www.example2.com";
foreach ($urls as $url){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
}
I see a function option was also answered! :) this one is with a loop.