Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gaan10's avatar

Using foreach,i am trying to send sms to multiple nos but able to sent to only one.

I am using foreach to iterate over an array and send sms to each nos,but not able to do so.It send to only a single. Mt code is:

foreach($new_tokens as $new_tokenss)

          {
        
           foreach($new_tokenss as $new_token)
        
                {
            
    $message = "Message";
            $message_encoded = urlencode($message);
            $postData = array(
                'authkey' => \Config::get('procedures.msg91.authKey'),
                'mobiles' => $new_token->mob,
                'message' => $message_encoded,
                'sender' => \Config::get('procedures.msg91.senderId'),
                'route' => \Config::get('procedures.msg91.route')
            );
          
            $curl = new \anlutro\cURL\cURL;
            $curl->post(\Config::get('procedures.msg91.api_url'), $postData);
            return 'New emergency patient';
        }
              
            }

The 2nd foreach should send to each new_token->mob .but it sends to one and then stops.what am doing wrong?

0 likes
13 replies
Cronix's avatar
'mobiles' => $token3[0],

where does $token3 come from?

It's also hard to answer your question, because we have no idea what your $new_tokens that you are foreach'ing over looks like.

gaan10's avatar

sorry,my bad,i updated it. Actually $new_tokens is some token.I am taking the mobile corresponding to that tokens. Is it ok now?

Cronix's avatar

Is it ok now?

I don't know, does it work? Again, It's hard to answer without seeing the data in $new_tokens

gaan10's avatar

No its not working. tried doing $record []=$new_token->mob and count the $records later, shows all the mobile nos.But when i put the msg body it shows only one time. Any pointers.

Cronix's avatar

Any pointers.

Lol, yes, the thing I've asked for twice now...show us the data in $new_tokens. Other than that, no one can really help without seeing what you're looping over.

dump($new_tokens)... copy/paste the output to here

gaan10's avatar

ok this is what new_tokens looks like:

array:1 [ 0 => Collection {#262 #items: array:5 [

  0 => {#252
    +"id": 2
    +"user_id": 90
    +"aadhar_no": "234356768767"
    +"district_counsellor_id": "EZDCI0008"
    +"mob": "9611321782"
    +"address_id": 52
    +"token": "llllololol"
    +"created_at": "2018-04-23 04:17:14"
    +"updated_at": "2018-04-24 04:33:51"
    +"district": "mumbai"
  }

  1 => {#253
    +"id": 1
    +"user_id": 92
    +"aadhar_no": "123445676789"
    +"district_counsellor_id": "EZDCI0009"
+"mob": "9066578876"
    +"address_id": 50
    +"token": "lopollopoolooppo"
    +"created_at": "2018-04-23 09:23:23"
    +"updated_at": "2018-04-23 20:00:00"
    +"district": "bangalore"
  }

.........like this json

gaan10's avatar

Also i have seen that without curl and post it iterates,only when i give curl and post it iterates 1 time. Any solutions plz

Cronix's avatar

Based on that, it seems you only need one loop...the first one. I'm not sure what you're trying to do with the 2nd loop.

Snapey's avatar

arse. I didn't even see the first loop as its outside the code block.

What a mess.

foreach($new_tokens as $new_token) {
            
    $postData = array(
        'authkey' => \Config::get('procedures.msg91.authKey'),
        'mobiles' => $new_token->mob,
        'message' => urlencode("Message"),
        'sender' => \Config::get('procedures.msg91.senderId'),
        'route' => \Config::get('procedures.msg91.route')
        );
          
    //$curl = new \anlutro\cURL\cURL;
    //$curl->post(\Config::get('procedures.msg91.api_url'), $postData);

    dump($postData);              
}

take curl out of it until you have proved it iterates the correct number of times and with the correct data available.

Snapey's avatar
Snapey
Best Answer
Level 122

or better

$postData = array(
        'authkey' => \Config::get('procedures.msg91.authKey'),
        'message' => urlencode("Message"),
        'sender' => \Config::get('procedures.msg91.senderId'),
        'route' => \Config::get('procedures.msg91.route')
        );

foreach($new_tokens as $new_token) {
            
    $postData['mobiles'] = $new_token->mob;
          
    //$curl = new \anlutro\cURL\cURL;
    //$curl->post(\Config::get('procedures.msg91.api_url'), $postData);

    dump($postData);              
}

1 like
gaan10's avatar

@Snapey as i already said without curl it works.But if i don't use curl then how to send the sms

gaan10's avatar

This the dump postdata:

array:5 [ "authkey" => "115544AE7P5IoXv5757e408" "message" => "Message" "sender" => "EZHLTH" "route" => "4" "mobiles" => "9611321782" ] array:5 [ "authkey" => "115544AE7P5IoXv5757e408" "message" => "Message" "sender" => "EZHLTH" "route" => "4" "mobiles" => "9066578876" ]

How to use curl now?Any help plz. Without curl all works .

Please or to participate in this conversation.