smpp

Hello friends,

If you have a smpp account you can simple send sms / unicode to any number using php. It is very simple .

Sample code is:

requireonce('smppclass.php'); $smpphost = "203.199.142.41"; // your host address $smppport = 2345; $systemid = "idsfds"; // Your system id $password = "es223"; // Your smpp account password $systemtype = "Rdsd "; // Your userid $from = "919846341106"; // From number $smpp = new SMPPClass(); $smpp->SetSender($from); /* bind to smpp server / $smpp->Start($smpphost, $smppport, $systemid, $password, $system_type); / send enquire link PDU to smpp server / $smpp->TestLink(); / send single message; large messages are automatically split / $smpp->Send("971532663061", "This is a test message. Give me a missed call if you get this. sajith"); / send unicode message / //$smpp->Send("31648072766", "صباحالخير", true); / send message to multiple recipients at once / //$smpp->SendMulti("31648072766,31651931985", "This is my PHP message"); / unbind from smpp server / $smpp->End(); //Click read more for smppclass.php library More... //file smppclass.php define(CM_BIND_TRANSMITTER, 0x00000002); define(CM_SUBMIT_SM, 0x00000004); define(CM_SUBMIT_MULTI, 0x00000021); define(CM_UNBIND, 0x00000006); define(CM_ENQUIRELINK, 0x00000015); class SMPPClass { // public members: / Constructor. Parameters: none. Example: $smpp = new SMPPClass(); / function SMPPClass() { / seed random generator / list($usec, $sec) = explode(' ', microtime()); $seed = (float) $sec + ((float) $usec * 100000); srand($seed); / initialize member variables / $this->_debug = true; / set this to false if you want to suppress debug output. / $this->_socket = NULL; $this->_command_status = 0; $this->_sequence_number = 1; $this->_source_address = ""; $this->_message_sequence = rand(1,255); } / For SMS gateways that support sender-ID branding, the method can be used to set the originating address. Parameters: $from : Originating address Example: $smpp->SetSender("31495595392"); / function SetSender($from) { if (strlen($from) > 20) { $this->debug("Error: sender id too long.n"); return; } $this->_source_address = $from; } / This method initiates an SMPP session. It is to be called BEFORE using the Send() method. Parameters: $host : SMPP ip to connect to. $port : port # to connect to. $username : SMPP system ID $password : SMPP passord. $systemtype : SMPP System type Returns: true if successful, otherwise false Example: $smpp->Start("smpp.chimit.nl", 2345, "chimit", "mypassword", "client01"); / function Start($host, $port, $username, $password, $system_type) { / $testarr = streamgettransports(); $havetcp = false; reset($testarr); while (list(, $transport) = each($testarr)) { if ($transport "tcpp") { $havetcp = true; } } if (!$havetcp) { $this->debug("No TCP support in this version of PHP.n"); return false; } */ $this->socket = fsockopen($host, $port, $errno, $errstr, 20); // todo: sanity check on input parameters if (!$this->socket) { $this->debug("Error opening SMPP session.n"); $this->debug("Error was: $errstr.n"); return; } socketsettimeout($this->socket, 1200); $status = $this->SendBindTransmitter($username, $password, $systemtype); if ($status != 0) { $this->debug("Error binding to SMPP server. Invalid credentials?n"); } return ($status 0); } /* This method sends out one SMS message. Parameters: $to : destination address. $text : text of message to send. $unicode: Optional. Indicates if input string is html encoded unicode. Returns: true if messages sent successfull, otherwise false. Example: $smpp->Send("31649072766", "This is an SMPP Test message."); $smpp->Send("31648072766", "صباحالخير", true); */ function Send($to, $text, $unicode = false) { if (strlen($to) > 20) { $this->debug("to-address too long.n"); return; } if (!$this->socket) { $this->debug("Not connected, while trying to send SUBMITSM.n"); // return; } $servicetype = ""; //default source TON and NPI for international sender $sourceaddrton = 1; $sourceaddrnpi = 1; $sourceaddr = $this->sourceaddress; if (pregmatch('/D/', $sourceaddr)) //alphanumeric sender { $sourceaddrton = 5; $sourceaddrnpi = 0; } elseif (strlen($sourceaddr) < 11) //national or shortcode sender { $sourceaddrton = 2; $sourceaddrnpi = 1; } $destaddrton = 1; $destaddrnpi = 1; $destinationaddr = $to; $esmclass = 3; $protocolid = 0; $priorityflag = 0; $scheduledeliverytime = ""; $validityperiod = ""; $registereddeliveryflag = 0; $replaceifpresentflag = 0; $datacoding = 241; $smdefaultmsgid = 0; if ($unicode) { $text = mbconvertencoding($text, "UCS-2BE", "HTML-ENTITIES"); /* UCS-2BE / $data_coding = 8; / UCS2 / $multi = $this->split_message_unicode($text); } else { $multi = $this->split_message($text); } $multiple = (count($multi) > 1); if ($multiple) { $esm_class += 0x00000040; } $result = true; reset($multi); while (list(, $part) = each($multi)) { $short_message = $part; $sm_length = strlen($short_message); $status = $this->SendSubmitSM($service_type, $source_addr_ton, $source_addr_npi, $source_addr, $dest_addr_ton, $dest_addr_npi, $destination_addr, $esm_class, $protocol_id, $priority_flag, $schedule_delivery_time, $validity_period, $registered_delivery_flag, $replace_if_present_flag, $data_coding, $sm_default_msg_id, $sm_length, $short_message); if ($status != 0) { $this->debug("SMPP server returned error $status.n"); $result = false; } } return $result; } / This method ends a SMPP session. Parameters: none Returns: true if successful, otherwise false Example: $smpp->End(); / function End() { if (!$this->_socket) { // not connected return; } $status = $this->SendUnbind(); if ($status != 0) { $this->debug("SMPP Server returned error $status.n"); } fclose($this->_socket); $this->_socket = NULL; return ($status 0); } / This method sends an enquirelink PDU to the server and waits for a response. Parameters: none Returns: true if successfull, otherwise false. Example: $smpp->TestLink() */ function TestLink() { $pdu = ""; $status = $this->SendPDU(CMENQUIRELINK, $pdu); return ($status 0); } /* This method sends a single message to a comma separated list of phone numbers. There is no limit to the number of messages to send. Parameters: $tolist : comma seperated list of phone numbers $text : text of message to send $unicode: Optional. Indicates if input string is html encoded unicode string. Returns: true if messages received by smpp server, otherwise false. Example: $smpp->SendMulti("31777110204,31649072766,...,...", "This is an SMPP Test message."); / function SendMulti($tolist, $text, $unicode = false) { if (!$this->_socket) { $this->debug("Not connected, while trying to send SUBMIT_MULTI.n"); // return; } $service_type = ""; $source_addr = $this->_source_address; //default source TON and NPI for international sender $source_addr_ton = 1; $source_addr_npi = 1; $source_addr = $this->_source_address; if (preg_match('/D/', $source_addr)) //alphanumeric sender { $source_addr_ton = 5; $source_addr_npi = 0; } elseif (strlen($source_addr) < 11) //national or shortcode sender { $source_addr_ton = 2; $source_addr_npi = 1; } $dest_addr_ton = 1; $dest_addr_npi = 1; $destination_arr = explode(",", $tolist); $esm_class = 3; $protocol_id = 0; $priority_flag = 0; $schedule_delivery_time = ""; $validity_period = ""; $registered_delivery_flag = 0; $replace_if_present_flag = 0; $data_coding = 241; $sm_default_msg_id = 0; if ($unicode) { $text = mb_convert_encoding($text, "UCS-2BE", "HTML-ENTITIES"); $data_coding = 8; / UCS2 */ $multi = $this->splitmessageunicode($text); } else { $multi = $this->splitmessage($text); } $multiple = (count($multi) > 1); if ($multiple) { $esmclass += 0x00000040; } $result = true; reset($multi); while (list(, $part) = each($multi)) { $shortmessage = $part; $smlength = strlen($shortmessage); $status = $this->SendSubmitMulti($servicetype, $sourceaddrton, $sourceaddrnpi, $sourceaddr, $destaddrton, $destaddrnpi, $destinationarr, $esmclass, $protocolid, $priorityflag, $scheduledeliverytime, $validityperiod, $registereddeliveryflag, $replaceifpresentflag, $datacoding, $smdefaultmsgid, $smlength, $shortmessage); if ($status != 0) { $this->debug("SMPP server returned error $status.n"); $result = false; } } return $result; } // private members (not documented): function ExpectPDU($oursequencenumber) { do { $this->debug("Trying to read PDU.n"); if (feof($this->socket)) { $this->debug("Socket was closed.!!n"); } $elength = fread($this->socket, 4); if (empty($elength)) { $this->debug("Connection lost.n"); return; } extract(unpack("Nlength", $elength)); $this->debug("Reading PDU : $length bytes.n"); $stream = fread($this->socket, $length - 4); $this->debug("Stream len : " . strlen($stream) . "n"); extract(unpack("Ncommandid/Ncommandstatus/Nsequencenumber", $stream)); $commandid &= 0x0fffffff; $this->debug("Command id : $commandid.n"); $this->debug("Command status : $commandstatus.n"); $this->debug("sequencenumber : $sequencenumber.n"); $pdu = substr($stream, 12); switch ($commandid) { case CMBINDTRANSMITTER: $this->debug("Got CMBINDTRANSMITTERRESP.n"); $spec = "asystemid"; extract($this->unpack2($spec, $pdu)); $this->debug("system id : $systemid.n"); break; case CMUNBIND: $this->debug("Got CMUNBINDRESP.n"); break; case CMSUBMITSM: $this->debug("Got CMSUBMITSMRESP.n"); if ($commandstatus 0) { $spec = "amessageid"; extract($this->unpack2($spec, $pdu)); $this->debug("message id : $messageid.n"); } break; case CMSUBMITMULTI: $this->debug("Got CMSUBMITMULTIRESP.n"); $spec = "amessageid/cnounsuccess/"; extract($this->unpack2($spec, $pdu)); $this->debug("message id : $messageid.n"); $this->debug("nounsuccess : $nounsuccess.n"); break; case CMENQUIRELINK: $this->debug("GOT CMENQUIRELINKRESP.n"); break; default: $this->debug("Got unknown SMPP pdu.n"); break; } $this->debug("nReceived PDU: "); for ($i = 0; $i < strlen($stream); $i++) { if (ord($stream[$i]) < 32) $this->debug("(" . ord($stream[$i]) . ")"); else $this->debug($stream[$i]); } $this->debug("n"); } while ($sequencenumber != $oursequencenumber); return $commandstatus; } function SendPDU($commandid, $pdu) { $length = strlen($pdu) + 16; $header = pack("NNNN", $length, $commandid, $this->commandstatus, $this->sequencenumber); $this->debug("Sending PDU, len $lengthn"); $this->debug("Sending PDU, header-len " . strlen($header) . "n"); $this->debug("Sending PDU, commandid " . $commandid . "n"); fwrite($this->socket, $header . $pdu, $length); $status = $this->ExpectPDU($this->sequencenumber); $this->sequencenumber = $this->sequencenumber + 1; return $status; } function SendBindTransmitter($systemid, $smpppassword, $systemtype) { $systemid = $systemid . chr(0); $systemidlen = strlen($systemid); $smpppassword = $smpppassword . chr(0); $smpppasswordlen = strlen($smpppassword); $systemtype = $systemtype . chr(0); $systemtypelen = strlen($systemtype); $pdu = pack("a{$systemidlen}a{$smpppasswordlen}a{$systemtypelen}CCCa1", $systemid, $smpppassword, $systemtype, 0x33, 0, 0, chr(0)); $this->debug("Bind Transmitter PDU: "); for ($i = 0; $i < strlen($pdu); $i++) { $this->debug(ord($pdu[$i]) . " "); } $this->debug("n"); $status = $this->SendPDU(CMBINDTRANSMITTER, $pdu); return $status; } function SendUnbind() { $pdu = ""; $status = $this->SendPDU(CMUNBIND, $pdu); return $status; } function SendSubmitSM($servicetype, $sourceaddrton, $sourceaddrnpi, $sourceaddr, $destaddrton, $destaddrnpi, $destinationaddr, $esmclass, $protocolid, $priorityflag, $scheduledeliverytime, $validityperiod, $registereddeliveryflag, $replaceifpresentflag, $datacoding, $smdefaultmsgid, $smlength, $shortmessage) { $servicetype = $servicetype . chr(0); $servicetypelen = strlen($servicetype); $sourceaddr = $sourceaddr . chr(0); $sourceaddrlen = strlen($sourceaddr); $destinationaddr = $destinationaddr . chr(0); $destinationaddrlen = strlen($destinationaddr); $scheduledeliverytime = $scheduledeliverytime . chr(0); $scheduledeliverytimelen = strlen($scheduledeliverytime); $validityperiod = $validityperiod . chr(0); $validityperiodlen = strlen($validityperiod); // $shortmessage = $shortmessage . chr(0); $messagelen = $smlength; $spec = "a{$servicetypelen}cca{$sourceaddrlen}cca{$destinationaddrlen}ccca{$scheduledeliverytimelen}a{$validityperiodlen}ccccca{$messagelen}"; $this->debug("PDU spec: $spec.n"); $pdu = pack($spec, $servicetype, $sourceaddrton, $sourceaddrnpi, $sourceaddr, $destaddrton, $destaddrnpi, $destinationaddr, $esmclass, $protocolid, $priorityflag, $scheduledeliverytime, $validityperiod, $registereddeliveryflag, $replaceifpresentflag, $datacoding, $smdefaultmsgid, $smlength, $shortmessage); $status = $this->SendPDU(CMSUBMITSM, $pdu); return $status; } function SendSubmitMulti($servicetype, $sourceaddrton, $sourceaddrnpi, $sourceaddr, $destaddrton, $destaddrnpi, $destinationarr, $esmclass, $protocolid, $priorityflag, $scheduledeliverytime, $validityperiod, $registereddeliveryflag, $replaceifpresentflag, $datacoding, $smdefaultmsgid, $smlength, $shortmessage) { $servicetype = $servicetype . chr(0); $servicetypelen = strlen($servicetype); $sourceaddr = $sourceaddr . chr(0); $sourceaddrlen = strlen($sourceaddr); $numberdestinations = count($destinationarr); $destflag = 1; $spec = "a{$servicetypelen}cca{$sourceaddrlen}c"; $pdu = pack($spec, $servicetype, $sourceaddrton, $sourceaddrnpi, $sourceaddr, $numberdestinations ); $destflag = 1; reset($destinationarr); while (list(, $destinationaddr) = each($destinationarr)) { $destinationaddr .= chr(0); $destlen = strlen($destinationaddr); $spec = "ccca{$destlen}"; $pdu .= pack($spec, $destflag, $destaddrton, $destaddrnpi, $destinationaddr); } $scheduledeliverytime = $scheduledeliverytime . chr(0); $scheduledeliverytimelen = strlen($scheduledeliverytime); $validityperiod = $validityperiod . chr(0); $validityperiodlen = strlen($validityperiod); $messagelen = $smlength; $spec = "ccca{$scheduledeliverytimelen}a{$validityperiodlen}ccccca{$messagelen}"; $pdu .= pack($spec, $esmclass, $protocolid, $priorityflag, $scheduledeliverytime, $validityperiod, $registereddeliveryflag, $replaceifpresentflag, $datacoding, $smdefaultmsgid, $smlength, $shortmessage); $this->debug("nMulti PDU: "); for ($i = 0; $i < strlen($pdu); $i++) { if (ord($pdu[$i]) < 32) $this->debug("."); else $this->debug($pdu[$i]); } $this->debug("n"); $status = $this->SendPDU(CMSUBMITMULTI, $pdu); return $status; } function splitmessage($text) { $this->debug("In splitmessage.n"); $maxlen = 153; $res = array(); if (strlen($text) <= 160) { $this->debug("One message: " . strlen($text) . "n"); $res[] = $text; return $res; } $pos = 0; $msgsequence = $this->messagesequence++; $nummessages = ceil(strlen($text) / $maxlen); $partno = 1; while ($pos < strlen($text)) { $ttext = substr($text, $pos, $maxlen); $pos += strlen($ttext); $udh = pack("cccccc", 5, 0, 3, $msgsequence, $nummessages, $partno); $partno++; $res[] = $udh . $ttext; $this->debug("Split: UDH = "); for ($i = 0; $i < strlen($udh); $i++) { $this->debug(ord($udh[$i]) . " "); } $this->debug("n"); $this->debug("Split: $ttext.n"); } return $res; } function splitmessageunicode($text) { $this->debug("In splitmessage.n"); $maxlen = 134; $res = array(); if (mbstrlen($text) <= 140) { $this->debug("One message: " . mbstrlen($text) . "n"); $res[] = $text; return $res; } $pos = 0; $msgsequence = $this->messagesequence++; $nummessages = ceil(mbstrlen($text) / $maxlen); $partno = 1; while ($pos < mbstrlen($text)) { $ttext = mbsubstr($text, $pos, $maxlen); $pos += mbstrlen($ttext); $udh = pack("cccccc", 5, 0, 3, $msgsequence, $nummessages, $partno); $partno++; $res[] = $udh . $ttext; $this->debug("Split: UDH = "); for ($i = 0; $i < strlen($udh); $i++) { $this->debug(ord($udh[$i]) . " "); } $this->debug("n"); $this->debug("Split: $ttext.n"); } return $res; } function unpack2($spec, $data) { $res = array(); $specs = explode("/", $spec); $pos = 0; reset($specs); while (list(, $sp) = each($specs)) { $subject = substr($data, $pos); $type = substr($sp, 0, 1); $var = substr($sp, 1); switch ($type) { case "N": $temp = unpack("Ntemp2", $subject); $res[$var] = $temp["temp2"]; $pos += 4; break; case "c": $temp = unpack("ctemp2", $subject); $res[$var] = $temp["temp2"]; $pos += 1; break; case "a": $pos2 = strpos($subject, chr(0)) + 1; $temp = unpack("a{$pos2}temp2", $subject); $res[$var] = $temp["temp2"]; $pos += $pos2; break; } } return $res; } function debug($str) { if ($this->debug) { echo $str; } } };