KSamsok PHP the Basics
25th January 2016K-Samsök also known as SOCH is an aggregator and API for Swedish culture heritage institutions, it’s developed by the Swedish National Heritage Board and in the time of writing it has 6,066,262 items indexed.
- 2,660,852 items has images
- 1,612,261 items has coordinates
- 628,582 media items has a public domain like license
- 2,929 items are maps
- and there is tons of linked data in there
Because content APIs that default to RDF sucks(I love linked data but still), messy URIs sucks and because K-Samsök got amazing content I wrote this API library.
Ksamsök-PHP links:
Installing
Installing K-Samsök PHP requries the Composer package manager:
composer require abbe98/ksamsok-php
Initialize
K-Samsök requires an API key, for messing around you can use test
. Create a new instance of the KSamsok
class and provide your API key:
$kSamsok = new KSamsok('test');
Making a Simple Text Search
Making a text based search is done using the the search method:
$kSamsok = new KSamsok('test');
$searchResultArray = $kSamsok->search('kanon', 1, 60);
Oh, parameters! The first one('kanon'
) is the string to search for, the second parameter defines where in the results you want to start retrieving items for example 1
means you want to start from the beginning of the search results. The last parameter in the example above tells the API how many items you want to retrieve(1-500 is valid).
The following example would result in a search for the string kyrka
starting at result 60 retrieving 60 results:
$kSamsok = new KSamsok('test');
$searchResultArray = $kSamsok->search('kyrka', 60, 60);
There is an optional parameter too:
$kSamsok = new KSamsok('test');
$searchResultArray = $kSamsok->search('kyrka', 60, 60, true);
This would simply make the something as the last example but only request items whit images, neat isn’t it?
Okay, that’s a basic text based search, lets move on.
Autocompleting Search
K-Samsök has a simple method for auto completing and it’s super simple to use with KSamsök-PHP:
$kSamsok = new KSamsok('test');
$searchHintObject = $kSamsok->searchHint('ka', 3);
The first searchHint()
parameter is the string to get auto completing suggestions from, the second parameter is optional and sets the number of suggestions to retrieve the default value is 5
. So now you can actually create a basic search engine.
Searching within a Bounding Box
Now lets check out the KSamsök-PHP method for making bounding box searches:
$west = '16.41';
$south = '59.07';
$east = '16.42';
$north = '59.08';
$kSamsok = new KSamsok('test');
$bBoxResultsArray = $kSamsok->geoSearch($west, $south, $east, $north, 300, 500);
The parameters should be quite straight forward the four first parameters define the lat/lon borders of the bounding box. The first integer parameter does define where in the results to start retrieving items the last parameter define how many results to retrieve(1-500 is valid).
The remaining public methods of KSamsök-PHP uses URIs as the main parameter so first let me introduce you to URIs in K-Samsök. Lets take a look at the following URI: raa/kmb/16000300020896
This is the most “raw” URI you can get in K-Samsök. First the data provider is defined and then there is a regular ID.
To retrieve the XML source of this item(from the API) you would guess there would be some parameter or extention such as raa/kmb/16000300020896.xml
or raa/kmb/16000300020896?format=xml
. This is actullay how it’s done: raa/kmb/xml/16000300020896
.
This results in the fact that items URIs and URLs is somewhat diverse:
raa/kmb/16000300020896
raa/kmb/xml/16000300020896
raa/kmb/rdf/16000300020896
raa/kmb/html/16000300020896
http://kulturarvsdata.se/raa/kmb/16000300020896
http://kulturarvsdata.se/raa/kmb/xml/16000300020896
http://kulturarvsdata.se/raa/kmb/rdf/16000300020896
http://kulturarvsdata.se/raa/kmb/html/16000300020896
Yep that’s the same item. In KSamsök-PHP it does not matter what URI or URL you provides it handles it for you out of the box!
Getting a Single Item
Lets return a single item based on its URI or URL:
$kSamsok = new KSamsok('test');
$itemObject = $kSamsok->object('http://kulturarvsdata.se/shm/site/xml/18797');
The following example would return the same result:
$kSamsok = new KSamsok('test');
$itemObject = $kSamsok->object('shm/site/18797');
Getting Items Relations
Returning all relations for a item is just as neat:
$kSamsok = new KSamsok('test');
$relationsObject = $kSamsok->relations('shm/site/18797');
Now we only have one public method left and it’s uriFormat()
. uritFormat()
convert K-Samsök URIs and URLs to any URI/URL type and even validate them:
$kSamsok = new KSamsok('test');
$uriString = $kSamsok->uriFormat('shm/site/18797', 'xmlurl');
This would result in the string http://kulturarvsdata.se/shm/site/xml/18797
being returned. The first parameter is simply the item URI or URL. The second parameter sets the type of URI or URL to be returned(see the documentation). By setting a third optional parameter to true
you can trigger validation of the URI(returns false
if invalid).
That’s the basic usage of KSamsök-PHP in part two we will extend its features using its protected methods to be able to take advantage of all K-Samsöks powerful native methods.
Previous posts about K-Samsök and heritage data: