/** * Method wich tries to request an jrd document from the given url. Used * also in the webfinger class therefore it is here static * * @return PSX\Hostmeta\Document */ public static function requestJrd(Http $http, Url $url) { $request = new GetRequest($url, array('Accept' => 'application/jrd+json', 'User-Agent' => __CLASS__ . ' ' . Base::VERSION)); $request->setFollowLocation(true); $response = $http->request($request); if ($response->getStatusCode() == 200) { $contentType = $response->getHeader('Content-Type'); if (strpos($contentType, 'application/jrd+json') !== false || strpos($contentType, 'application/json') !== false) { $jrd = new Jrd(); $jrd->import(Json::decode($response->getBody())); return $jrd; } else { if (strpos($contentType, 'application/xrd+xml') !== false || strpos($contentType, 'application/xml') !== false) { $xrd = new Xrd(); $xrd->import(simplexml_load_string((string) $response->getBody())); return $xrd; } else { throw new Exception('Received unknown content type'); } } } else { throw new Exception('Invalid response code ' . $response->getStatusCode() . ' from ' . strval($url)); } }
/** * Returns hostmeta informations * * @httpMethod GET * @path / * @nickname getHostmeta * @responseClass PSX_Data_ResultSet */ public function getHostmeta() { try { $accept = $this->getHeader('Accept'); $format = $this->get->format('string'); if ($format == 'xml' || $accept == 'application/xrd+xml') { header('Content-Type: application/xrd+xml'); $document = new Xrd(); } else { header('Content-Type: application/jrd+json'); $document = new Jrd(); } $document->setSubject($this->config['psx_url']); $document->addProperty('http://ns.amun-project.org/2011/meta/title', $this->registry['core.title']); $document->addProperty('http://ns.amun-project.org/2011/meta/subTitle', $this->registry['core.sub_title']); $document->addProperty('http://ns.amun-project.org/2011/meta/timezone', $this->registry['core.default_timezone']->getName()); echo $document->export(); } catch (\Exception $e) { $code = isset(Http::$codes[$e->getCode()]) ? $e->getCode() : 500; $msg = new Message($e->getMessage() . $e->getTraceAsString(), false); $this->setResponse($msg, null, $code); } }
public function testImport() { $json = <<<JSON { "subject":"http://blog.example.com/article/id/314", "expires":"2010-01-30T09:30:00+00:00", "aliases":[ "http://blog.example.com/cool_new_thing", "http://blog.example.com/steve/article/7"], "properties":{ "http://blgx.example.net/ns/version":"1.3", "http://blgx.example.net/ns/ext":null }, "links":[ { "rel":"author", "type":"text/html", "href":"http://blog.example.com/author/steve", "titles":{ "default":"About the Author", "en-us":"Author Information" }, "properties":{ "http://example.com/role":"editor" } }, { "rel":"author", "href":"http://example.com/author/john", "titles":{ "default":"The other author" } }, { "rel":"copyright", "template":"http://example.com/copyright?id={uri}" } ] } JSON; $jrd = new Jrd(); $jrd->import(Json::decode($json)); $this->assertEquals('http://blog.example.com/article/id/314', $jrd->getSubject()); $this->assertEquals('Sat, 30 Jan 2010 09:30:00 +0000', $jrd->getExpires()->format('r')); $this->assertEquals(array('http://blog.example.com/cool_new_thing', 'http://blog.example.com/steve/article/7'), $jrd->getAliases()); $this->assertEquals(array('http://blgx.example.net/ns/version' => '1.3', 'http://blgx.example.net/ns/ext' => null), $jrd->getProperties()); $links = $jrd->getLinks(); $this->assertEquals('author', $links[0]->getRel()); $this->assertEquals('text/html', $links[0]->getType()); $this->assertEquals('http://blog.example.com/author/steve', $links[0]->getHref()); $this->assertEquals(array('default' => 'About the Author', 'en-us' => 'Author Information'), $links[0]->getTitles()); $this->assertEquals(array('http://example.com/role' => 'editor'), $links[0]->getProperties()); $this->assertEquals('author', $links[1]->getRel()); $this->assertEquals('http://example.com/author/john', $links[1]->getHref()); $this->assertEquals(array('default' => 'The other author'), $links[1]->getTitles()); $this->assertEquals('copyright', $links[2]->getRel()); $this->assertEquals('http://example.com/copyright?id={uri}', $links[2]->getTemplate()); $this->assertJsonStringEqualsJsonString($json, $jrd->export()); }