Пример #1
0
 /**
  * @param Message|array $message
  * @return Message
  * @throws Exception\Exception
  * @throws Exception\Request
  * @throws Exception\Server
  */
 public function send($message)
 {
     if (!$message instanceof MessageInterface) {
         $message = $this->createMessageFromArray($message);
     }
     $params = $message->getRequestData(false);
     $request = new Request(\Nexmo\Client::BASE_REST . '/sms/json', 'POST', 'php://temp', ['content-type' => 'application/json']);
     $request->getBody()->write(json_encode($params));
     $message->setRequest($request);
     $response = $this->client->send($request);
     $message->setResponse($response);
     //check for valid data, as well as an error response from the API
     $data = $message->getResponseData();
     if (!isset($data['messages'])) {
         throw new Exception\Exception('unexpected response from API');
     }
     //normalize errors (client vrs server)
     foreach ($data['messages'] as $part) {
         switch ($part['status']) {
             case '0':
                 continue;
                 //all okay
             //all okay
             case '1':
                 if (preg_match('#\\[\\s+(\\d+)\\s+\\]#', $part['error-text'], $match)) {
                     usleep($match[1] + 1);
                 } else {
                     sleep(1);
                 }
                 return $this->send($message);
             case '5':
                 $e = new Exception\Server($part['error-text'], $part['status']);
                 $e->setEntity($message);
                 throw $e;
             default:
                 $e = new Exception\Request($part['error-text'], $part['status']);
                 $e->setEntity($message);
                 throw $e;
         }
     }
     return $message;
 }
Пример #2
0
 protected function getRequest($params, $path = null)
 {
     if (!is_null($path)) {
         $path = '/verify/' . $path . '/json';
     } else {
         $path = '/verify/json';
     }
     $request = new Request(\Nexmo\Client::BASE_API . $path, 'POST', 'php://temp', ['content-type' => 'application/json']);
     $request->getBody()->write(json_encode($params));
     return $request;
 }