KSamsök-PHP
Looking for the official K-Samsök(SOCH) API documentation?
This documentation was last updated for KSamsök-PHP version 0.9.0
Introduction
KSamsök-PHP is a PHP library for the K-Samsök(SOCH) API. The K-Samsök aggregator has more then 6 million cultural objects indexed from various sources.
KSamsök-PHP is licensed under MIT and you can contribute to the project through Github.
Install
Composer:Run the following command in your project root:
composer require abbe98/ksamsok-php
PHP require()
If you are not able to use Composer you can simply require the file kSamsok.php
.
Usage
The API requires a API key that you can obtain by contacting the Swedish National Heritage Board. For development you can use the key test
.
Create a new instance of the KSamsok
class and include your API key.
- An optional parameter exists for setting custom K-Samsök endpoints.
Basic example.
$kSamsok = new KSamsok('test');
Usage example with custom K-Samsök endpoint.
$kSamsok = new KSamsok('test', 'https://example.com/endpoint');
Public Methods
Search
The basic search()
method has a total of four parameters:
text
(string), the string to search for in K-Samsök.start
(int/string), the result to start at, for returning the first result this should be set to1
.hits
(int/string), the number of results to return(1-500 is valid).image
(boal), optional parameter, set totrue
if you only want to return objects with images.
Searching for the string Fiskmås
returning 60 results starting at te first result:
$kSamsok->search('Fiskmås', 1, 60);
Searching for the string stockholm vattentorn
only returning results with images, starting at result 60 and getting 400 results:
$kSamsok->search('stockholm vattentorn', 60, 400, true);
Geo Search
The method geoSearch()
allows you to search by a geographical bounding box. geoSearch()
has six parameters:
west
(int/string), the most west longitude border of your bounding box.south
(int/string, the most south latitude border of your bounding box.east
(int/string), the most east longitude border of your bounding box.north
(int/string), the most north latitude border of your bounding box.start
(int/string), the result to start at, for returning the first result this should be set to1
.hits
(int/string), optional by default set to60
. The number of results to return(1-500 is valid).
Search by small bounding box in Sörmland, starting at the first result:
$west = '16.41'; $south = '59.07'; $east = '16.42'; $north = '59.08'; $kSamsok->geoSearch($west, $south, $east, $north, 1);
Search in the same bounding box as above but return 500 results starting at result 300:
$west = '16.41'; $south = '59.07'; $east = '16.42'; $north = '59.08'; $kSamsok->geoSearch($west, $south, $east, $north, 300, 500);
Search Hint
The searchHint()
method allows you to return search suggestions from a string. This method has two parameters:
text
(string), the string to get suggestions from.hits
(int/string), optional the number of suggestions to return,5
by default.
Get five suggestions for the string ny
:
$kSamsok->searchHint('ny');
Get three suggestions for the string ka
:
$kSamsok->searchHint('ka', 3);
Relations
The relations()
method allows you to return a list of object related to another. The method has only one parameter:
object uri
(string), a valid URI for a object in K-Samsök. The parameters allows different types of URIs described below.
Raw URLs/URIs such as:
http://kulturarvsdata.se/raa/kmb/16000300020896
raa/fmi/10028201230001
HTML, JSONLD, RDF, XML resource URLs/URIs:
http://kulturarvsdata.se/raa/kmb/xml/16000300020896
http://kulturarvsdata.se/raa/kmb/rdf/16000300020896
http://kulturarvsdata.se/raa/kmb/html/16000300020896
http://kulturarvsdata.se/raa/kmb/jsonld/16000300020896
raa/kmb/xml/16000300020896
raa/kmb/rdf/16000300020896
raa/kmb/html/16000300020896
raa/kmb/jsonld/16000300020896
Get all relations for the object raa/fmi/10028201230001
:
$kSamsok->relations('raa/fmi/10028201230001');
Object
The object()
method returns a object based on a URI or URL. The method has only one parameter:
object uri
(string), a valid URI for a object in K-Samsök. The parameters allows different types of URIs described below.
Raw URLs/URIs such as:
http://kulturarvsdata.se/raa/kmb/16000300020896
raa/fmi/10028201230001
HTML, JSONLD, RDF, XML resource URLs/URIs:
http://kulturarvsdata.se/raa/kmb/xml/16000300020896
http://kulturarvsdata.se/raa/kmb/rdf/16000300020896
http://kulturarvsdata.se/raa/kmb/html/16000300020896
http://kulturarvsdata.se/raa/kmb/jsonld/16000300020896
raa/kmb/xml/16000300020896
raa/kmb/rdf/16000300020896
raa/kmb/html/16000300020896
raa/kmb/jsonld/16000300020896
Return the object shm/18797
based on the URL to its XML source:
$kSamsok->object('http://kulturarvsdata.se/shm/site/xml/18797');
URI Format
TheuriFormat()
method can validate and convert SOCH URI/URL;s. uriFormat()
will return false
if provided with a invalid URI. uriFormat()
has three parameters:
uri
(string), the URI or URL to the object.format
(string), All supported output/input formats:raw
(default)rawurl
xml
xmlurl
jsonld
jsonldurl
rdf
rdfurl
html
htmlurl
validate
(bool), optional and defaults tofalse
.
Get the XML source URL from the raw URI shm/18797
:
$kSamsok->uriFormat('shm/site/18797', 'xmlurl');
Advanced Usage
Extending
When KSamsök-PHP does not have a method for a request you want to preform against the K-Samsök API extending KSamsök-PHP might be the solution. Extending KSamsök-PHP allows you to use KSamsök-PHP functions for formating URIs, validate responds, parsing and more, together with your own custom requests.
Note that this may require that you are familiar with The K-Samsök(SOCH) API.
Extending: Introduction
Extending KSamsök-PHP is done using PHP extended classes and the extends keyword. Extending K-Samsök does not support JSON-LD responses by default.
customKSamsok extends kSamsok { // your custom methods }
killXmlNamespace()
Because the KSamsök-PHP parsing function does not support XML Namespaces killXmlNamespace()
is used to over ride them.
Over ride the XML Namespaces in respond:
customKSamsok extends kSamsok { public function customMethod($url) { $xml = file_get_contents(utf8_decode($url)); if ($xml) { $xml = $this->killXmlNamespace($xml); } } }
parseRecord()
The parseRecord()
does parse a single record(SimpleXMLElement object) in XML format and returns it as a single PHP object.
Parse all returned records and push them to result array:
customKSamsok extends kSamsok { public function customMethod($url) { $xml = file_get_contents(utf8_decode($url)); if ($xml) { $xml = $this->killXmlNamespace($xml); $xml = new SimpleXMLElement($xml); $result[] = array(); foreach ($xml->records->record as $record) { $result[] = $this->parseRecord($record); } } } }
Extending: Example
The following example makes a text search for all results with images and then just returns the ones that does not have any coordinate data:
customKSamsok extends kSamsok { public function photoSearchNoCoords($text) { $url = $this->url . 'x-api=' . $this->key . '&method=search&hitsPerPage=250&query=itemType="Foto"%20and%20text="' . $text . '"%20and%20mediaType="image/jpeg"%20and%20thumbnailExists=j&recordSchema=presentation'; $xml = file_get_contents(utf8_decode($url)); if ($xml) { $xml = $this->killXmlNamespace($xml); $xml = new SimpleXMLElement($xml); $firstResults[] = array(); foreach ($xml->records->record as $record) { $firstResults[] = $this->parseRecord($record); } if (!empty($firstResults)) { for ($i=0; $i < count($firstResults); $i++) { if (empty($firstResults[$i]['presentation']['coordinates'])) { $results[] = $firstResults[$i]; } } } return $results; } else { return false; } } }