示例#1
0
 /**
  * @param string $url
  * @param string $representation
  * @throws \Exception
  * @return \Perry\Response
  */
 public function doGetRequest($url, $representation)
 {
     if ($data = CacheManager::getInstance()->load($url)) {
         return new Response($data['value'], $data['representation']);
     }
     $response = $this->guzzle->get($url, $this->getOpts($representation));
     $data = $response->getBody();
     $data = (string) $data;
     if ($response->hasHeader("Content-Type")) {
         if (false !== ($retrep = Tool::parseContentTypeToRepresentation($response->getHeader("Content-Type")))) {
             $representation = $retrep;
         }
     }
     CacheManager::getInstance()->save($url, ["representation" => $representation, "value" => $data]);
     return new Response($data, $representation);
 }
示例#2
0
 /**
  * @param string $url
  * @param string $representation
  * @throws \Exception
  * @return \Perry\Response
  */
 public function doGetRequest($url, $representation)
 {
     if ($data = CacheManager::getInstance()->load($url)) {
         return new Response($data['value'], $data['representation']);
     }
     $context = stream_context_create($this->getOpts($representation));
     if (false === ($data = @file_get_contents($url, false, $context))) {
         if (false === ($headers = @get_headers($url, 1))) {
             throw new \Exception("could not connect to api");
         }
         throw new \Exception("an error occured with the http request: " . $headers[0]);
     } else {
         $headers = @get_headers($url, 1);
         if (isset($headers['Content-Type'])) {
             if (false !== ($retrep = Tool::parseContentTypeToRepresentation($headers['Content-Type']))) {
                 $representation = $retrep;
             }
         }
     }
     CacheManager::getInstance()->save($url, ["representation" => $representation, "value" => $data]);
     return new Response($data, $representation);
 }
示例#3
0
 public function testParseContentTypeToRepresentation()
 {
     $this->assertEquals("vnd.ccp.eve.Killmail-v1", Tool::parseContentTypeToRepresentation('application/vnd.ccp.eve.Killmail-v1+json; charset=utf-8'));
     $this->assertFalse(Tool::parseContentTypeToRepresentation('fail'));
 }