diff --git a/src/kSamsok.php b/src/kSamsok.php index 78330c5..3874539 100644 --- a/src/kSamsok.php +++ b/src/kSamsok.php @@ -1,14 +1,22 @@ key = $key; + $this->ugcKey = $ugcKey; // checks if API Key or request URL is bad(can also ) // check if URL does return a error $testQuery = $this->url . 'x-api=' . $this->key . '&method=search&query=text%3D"test"&recordSchema=presentation'; $this->validXml($testQuery); + + if ($this->ugcKey !== false) { + $testQuery = $this->url . 'x-api=' . $this->ugcKey . 'method=retrieve&scope=count&objectUri=all'; + $this->validXml($testQuery); + } } // Checks if valid xml is returned, if not throw Exception and kill the script @@ -27,6 +35,20 @@ protected function validXml($url) { } } + protected function validJson($url) { + try { + @$json = file_get_contents($url); + + if ($json === false || json_decode($json) === null) { + throw new Exception('Bad API request. (' . $url . ')'); + } + } catch(Exception $e) { + echo 'Caught Exception: ', $e->getMessage(), "\n"; + // these are fatal errors so kill the script + die(); + } + } + protected function prepareUrl($url) { // replace withe space $url = preg_replace('/\\s/', '%20', $url); @@ -133,7 +155,7 @@ protected function parseRecord($record) { } protected function idFormat($id, $format = 'raw') { - // $format can be string 'xml' or string 'raw' + // $format can be string 'xml'/string 'raw'/string 'uri' // if is the entire url strip it off if (stripos($id, 'http://kulturarvsdata.se/') !== false) { @@ -169,6 +191,14 @@ protected function idFormat($id, $format = 'raw') { return substr_replace($id, '/xml', $formatLocation, 0); } } + + if ($format === 'url') { + if (strpos($id,'http://kulturarvsdata.se/')) { + return $id; + } else { + return 'http://kulturarvsdata.se/' . $id; + } + } } public function search($text, $start, $hits, $images = false) { @@ -311,4 +341,30 @@ public function searchHint($string, $count = '5') { return false; } } + + public function ugcObject($objectId) { + $objectId = $this->idFormat($objectId, 'url'); + + $urlQuery = $this->ugcUrl . 'x-api=' . $this->ugcKey . '&method=retrieve&scope=all&objectUri=' . $objectId . '&format=json'; + $this->validJson($urlQuery); + + return file_get_contents($urlQuery); + } + + public function ugcCount($objectId) { + $objectId = $this->idFormat($objectId, 'url'); + + $urlQuery = $this->ugcUrl . 'x-api=' . $this->ugcKey . '&method=retrieve&scope=count&objectUri=' . $objectId . '&format=json'; + $this->validJson($urlQuery); + + return file_get_contents($urlQuery); + } + + public function ugcSingleRelation($contentId) { + + $urlQuery = $this->ugcUrl . 'x-api=' . $this->ugcKey . '&method=retrieve&objectUri=all&contentId=' . $contentId . '&scope=single&format=json'; + $this->validJson($urlQuery); + + return file_get_contents($urlQuery); + } } \ No newline at end of file