Esempio n. 1
0
 /**
  * {@inheritDoc}
  *
  * @see UnwireProvider::sendOne()
  */
 public function send($dry_run = false)
 {
     switch (count($this->messages)) {
         case 0:
             throw new RuntimeException("No messages to send.");
         case 1:
             return $this->sendOne($dry_run);
     }
     $xml = new SimpleXMLExtended('<?xml version="1.0" encoding="utf-8" ?><messages></messages>');
     $xml->addAttribute('xmlns', 'http://www.unwire.com/UM/core/v1.3');
     $xml->addAttribute('xsi:schemaLocation', 'http://www.unwire.com/UM/core/v1.3 https://gw.unwire.com/service/xmlinterface/core-1.3.xsd', 'http://www.w3.org/2001/XMLSchema-instance');
     $xml->addAttribute('count', count($this->messages));
     $xml->addAttribute('service', 'mt');
     $xml->addAttribute('sessionid', session_id() ?: time());
     $default = $xml->addChild('defaultvalues');
     $default->addAttribute('type', 'text');
     // set default values
     foreach ($this->getDefaultValues() as $key => $value) {
         if ($key == 'content') {
             $content = $default->addChild($key);
             $content->addChild('text', $value);
         } else {
             if ($key == 'price') {
                 $price = $default->addChild('price', $value['amount']);
                 $price->addAttribute('currency', $value['currency']);
                 if (!is_null($value['vat'])) {
                     $price->addAttribute('vat', $value['vat']);
                 }
             } else {
                 if ($value) {
                     $default->addChild($key, $value);
                 }
             }
         }
     }
     $i = 1;
     // add messages
     foreach ($this->messages as $message) {
         $msg = $xml->addChild('sms');
         $msg->addAttribute('id', $i);
         $msg->addAttribute('type', 'text');
         $msg->addChild('msisdn', $message->to);
         if (empty($this->defaults['content'])) {
             $content = $msg->addChild('content');
             $content->addChild('text', $message->message);
         }
         if (empty($this->defaults['operator']) && isset($message->options['smsc'])) {
             $msg->addChild('operator', $message->options['smsc']);
         }
         if (isset($message->options['shortcode'])) {
             $msg->addChild('mediacode', $message->options['shortcode']);
         } else {
             if (empty($this->defaults['shortcode'])) {
                 $msg->addChild('shortcode', $this->config['appnr']);
             }
         }
         if (isset($message->options['mediacode'])) {
             $msg->addChild('mediacode', $message->options['mediacode']);
         } else {
             if (empty($this->defaults['mediacode'])) {
                 $msg->addChild('mediacode', $this->config['mediacode']);
             }
         }
         if (isset($message->options['price'])) {
             preg_match('/([0-9.]+)([A-Z]{3})/', $message->options['price'], $matches);
             $price = $msg->addChild('price', $matches[1]);
             $price->addAttribute('currency', $matches[2]);
             if (!is_null($this->defaults['price']['vat'])) {
                 $price->addAttribute('vat', $this->defaults['price']['vat']);
             }
         }
         $i++;
     }
     $endpoint = str_replace('://', '://' . $this->config['user'] . ':' . $this->config['password'] . '@', self::BATCH_ENDPOINT);
     $adapter = $this->getAdapter();
     $adapter->setEndpoint($endpoint);
     $adapter->setBody($xml->asXML());
     if ($dry_run) {
         $response = $adapter->dryRun();
     } else {
         $response = $adapter->post();
     }
     // empty query
     $this->messages = array();
     return $response;
 }