Example #1
0
 public function parseResponse(Request $response)
 {
     $contentType = $response->getResponseHeader('content-type');
     $contentType = explode(';', $contentType);
     $boundary = false;
     foreach ($contentType as $part) {
         $part = explode('=', $part, 2);
         if (isset($part[0]) && 'boundary' == trim($part[0])) {
             $boundary = $part[1];
         }
     }
     $body = $response->getResponseBody();
     if ($body) {
         $body = str_replace("--{$boundary}--", "--{$boundary}", $body);
         $parts = explode("--{$boundary}", $body);
         $responses = array();
         foreach ($parts as $part) {
             $part = trim($part);
             if (!empty($part)) {
                 list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
                 $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders);
                 $status = substr($part, 0, strpos($part, "\n"));
                 $status = explode(" ", $status);
                 $status = $status[1];
                 list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false);
                 $response = new Request("");
                 $response->setResponseHttpCode($status);
                 $response->setResponseHeaders($partHeaders);
                 $response->setResponseBody($partBody);
                 // Need content id.
                 $key = $metaHeaders['content-id'];
                 if (isset($this->expected_classes[$key]) && strlen($this->expected_classes[$key]) > 0) {
                     $class = $this->expected_classes[$key];
                     $response->setExpectedClass($class);
                 }
                 try {
                     $response = REST::decodeHttpResponse($response, $this->client);
                     $responses[$key] = $response;
                 } catch (Exception $e) {
                     // Store the exception as the response, so successful responses
                     // can be processed.
                     $responses[$key] = $e;
                 }
             }
         }
         return $responses;
     }
     return null;
 }
Example #2
0
 /**
  * Decode an HTTP Response.
  * @static
  * @throws Exception
  * @param Request $response The http response to be decoded.
  * @param Client $client
  * @return mixed|null
  */
 public static function decodeHttpResponse($response, Client $client = null)
 {
     $code = $response->getResponseHttpCode();
     $body = $response->getResponseBody();
     $decoded = null;
     if (intVal($code) >= 300) {
         $decoded = json_decode($body, true);
         $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
         if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
             // if we're getting a json encoded error definition, use that instead of the raw response
             // body for improved readability
             $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
         } else {
             $err .= ": ({$code}) {$body}";
         }
         $errors = null;
         // Specific check for APIs which don't return error details, such as Blogger.
         if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
             $errors = $decoded['error']['errors'];
         }
         $map = null;
         if ($client) {
             $client->getLogger()->error($err, array('code' => $code, 'errors' => $errors));
             $map = $client->getClassConfig('Exception', 'retry_map');
         }
         throw new Exception($err, $code, null, $errors, $map);
     }
     // Only attempt to decode the response, if the response code wasn't (204) 'no content'
     if ($code != '204') {
         if ($response->getExpectedRaw()) {
             return $body;
         }
         $decoded = json_decode($body, true);
         if ($decoded === null || $decoded === "") {
             $error = "Invalid json in service response: {$body}";
             if ($client) {
                 $client->getLogger()->error($error);
             }
             throw new Exception($error);
         }
         if ($response->getExpectedClass()) {
             $class = $response->getExpectedClass();
             $decoded = new $class($decoded);
         }
     }
     return $decoded;
 }
Example #3
0
 /**
  * Save the poster/cover photo to disk
  * @param string $path where to store the file
  * @param boolean $thumb get the thumbnail (100x140, default) or the
  *        bigger variant (400x600 - FALSE)
  * @return boolean success
  * @see IMDB page / (TitlePage)
  */
 public function savephoto($path, $thumb = true)
 {
     $photo_url = $this->photo($thumb);
     if (!$photo_url) {
         return false;
     }
     $req = new Request($photo_url, $this->config);
     $req->sendRequest();
     if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) {
         $fp = $req->getResponseBody();
     } else {
         $ctype = $req->getResponseHeader("Content-Type");
         $this->debug_scalar("<BR>*photoerror* at " . __FILE__ . " line " . __LINE__ . ": " . $photo_url . ": Content Type is '{$ctype}'<BR>");
         if (substr($ctype, 0, 4) == 'text') {
             $this->debug_scalar("Details: <PRE>" . $req->getResponseBody() . "</PRE>\n");
         }
         return false;
     }
     $fp2 = fopen($path, "w");
     if (!$fp || !$fp2) {
         $this->debug_scalar("image error at " . __FILE__ . " line " . __LINE__ . "...<BR>");
         return false;
     }
     fputs($fp2, $fp);
     return true;
 }
Example #4
0
 /** Retrieve trailers from azmovietrailers.com
  * @method getAZMovieTrailers
  * @param string url page url as retrieved with imdb::videosites
  * @return array [0..n] of array[url,format] of movie trailers (Flash)
  */
 function getAZMovieTrailers($url)
 {
     $req = new Request($url, $this->config);
     $req->sendRequest();
     $this->page = $req->getResponseBody();
     if ($this->page == "" || $this->page == false) {
         return false;
     }
     preg_match('|flashvars\\="file\\=(http.*)\\&|iUms', $this->page, $match);
     preg_match('|\\.(.{3})$|i', $match[1], $format);
     if (!empty($match[1])) {
         return array(array("url" => $match[1], "format" => $format[1]));
     }
 }
Example #5
0
 /**
  * Save the photo to disk
  * @param string path where to store the file
  * @param optional boolean thumb get the thumbnail (100x140, default) or the
  *        bigger variant (400x600 - FALSE)
  * @return boolean success
  * @see IMDB person page / (Main page)
  */
 public function savephoto($path, $thumb = TRUE, $rerun = FALSE)
 {
     $photo_url = $this->photo($thumb);
     if (!$photo_url) {
         return FALSE;
     }
     $req = new Request($photo_url, $this->config);
     $req->sendRequest();
     if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) {
         $fp = $req->getResponseBody();
     } else {
         if ($rerun) {
             $this->debug_scalar("<BR>*photoerror* at " . __FILE__ . " line " . __LINE__ . ": " . $photo_url . ": Content Type is '" . $req->getResponseHeader("Content-Type") . "'<BR>");
             return FALSE;
         } else {
             $this->debug_scalar("<BR>Initiate second run for photo '{$path}'<BR>");
             return $this->savephoto($path, $thumb, TRUE);
         }
     }
     $fp2 = fopen($path, "w");
     if (!$fp || !$fp2) {
         $this->debug_scalar("image error...<BR>");
         return false;
     }
     fputs($fp2, $fp);
     return TRUE;
 }