I am new here to get answers for my issues, hoping for your kind advice. Thanks in advance.

I have written a HTTP API to send SMS using curl. Everything is working fine, except I am failing to loop and post curl for certain phone numbers. For example: User uploads 50000 phone numbers using excel sheet on my site, I fetch all the mobile numbers from the database, and then post it through CURL.

Now the sms gateway which I send the request accepts only maximum 10000 numbers at once via http api.

So from the 50000 fetched numbers I want to split the numbers to 10000 each and loop that and send curl post.

Here is my code

   //have taken care of sql injection on live site
    $resultRestore = mysql_query("SELECT * FROM temptable WHERE userid = '".$this->user_id."' AND  uploadid='".$uploadid."' ");
    $rowRestoreCount = mysql_num_rows($resultRestore);
    @mysql_data_seek($resultRestore, 0); 
    $phone_list = "";
    while($rowRestore = mysql_fetch_array($resultRestore))
    {
        $phone_list .= $rowRestore['recphone'].","; 
    }

    $url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode(substr($phone_list, 0, -1))."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
    //echo $url;
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch); 

Now, from the $phone_list, I need to loop for every 10000 numbers, How can I achieve this?

Its been 2 days, I have tried several things and not getting the result.

Kindly help...

Instead of this:

$phone_list = "";
while($rowRestore = mysql_fetch_array($resultRestore))
{
    $phone_list .= $rowRestore['recphone'].","; 
}

Do this:

$phone_list = array ();
$index = 0;
$count = 0;
while ($rowRestore = mysql_fetch_array($resultRestore))
{
    // $phone_list[0] .= $rowRestore['recphone'] . ','; 
    $phone_list[$index] .= $rowRestore['recphone'] . ','; 

    $count++;
    if ($count == 10000) 
    {
        $count = 0;
        $index++;
    }
}

foreach ($phone_list as $block)
{
    // process $block with curl
}

Thanks for taking time to respons my issue.

$block is taking the entire numbers, not the number which we are trying to limit to 10000

$block is taking the entire numbers, not the number which we are trying to limit to 10000

I don't understand what you mean, can you explain?

If query is for 25000 numbers, then it sends out all the 250000 numbers at once. It is not sending 10000 batch wise.

Show your changed code.

I used the same code as you have given, for example when I queried the database, there was total 25000 rows to be fetched when i fetched and used your code to send by 10000 batch but it all sent at once i.e. 25000 rows at once, it did not go through for 10000 each time. I dont know what is the issue...

Edit:
Foreach block

foreach ($phone_list as $block)
{
 $url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode(substr($block, 0, -1))."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch); 
}

Spotted. This:

$phone_list[0] .= $rowRestore['recphone'] . ',';

should be:

$phone_list[$index] .= $rowRestore['recphone'] . ',';

Now it only sends out the first 10000 numbers to the service provider and rest 15000 request not sent.

I suggest you debug and step through the code line by line to see what is happening.

I have no idea to debug further, $index++ is giving correct value, may be we are making the $count value to 0 before it proceeds further. I am too confused with this now and lost my brain into this.

I also commented $count=0 and tested still it stopped at 10000 and rest 15000 did not go through.

Is it perhaps a limitation of the service?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.