I have a mysql database. The table contains the urls that I need to curl and columns that show how often and how many times these urls are called.
My table like that:
URL | TIMEOUTMS | FREQUENCY | HOW MANY TIME | ERROR COUNT OF |
---|---|---|---|---|
url1 | 2000 | 5 | 3 | 3 |
url2 | 3000 | 10 | 5 | 5 |
url3 | 2000 | 30 | 3 | 3 |
For example, I have to curl url1 3 times and wait 5 seconds in between. If all three are wrong I should create an alarm.
I wrote the multi curl function, but I got a little confused because of the values I specified in the table
My code is:
<?php
function control_paralel($url_array,$timeout_array){
$nodes = $url_array;
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++)
{
$url =$nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT_MS,
$timeout_array[$i]);
curl_setopt($curl_arr[$i], CURLOPT_TIMEOUT_MS, $timeout_array[$i]);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
} while($running > 0);
for($i = 0; $i < $node_count; $i++)
{
$results[] = curl_multi_getcontent ( $curl_arr[$i] );
$httpcode[]= curl_getinfo($curl_arr[$i], CURLINFO_HTTP_CODE);
}
return $httpcode;
}
$result_alerts = get_all_alerts();
$url_array = [];
$timeout_array = [];
while($row = $result_alerts->fetch_assoc()) {
array_push($url_array,$row['URL']);
array_push($timeout_array,$row['TIMEOUT']);
}
print_r(control_paralel($url_array,$timeout_array));
?>
I thought to group by according to the FREQUENCY and HOW_MANY_TIME columns and send each one to multi_curl in separate groups, but do you think this is the right way? Do you have any other suggestions?