示例#1
0
文件: Oembed.php 项目: k42b3/psx-ws
 /**
  * Requests the $url and tries to parse the response as oembed type. The url
  * must be pointing to an oembed provider i.e.:
  * http://flickr.com/services/oembed?url=http://www.flickr.com/photos/neilio/20403964/
  *
  * @param PSX\Url $url
  * @return PSX\Oembed\TypeAbstract
  */
 public function request(Url $url)
 {
     if (!$url->issetParam('url')) {
         throw new Exception('Required parameter url missing');
     }
     $format = $url->addParam('format', 'json');
     $request = new GetRequest($url, array('User-Agent' => __CLASS__ . ' ' . Base::VERSION, 'Accept' => 'application/json'));
     $response = $this->http->request($request);
     if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
         $source = function (array $data) {
             $type = isset($data['type']) ? strtolower($data['type']) : null;
             if (in_array($type, array('link', 'photo', 'rich', 'video'))) {
                 $class = 'PSX\\Oembed\\Type\\' . ucfirst($type);
                 if (class_exists($class)) {
                     return new $class();
                 } else {
                     throw new Exception('Class "' . $class . '" does not exist');
                 }
             } else {
                 throw new InvalidDataException('Invalid type');
             }
         };
         return $this->importer->import($source, $response, null, ReaderInterface::JSON);
     } else {
         throw new Exception('Invalid response code ' . $response->getStatusCode());
     }
 }