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?

4
  • Are you using PDO? What does a dump of $row yield? Commented Apr 3, 2017 at 8:44
  • Using ADODB. I have given the array for url $row yield Commented Apr 3, 2017 at 8:46
  • you need to both init your curl and close it within your while loop, or instead use multi_curl_exec instead Commented Apr 3, 2017 at 8:56
  • @hassan I tried multi_curl_* too as per 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? Commented Apr 3, 2017 at 9:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for helping. Its vaguely running, sometimes its running 1 url and sometimes 3 and sometimes 2. I dont understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.