/** * {@inheritDoc} */ public function send(SmsInterface $sms, GatewayInterface $gateway) { // Get the gateway configurations $configs = $gateway->getConfigs(); // Create a new socket transport $transport = new \SocketTransport(array($gateway->getHost()), $gateway->getPort(), $configs['persistent']); $transport->setSendTimeout($configs['send_timeout']); $transport->setRecvTimeout($configs['receive_timeout']); $transport->debug = $configs['debug']; // Create a new SMPP client $smpp = new \SmppClient($transport); $smpp->debug = $configs['debug']; // Open the connection $transport->open(); $smpp->bindTransmitter($gateway->getUsername(), $gateway->getPassword()); // Configure a sender, recipient and message $sender = new \SmppAddress($sms->getSender(), $configs['sender']['ton'], $configs['sender']['npi']); $recipient = new \SmppAddress($sms->getRecipient(), $configs['recipient']['ton'], $configs['recipient']['npi']); $message = \GsmEncoder::utf8_to_gsm0338($sms->getMessage()); // Send an SMS and close the connection $messageId = $smpp->sendSMS($sender, $recipient, $message); $smpp->close(); return $messageId; }
/** * Parse received PDU from SMSC. * @param SmppPdu $pdu - received PDU from SMSC. * @return parsed PDU as array. */ protected function parseSMS(SmppPdu $pdu) { // Check command id if ($pdu->id != SMPP::DELIVER_SM) { throw new InvalidArgumentException('PDU is not an received SMS'); } // Unpack PDU $ar = unpack("C*", $pdu->body); // Read mandatory params $service_type = $this->getString($ar, 6, true); $source_addr_ton = next($ar); $source_addr_npi = next($ar); $source_addr = $this->getString($ar, 21); $source = new SmppAddress($source_addr, $source_addr_ton, $source_addr_npi); $dest_addr_ton = next($ar); $dest_addr_npi = next($ar); $destination_addr = $this->getString($ar, 21); $destination = new SmppAddress($destination_addr, $dest_addr_ton, $dest_addr_npi); $esmClass = next($ar); $protocolId = next($ar); $priorityFlag = next($ar); next($ar); // schedule_delivery_time next($ar); // validity_period $registeredDelivery = next($ar); next($ar); // replace_if_present_flag $dataCoding = next($ar); next($ar); // sm_default_msg_id $sm_length = next($ar); $message = $this->getOctets($ar, $sm_length); if ($dataCoding == 8) { $message = GsmEncoder::ucs2_to_utf8($message); } // Check for optional params, and parse them if (current($ar) !== false) { $tags = array(); do { $tag = $this->parseTag($ar); if ($tag !== false) { $tags[] = $tag; } } while (current($ar) !== false); } else { $tags = null; } if (($esmClass & SMPP::ESM_DELIVER_SMSC_RECEIPT) != 0) { $sms = new SmppDeliveryReceipt($pdu->id, $pdu->status, $pdu->sequence, $pdu->body, $service_type, $source, $destination, $esmClass, $protocolId, $priorityFlag, $registeredDelivery, $dataCoding, $message, $tags); $sms->parseDeliveryReceipt(); } else { $sms = new SmppSms($pdu->id, $pdu->status, $pdu->sequence, $pdu->body, $service_type, $source, $destination, $esmClass, $protocolId, $priorityFlag, $registeredDelivery, $dataCoding, $message, $tags); } if ($this->debug) { call_user_func($this->debugHandler, "Received sms:\n" . print_r($sms, true)); } // Send response of recieving sms $response = new SmppPdu(SMPP::DELIVER_SM_RESP, SMPP::ESME_ROK, $pdu->sequence, ""); $this->sendPDU($response); return $sms; }