3

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?

yuri1000
  • 361
  • 4
  • 21
  • Are you using PDO? What does a dump of `$row` yield? – Andrei Apr 03 '17 at 08:44
  • Using ADODB. I have given the array for `url` $row yield – yuri1000 Apr 03 '17 at 08:46
  • you need to both init your curl and close it within your while loop, or instead use [multi_curl_exec](http://php.net/manual/en/function.curl-multi-exec.php) instead – hassan Apr 03 '17 at 08:56
  • @hassan I tried multi_curl_* too as per http://stackoverflow.com/a/2874951/4306705 but there also only one URL ran not all. I am sensing all urls are same except message parameter, may be that is the cause? – yuri1000 Apr 03 '17 at 09:16

1 Answers1

2

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.

Saili Jaguste
  • 146
  • 10
  • Hi, thanks for helping. Its vaguely running, sometimes its running 1 url and sometimes 3 and sometimes 2. I dont understand – yuri1000 Apr 03 '17 at 10:05