yuri1000
yuri1000

Reputation: 361

Executing PHP CURL in While Loop

I have a table where I want to run a PHP script during the maintenance.

I have the following code.

$sql = "SELECT `id`, `url` FROM `log_requests` WHERE `is_sent` = '0'";
$e = $conn->execute($sql);

My url array after fetching the table from DB.

Array
(
[0] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+1
[1] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+2
[2] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+3
[3] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+4
)

Then I run while loop

$ch = curl_init();
while($row = $e->FetchRow() ){
    curl_setopt($ch, CURLOPT_URL, $row['url']);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $q = "UPDATE `log_requests` SET `is_sent` = '1' WHERE `id` = '".$row['id']."'";
    $conn->execute($q);
}
curl_close($ch);

Issue is only one URL is executed using CURL and in my table only one row gets updated as 1 in is_sent column.

Why is it running single url in my while loop. What mistakes am I making here?

************************* EDIT ********************

Okay, now I came out with following and running successfully now. I just want to know whether am doing right or not.

$set = array();
while ($row = $executequery2->FetchRow()) {
    $set[$row['url']][] = $row;
}

$curl_handles = array();
foreach($set as $url => $rows) {
    $curl_handles[$url] = curl_init();
    curl_setopt($curl_handles[$url], CURLOPT_URL, $url);
    foreach($rows as $row) {
        $query3 = "UPDATE `log_requests` SET `is_sent` = '1' WHERE `id` = '" . $row['id'] . "'";
        $conn->Execute($query3);
    }
}

$curl_multi_handle = curl_multi_init();
$i = 0;
$block = array();

foreach($curl_handles as $a_curl_handle) {
    $i++;
    curl_multi_add_handle($curl_multi_handle, $a_curl_handle);
    $block[] = $a_curl_handle;
    if (($i % 5 == 0) or ($i == count($curl_handles))) {
        $running = NULL;
        do {
            $running_before = $running;
            curl_multi_exec($curl_multi_handle, $running);
            if ($running != $running_before) {
                echo ("Waiting for $running sites to finish...<br />");
            }
        }
        while ($running > 0);
        foreach($block as $handle) {
            $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
            $curl_errno = curl_errno($handle);
            $curl_error = curl_error($handle);
            if ($curl_error) {
                echo ("    *** cURL error: ($curl_errno) $curl_error\n");
            }
            curl_multi_remove_handle($curl_multi_handle, $handle);
        }
        $block = array();
    }
}

curl_multi_close($curl_multi_handle);

Updating the table column is the right way?

Upvotes: 3

Views: 5219

Answers (1)

Saili Jaguste
Saili Jaguste

Reputation: 146

I had faced same issue and got this solution online. Just separate curl code from the loop and then try.

Something like this:

while($row = $e->FetchRow()) {
    $result = call_curl($row['url']);
    if($result === FALSE) {
        die("Error");
    }
    else
    {
        $q = "UPDATE `log_requests` SET `is_sent` = '1' WHERE `id` = '".$row['id']."'";
    }   
}


function call_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec ($ch);
    curl_close($ch);
    return $result;
}

Hope this helps you as well.

Upvotes: 2

Related Questions