Beispiel #1
0
 public function testGetFileName()
 {
     $resource = fopen('php://memory', 'r+');
     fwrite($resource, 'foobar');
     rewind($resource);
     $file = new FileStream($resource, 'foo.txt', 'text/plain');
     $this->assertEquals('foo.txt', $file->getFileName());
     $this->assertEquals('text/plain', $file->getContentType());
     $file = new FileStream($resource, 'foo.txt');
     $this->assertEquals('foo.txt', $file->getFileName());
     $this->assertEquals(null, $file->getContentType());
 }
Beispiel #2
0
 /**
  * @param string $resourcePath path to method endpoint
  * @param string $method method to call
  * @param array $queryParams parameters to be place in query URL
  * @param array $postData parameters to be placed in POST body
  * @param array $headerParams parameters to be place in request header
  * @return unknown
  */
 public function callAPI($apiServer, $resourcePath, $method, $queryParams, $postData, $headerParams, FileStream $outFileStream = null)
 {
     $headers = array();
     foreach ($this->headers as $key => $val) {
         $headers[] = "{$key}: {$val}";
     }
     if ($headerParams != null) {
         foreach ($headerParams as $key => $val) {
             $headers[] = "{$key}: {$val}";
         }
     }
     $isFileUpload = false;
     if (empty($postData)) {
         $headers[] = "Content-type: text/html";
     } else {
         if ($postData instanceof FileStream) {
             $isFileUpload = true;
             $headers[] = "Content-type: application/octet-stream";
             $headers[] = "Content-Length: " . $postData->getSize();
         } else {
             if (is_object($postData) or is_array($postData) or is_string($postData)) {
                 $headers[] = "Content-type: application/json";
                 $postData = json_encode(self::object_to_array($postData));
             }
         }
     }
     $url = $apiServer . $resourcePath;
     $timeoutSec = 0;
     $curl = curl_init();
     if ($this->debug) {
         // curl_setopt($curl, CURLOPT_HEADER, true); // Display headers; returns null response
         curl_setopt($curl, CURLOPT_VERBOSE, true);
         // Display communication with server
         curl_setopt($curl, CURLOPT_STDERR, $curl_log = fopen($this->curlLogFilepath, 'a+'));
     }
     curl_setopt($curl, CURLOPT_TIMEOUT, $timeoutSec);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     // return the result on success, rather than just TRUE
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     if (!empty($queryParams)) {
         $url = $url . '?' . http_build_query($queryParams);
     }
     if ($method == self::$POST) {
         if ($isFileUpload) {
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
             curl_setopt($curl, CURLOPT_TIMEOUT, 0);
             curl_setopt($curl, CURLOPT_PUT, true);
             curl_setopt($curl, CURLOPT_INFILE, $postData->getInputStream());
             curl_setopt($curl, CURLOPT_INFILESIZE, $postData->getSize());
         } else {
             curl_setopt($curl, CURLOPT_POST, true);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
         }
     } else {
         if ($method == self::$PUT) {
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
             curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
         } else {
             if ($method == self::$DELETE) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
             }
         }
     }
     $url = self::encodeURI($this->signer->signUrl($url));
     curl_setopt($curl, CURLOPT_URL, $url);
     if ($outFileStream !== null) {
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
         // curl_setopt($curl, CURLOPT_FILE, $outFileStream->getInputStream());
         curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($outFileStream, 'headerCallback'));
         curl_setopt($curl, CURLOPT_WRITEFUNCTION, array($outFileStream, 'bodyCallback'));
     }
     if ($this->debug) {
         $body = "> Request Body: {$this->newline}";
         if ($isFileUpload) {
             fwrite($curl_log, "{$body} >>>stream info: size=" . $postData->getSize() . " content-type=" . $postData->getContentType());
         } else {
             fwrite($curl_log, $body . $postData);
         }
         echo $this->newline;
     }
     // Make the request
     $response = curl_exec($curl);
     $response_info = curl_getinfo($curl);
     // Close curl
     curl_close($curl);
     if ($this->debug) {
         $body = "< Response Body: {$this->newline}";
         if ($outFileStream !== null) {
             fwrite($curl_log, "{$body} <<<stream info: size=" . $outFileStream->getSize() . " content-type=" . $outFileStream->getContentType() . " filename=" . $outFileStream->getFileName());
         } else {
             fwrite($curl_log, $body . $response);
         }
         fwrite($curl_log, $this->newline);
         fclose($curl_log);
     }
     // Handle the response
     if ($response_info['http_code'] == 0) {
         throw new ApiException("TIMEOUT: api call to " . $url . " took more than " . $timeoutSec . "s to return");
     } else {
         if ($response_info['http_code'] == 200 || $response_info['http_code'] == 201 || $response_info['http_code'] == 202) {
             if ($outFileStream !== null) {
                 if (in_array('Transfer-Encoding', $outFileStream->headers) or $outFileStream->getSize() > 0) {
                     fclose($outFileStream->getInputStream());
                     return $outFileStream;
                 } else {
                     return null;
                 }
             } else {
                 return json_decode($response);
             }
         } else {
             if ($response_info['http_code'] == 401) {
                 throw new ApiException("Unauthorized API request to " . $url, 401);
             } else {
                 if ($response_info['http_code'] == 404) {
                     return null;
                 } else {
                     $msg = $response;
                     if ($outFileStream !== null and !empty($outFileStream->jsonError)) {
                         $msg = $outFileStream->jsonError;
                     }
                     $jsonArray = json_decode($msg, true);
                     if (is_array($jsonArray)) {
                         $msg = $jsonArray['error_message'];
                     }
                     throw new ApiException($msg, $response_info['http_code']);
                 }
             }
         }
     }
 }