/** * 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); }
/** * Revokes the specified token. * * @param string $token token to revoke * @param string $hint hint for token type * * @throws OAuthException if API gateway returned an error */ public function revokeToken($token, $hint = 'access_token') { $httpPost = new HttpPost(); $httpPost->setParam('client_id', $this->_clientId)->setParam('client_secret', $this->_clientSecret)->setParam('token', $token)->setParam('token_type_hint', $hint); $req = new RestfulRequest($this->_revoke_url); $result = $req->sendHttpPost($httpPost); if ($result->getResponseCode() != 200) { throw new OAuthException('HTTP Code', $result->getResponseBody()); } }
public function sendHttpMultipart(HttpMultipart $multipart) { $this->setHeader('Content-Type', $multipart->getContentType()); $httpPost = new HttpPost(); $httpPost->setBody($multipart->getMultipartRaw()); return $this->sendHttpPost($httpPost); }
/** * Sends a request to the api for generating a signed document and * signature, both of which will be contained within the returned notary * object. * * @param string $payload payload used to generate signature and signed * document * * @return Notary notary generated from payload * @throws ServiceException if api response isn't successful */ public function getNotary($payload) { $endpoint = $this->_fqdn . '/Security/Notary/Rest/1/SignedPayload'; $req = new RestfulRequest($endpoint); $httpPost = new HttpPost(); $this->_setHeaders($req); $httpPost->setBody($payload); $result = $req->sendHttpPost($httpPost); $responseArr = Service::parseJson($result); return new Notary($responseArr['SignedDocument'], $responseArr['Signature'], $payload); }
public function createMessageIndex() { $endpoint = $this->getFqdn() . '/myMessages/v2/messages/index'; $req = new RestfulRequest($endpoint); $req->setHeader('Accept', 'application/json')->setAuthorizationHeader($this->getToken()); $httpPost = new HttpPost(); $httpPost->setBody(' '); //empty body $result = $req->sendHttpPost($httpPost); if ($result->getResponseCode() != 202) { $body = $result->getResponseBody(); throw new ServiceException($result->getResponseCode(), $body); } }
/** * 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 for creating a group. * * @param Group $group group information to create * * @return string location of created resource * @throws ServiceException if request was not successful */ public function createGroup(Group $group) { $endpoint = $this->getFqdn() . '/addressBook/v1/groups'; $req = new RestfulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/json'); $httpPost = new HttpPost(); $httpPost->setBody(json_encode($group->toArray())); $result = $req->sendHttpPost($httpPost); $code = $result->getResponseCode(); if ($code != 201 && $code != 204) { throw new ServiceException($code, $result->getResponseBody()); } return $result->getHeader('location'); }
/** * Gets a new OAuth token using the refresh token of the specified OAuth * token. * * The token request is done using the <i>refresh_token</i> grant type. * * @param OAuthToken $token OAuth token to use for refreshing * * @return OAuthToken an OAuth token * @throws OAuthException if server did not return valid access token */ public function refreshToken(OAuthToken $token) { $httpPost = new HttpPost(); $httpPost->setParam('refresh_token', $token->getRefreshToken())->setParam('grant_type', 'refresh_token')->setParam('client_id', $this->_clientId)->setParam('client_secret', $this->_clientSecret); $req = new RestfulRequest($this->_url); $result = $req->sendHttpPost($httpPost); return $this->parseResult($result); }
public function createNotificationSubscription(CreateSubscriptionArgs $args) { $channelId = urlencode($args->getChannelId()); $suburl = '/notification/v1/channels/' . $channelId . '/subscriptions'; $endpoint = $this->getFqdn() . $suburl; $subscription = array("events" => $args->getEvents()); if ($args->getCallbackData() != null) { $subscription['callbackData'] = $args->getCallbackData(); } if ($args->getExpiresIn() != null) { $subscription['expiresIn'] = $args->getExpiresIn(); } $jvals = json_encode(array("subscription" => $subscription)); $httpPost = new HttpPost(); $httpPost->setBody($jvals); $req = new RestfulRequest($endpoint); $result = $req->setAuthorizationHeader($this->getToken())->setHeader('Content-Type', 'application/json')->setHeader('Accept', 'application/json')->sendHttpPost($httpPost); $successCodes = array(201); $arr = Service::parseJson($result, $successCodes); $arrSubscription = $arr['subscription']; $arrSubscriptionId = $arrSubscription['subscriptionId']; $arrExpiresIn = null; if (isset($arrSubscription['expiresIn'])) { $arrExpiresIn = $arrSubscription['expiresIn']; } $subscriptionResponse = new SubscriptionResponse($arrSubscriptionId, $arrExpiresIn); $contentType = $result->getHeader('content-type'); $location = $result->getHeader('location'); $systemTransId = $result->getHeader('x-systemTransactionId'); $createSubscriptionResponse = new CreateSubscriptionResponse($contentType, $location, $systemTransId, $subscriptionResponse); return $createSubscriptionResponse; }