예제 #1
0
 /**
  * Sends a request to the API for getting an advertisement. 
  * 
  * @param string       $category  category of this app.
  * @param string       $userAgent user agent string to send to API.
  * @param string       $udid      specifies a universially unique
  *                                identifier, which must be at least 30
  *                                characters in length.
  * @param OptArgs|null $optArgs   any optional values.
  *
  * @return null|ADSResponse null if no ads were returned, 
  *                          otherwise an ADSResponse object
  * @throws ServiceException if API request was not successful
  */
 public function getAdvertisement($category, $userAgent, $udid, OptArgs $optArgs = null, $raw_response = false)
 {
     $endpoint = $this->getFqdn() . '/rest/1/ads';
     $req = new RestfulRequest($endpoint);
     $req->setAuthorizationHeader($this->getToken())->setHeader('User-agent', $userAgent)->setHeader('Udid', $udid);
     $httpGet = new HttpGet();
     $httpGet->setParam('Category', $category);
     if ($optArgs != null) {
         $this->_appendOptArgs($httpGet, $optArgs);
     }
     $result = $req->sendHttpGet($httpGet);
     // no ads returned
     if ($result->getResponseCode() == 204) {
         if ($raw_response) {
             return $result->getResponseBody();
         }
         return null;
     }
     if ($raw_response) {
         return Service::parseApiResposeBody($result);
     }
     // response as json array
     $jarr = Service::parseJson($result);
     return ADSResponse::fromArray($jarr);
 }
예제 #2
0
 /**
  * Sends a request to the API for getting any SMS messages that were sent 
  * to the specified short code. 
  *
  * @param string $shortCode gets messages sent to this short code
  *
  * @return GetSMSResponse API response
  * @throws ServiceException if API request was not successful
  */
 public function getMessages($shortCode, $raw_response = false)
 {
     $endpoint = $this->getFqdn() . '/sms/v3/messaging/inbox/' . urlencode($shortCode);
     $req = new RESTFulRequest($endpoint);
     $result = $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/x-www-form-urlencoded')->sendHttpGet();
     // 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 GetSMSResponse::fromArray($arr);
 }
예제 #3
0
 /**
  * Sends a request to the API for converting speech to text.
  *
  * @param string      $fname            file location that contains speech
  *                                      to convert.
  * @param string      $speechContext    speech context to use.
  * @param string|null $speechSubContext speech sub context to use, if not
  *                                      null.
  * @param string|null $xArg             optional arguments to use, if not
  *                                      null.
  * @param boolean     $chunked          whether to send the request chunked.
  *
  * @return SpeechResponse API response as a SpeechResponse object.
  * @throws ServiceException if API request was not successful.
  */
 public function speechToText($fname, $speechContext, $speechSubContext = null, $xArg = null, $chunked = true, $language = null, $raw_response = false, $ftype = null)
 {
     // read file
     $fileResource = fopen($fname, 'r');
     if (!$fileResource) {
         throw new InvalidArgumentException('Could not open file ' . $fname);
     }
     $fileBinary = fread($fileResource, filesize($fname));
     fclose($fileResource);
     $endpoint = $this->getFqdn() . '/speech/v3/speechToText';
     $req = new RESTFulRequest($endpoint);
     $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('X-SpeechContext', $speechContext);
     if ($ftype != null) {
         $req->setHeader('Content-Type', $ftype);
     } else {
         $req->setHeader('Content-Type', $this->_getFileMIMEType($fname));
     }
     if ($chunked) {
         $req->setHeader('Transfer-Encoding', 'chunked');
     } else {
         $req->setHeader('Content-Length', filesize($fname));
     }
     if ($language != null) {
         $req->setHeader('Content-Language', $language);
     }
     if ($xArg != null) {
         $req->setHeader('xArg', $xArg);
     }
     if ($speechSubContext != null) {
         $req->setHeader('X-SpeechSubContext', $speechSubContext);
     }
     $httpPost = new HttpPost();
     $httpPost->setBody($fileBinary);
     $result = $req->sendHttpPost($httpPost);
     if ($raw_response) {
         $body = Service::parseApiResposeBody($result);
         // Note: This could throw ServiceExeption
         return $body;
     }
     $jsonArr = Service::parseJson($result);
     return SpeechResponse::fromArray($jsonArr);
 }