3

In my PHP application, I am running curl_exec inside foreach loop.

$myarray = array('0'=>'mobileno.1', '1'=>'mobileno.2'); // Contains 4000 records - from contacts table
foreach($myarray as $myarr)
{
    $tomobile = $myarr;
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $sms_gateway_url."?username=".$sms_username."&api_password=".$sms_password."&to=".$tomobile."&priority=".$sms_priority."&sender=".$sender_id."&message=".$mes
    ));
    $resp = curl_exec($curl);
    curl_close($curl);

    if (strpos($resp, "Submitted") !== false)
    {
        $status = 'success';
    }
    else
    {
        $status = 'failed';
    }
    // mysql insert query (sendsms table)
    // mysql update query (contacts table)
}

Here, the logic is for each execution, I will get 4000 unique records from contacts table, and send SMS to each one using foreach loop.

Here the problem is sometimes duplicate SMS are sending for same contacts. For eg, in a single execution, my scripts is sending around 5000 SMS instead of 4000 SMS.

Example: If I send 4000 SMS, more than 5000 SMS have sent and same 5000 rows are inserted in sendsms table. The duplicate count gets varies for every execution. If I hide curl options, 4000 rows are inserted in sendsms table correctly.

3
  • 2
    This is not really an optimized method. If your server url accepts multiple numbers at the same time, try serializing the array or something with json. I suggest you to check those first before get into this Commented Nov 12, 2015 at 6:28
  • @ling.s : Actually i am sending emails and doing some other functionalities like delay timer, date timer along with the above SMS case. So, this is the logic which suits for all my cases. I have to check one by one entries in this sequence. Commented Nov 12, 2015 at 6:46
  • Your code looks ok. If you are using Clickatel, then I suggest that you follow @ling.s suggestion and then work through the API to check the last status however as a quick fix would be to add the Klogger class and to log each CURL send so you can check what you are actually sending and to see if there is some reason for this. Let us know how you go Commented Nov 14, 2015 at 13:50

0

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.