简体   繁体   中英

How can i foreach data from my array to an xmls tags so as to send message to multiple numbers if i would query from database

I have this provide who provide SMS API in my country, every thing works fine in sending sms to single user, the issue comes when i need to send to multple users from the database, they use XML to send to multiple users by hardcoding the numbers inside xml , whenever I try to foreach the value from my array it's give the error of undefined variable.

I have tried using print_r() , implode(",",$var) , json_encode() they seems to work, but when i loop still i get the error

 public function multi(){

             define ("URL_API_DOMAIN", "http://www.bongolive.co.tz/api/broadcastSMS.php");
        $sendername = "mysendername";
        $username = "myusername";
        $password = "mypassword";
        $apikey = "mykey";
        $numbers=array("+255*********","+255********");
        $number=print_r($numbers);
        // foreach ($numbers as $key => $numb) {
        //  echo $number = $numb;
        // }
        // return $number;
        $callbackURL = "";
        $messageXML = "
<Broadcast>
    <Authentication>
        <Sendername>".$sendername."</Sendername>
        <Username>".$username."</Username>
        <Password>".$password."</Password>
        <Apikey>".$apikey."</Apikey>
    </Authentication>
        <Message>
            <Content>Test broad cast from eboaard Message, Ukipata hii text nijulishe MAKAVELI</Content>
            <Receivers>

            foreach($number as $numb){
                <Receiver id=''>".$numb."</Receiver>
            }
            </Receivers>
                <Callbackurl>".$callbackURL."</Callbackurl>
        </Message>
 </Broadcast>";
 $data = array('messageXML' => $messageXML);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, URL_API_DOMAIN);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

  $response = curl_exec($ch);
  echo "response = $response ";
        }

}

//their codes example
<?php
        define ("URL_API_DOMAIN", "http://www.bongolive.co.tz/api/broadcastSMS.php");
        $sendername = "Bongo Live";
        $username = "testaccount";
        $password = "123456";
        $apikey = "c4a12fa8-ed6f-11df-a1f1-00181236674f";
        $callbackURL = "http://www.yourdomain.com/sms/dlr.php";
        $messageXML = "
<Broadcast>
    <Authentication>
        <Sendername>".$sendername."</Sendername>
        <Username>".$username."</Username>
        <Password>".$password."</Password>
        <Apikey>".$apikey."</Apikey>
    </Authentication>
        <Message>
            <Content>Test Message</Content>
            <Receivers>
                <Receiver id='7772237998'>255655123123</Receiver>
            </Receivers>
                <Callbackurl>".$callbackURL."</Callbackurl>
        </Message>
 </Broadcast>";
 $data = array('messageXML' => $messageXML);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, URL_API_DOMAIN);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

  $response = curl_exec($ch);
  echo "response = $response ";
 ?>

I expected to send messages to my two numbers, but I get ErrorException (E_NOTICE) Undefined variable: numb error , am using laravel controllers, am don't have much knowledge on xmls

Change this:

$messageXML = "
<Broadcast>
    <Authentication>
        <Sendername>".$sendername."</Sendername>
        <Username>".$username."</Username>
        <Password>".$password."</Password>
        <Apikey>".$apikey."</Apikey>
    </Authentication>
    <Message>
        <Content>Test broad cast from eboaard Message, Ukipata hii text nijulishe MAKAVELI</Content>
        <Receivers>

        foreach($number as $numb){
            <Receiver id=''>".$numb."</Receiver>
        }
        </Receivers>
            <Callbackurl>".$callbackURL."</Callbackurl>
    </Message>
 </Broadcast>";

To this:

$messageXML = "<Broadcast><Authentication>".
              "<Sendername>{$sendername}</Sendername>".
              "<Username>{$username}</Username>".
              "<Password>{$password}</Password>".
              "<Apikey>{$apikey}</Apikey></Authentication>".
              "<Message><Content>Test broad cast from eboaard ".
              "Message, Ukipata hii text nijulishe MAKAVELI</Content><Receivers>";

foreach($number as $numb) {
    $messageXML .= "<Receiver id=''>{$numb}</Receiver>";
}

$messageXML .= "</Receivers><Callbackurl>{$callbackURL}</Callbackurl></Message></Broadcast>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM