/** * Sends a request to the API for sending a SMS to the specified address. * * @param string $addr address to which SMS should be sent. * @param string $msg SMS message body to send. * @param boolean $notifyDeliveryStatus whether the API should sent a * notification after delivery. * * @return SendSMSResponse API response. * @throws ServiceException if API request was not successful. */ public function sendSMS($addr, $msg, $notifyDeliveryStatus = true) { $vals = array('address' => $addr, 'message' => $msg, 'notifyDeliveryStatus' => $notifyDeliveryStatus); $jsobj = array('outboundSMSRequest' => $vals); $jvals = json_encode($jsobj); $endpoint = $this->getFqdn() . '/sms/v3/messaging/outbox'; $req = new RESTFulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/json'); $httpPost = new HttpPost(); $httpPost->setBody($jvals); $result = $req->sendHttpPost($httpPost); $arr = Service::parseJson($result); return SendSMSResponse::fromArray($arr); }
/** * Sends a request to the API for sending a SMS to the specified address. * * @param string $addr address to which SMS should be sent. * @param string $msg SMS message body to send. * @param boolean $notifyDeliveryStatus whether the API should sent a * notification after delivery. * * @return SendSMSResponse API response. * @throws ServiceException if API request was not successful. */ public function sendSMS($addr, $msg, $notifyDeliveryStatus = false, $raw_response = false) { $vals = array('address' => $addr, 'message' => $msg, 'notifyDeliveryStatus' => $notifyDeliveryStatus); $jsobj = array('outboundSMSRequest' => $vals); $jvals = json_encode($jsobj); $endpoint = $this->getFqdn() . '/sms/v3/messaging/outbox'; $req = new RESTFulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/json'); $httpPost = new HttpPost(); $httpPost->setBody($jvals); $result = $req->sendHttpPost($httpPost); // Handle the flag to return json. if ($raw_response) { $body = Service::parseApiResposeBody($result); // Note: This could throw ServiceExeption return $body; } $arr = Service::parseJson($result); return SendSMSResponse::fromArray($arr); }
/** * Sends a message to the specified addresses. * * @param array $addresses strings that holds the addresses to which * the specified messages will be sent. * @param string|null $text text body of message or null if none * @param string|null $subject subject of message or null if none * @param array|null $fnames file names of attachments or null if none * @param bool|null $isGroup whether to send as broadcast or null to * use default * * @return string message id * @throws ServiceException if API request was not successful */ public function sendMessage($addresses, $text, $subject, $fnames = null, $isGroup = null) { $endpoint = $this->getFqdn() . '/myMessages/v2/messages'; $req = new RESTFulRequest($endpoint); $req->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/json')->setAuthorizationHeader($this->getToken()); $vals = array('addresses' => $addresses); $vals['isGroup'] = $isGroup ? 'true' : 'false'; if ($text !== null) { $vals['text'] = $text; } if ($subject !== null) { $vals['subject'] = $subject; } $messageRequest = array('messageRequest' => $vals); $jvals = json_encode($messageRequest); $result = null; if ($fnames == null) { // no attachments; send basic POST $req->setHeader('Content-Type', 'application/json'); $httpPost = new HttpPost(); $httpPost->setBody($jvals); $result = $req->sendHttpPost($httpPost); } else { // attachments; send as multipart $mpart = new HttpMultipart(); $mpart->addJSONPart($jvals); foreach ($fnames as $fname) { $mpart->addFilePart($fname); } $result = $req->sendHttpMultipart($mpart); } $responseArr = Service::parseJson($result); return $responseArr['id']; }
/** * Sends a request to the API for converting text to speech. * * @param string $ctype content type. * @param string $txt text to convert to speech. * @param string|null $xArg optional arguments to set, if not null. * * @return raw audio/x-wave data. * @throws ServiceException if API request was not successful. */ public function textToSpeech($ctype, $txt, $xArg = null) { $endpoint = $this->getFqdn() . '/speech/v3/textToSpeech'; $req = new RESTFulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'audio/x-wav')->setHeader('Content-Type', $ctype); if ($xArg != null) { $req->setHeader('X-Arg', $xArg); } $httpPost = new HttpPost(); $httpPost->setBody($txt); $result = $req->sendHttpPost($httpPost); $code = $result->getResponseCode(); if ($code != 200 && $code != 201) { throw new ServiceException($body, $code); } $body = $result->getResponseBody(); return $body; }
/** * Sends a request to the API gateway adding contacts to a group. * * @param string $groupId group id to add contacts to * @param array $contactIds contact ids to add to group * * @return void * @throws ServiceException if request was not successful */ public function addContactsToGroup($groupId, $contactIds) { $groupId = urlencode($groupId); $contactIds = urlencode(implode(',', $contactIds)); $subUrl = "/addressBook/v1/groups/{$groupId}/contacts?contactIds={$contactIds}"; $endpoint = $this->getFqdn() . $subUrl; $req = new RESTFulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/json'); $result = $req->sendHttpPost(); $code = $result->getResponseCode(); if ($code != 204) { throw new ServiceException($result->getResponseBody(), $code); } }