This is a PHP library that makes it easier to use the Roomstyler RESTful API. It is intended to be a wrapper for the API so that you, as user of the API will never have to write your own HTTP requests but instead can simply call a method to do it for you., (*1)
Based on the roomstyler RESTful API, should anything be/remain unclear after reading the docs, check the documentation on there as well to see if it is more clear.
Also, this is not a 1-on-1 copy of the API, some method calls are grouped (render
for instance can do panorama
, hq
and 2d
aside from the default 3d
) or have a slightly more convenient way of adding parameters., (*2)
Clone or download
buttonDownload ZIP
.zip
and move or copy it to the root of the projectroomstyler-api-php-master/api/RoomstylerApi.php
in your application~ $ cd project-root project-root $ git clone git@github.com:floorplanner/roomstyler-api-php.git
This command clones the repository to project-root/roomstyler-api-php/
, to require it add:, (*3)
~ $ cd composer-project-root composer-project-root $ composer require floorplanner/roomstyler-api-php
When you want to read public data, (*4)
For personal use, read global data or perform actions on rooms as the signed in user (whitelabel users can also login), (*5)
['name' => 'myusername', 'password' => 'mypassword']]); ?>
For when you want to read global data and read, write or modify your own whitelabel data, (*6)
['name' => 'mywhitelabel', 'password' => 'mywhitelabelpassword']]); ?>
For the those who want to maximize their integration potential, this allows you to read and write and modify data of your whitelabel and your own rooms and perform actions on rooms as the signed in user, (*7)
['name' => 'myusername', 'password' => 'mypassword'], 'whitelabel' => ['name' => 'mywhitelabel', 'password' => 'mywhitelabelpassword']]); ?>
After doing this setup you should probably run a simple test to check if you can actually get a response back from a call., (*8)
<?php # print the 5 latest rooms echo '<pre>'; print_r($rsapi->rooms->index(['limit' => 5])); echo '</pre>'; ?>
We just talked about the user
and whitelabel
options that can be passed to the constructor of the RoomstylerApi
class but there are more options:, (*9)
https
[]
[]
roomstyler.com
api
rooms/10
=> api/rooms/10
NULL
user
option this property will be set to the server generated token5
en
en
, fr
, de
, es
, nl
30
['Content-Type: application/json; charset=utf-8']
POST
requestsfalse
result
and request_info
which can be used to view the requestEverything is already setup to work with the API so you barely have to change these settings.
The option you'll most likely be using is debug
which allows you to take a peek into the request., (*10)
This is a general overview of the structure of the core objects behind the API, I will try to explain what you can do and when you can do it as best as I can. The API is OOP only, which means you're going to have to apply a little bit of PHP's OOP but don't worry, it'll be easy!, (*11)
Starting with the RoomstylerApi
class, this is the base for the entire API.
It contains the settings and defaults that we've already discussed here, (*12)
It also handles calls like this that you will be using:, (*13)
#call to a new instance of "RoomstylerRoomMethods" $api->rooms; #call to a new instance of "RoomstylerComponentMethods" $api->components;
If you're someone who has done OOP for some time and are familiar with PHP's magic methods
and more specifically the __get
magic method you'll know exactly what I'm talking about., (*14)
When you call something like ->components
on the $api
(Could be any variable name, must be an instance of the RoomstylerApi
class)
the $api
will look up a class with the name of RoomstylerComponentMethods
., (*15)
It does this by first converting whatever property you're trying to call to its singular form so components
becomes component
, then converting the first character to uppercase so component
becomes Component
and last but not least it prepends Roomstyler
and appends Methods
so that the final result becomes RoomstylerComponentMethods
., (*16)
If you're already using the singular form of a word, e.g. component
then the step to convert will do nothing and it will still uppercase the first character and prepend Roomstyler
and append Methods
, (*17)
This RoomstylerComponentMethods
class extends RoomstylerMethodBase
and allows you to call the documented component aggregation methods., (*18)
Essentially this means that you can call any property and get either an instance of a class back if it exists and is included, or a Fatal Error: Class 'Roomstyler[NonExistentClass]Methods' not found in...
., (*19)
This is the base class behind the scenes that allows you to use any and all of the RoomstylerApi->_settings
within an instance of Roomstyler[...]Methods
It's purpose is to provide a standard interface for actions you execute to get a dataset., (*20)
This is the base class behind the returned results from the requests. You use the methods in Roomstyler[...]Methods
to get a set of results (through some find()
, index()
or search()
action)
after which you get a single object or an array of objects back which you can then manipulate., (*21)
This base class is actually more useful than the RoomstylerMethodBase
since this one does the same and more, it also dynamically populates itself with properties returned from the API., (*22)
To get an idea of what I'm talking about, consider this json
response from api/users/972691
it should look something like this:, (*23)
{ "id": 972691, "username": "Sidney Liebrand", "role": "admin", "bio": "I'm a 21 year old web developer from the Netherlands, born, raised and still living in the always magnificent Dinteloord.", "avatar": "https://d2sdvaauesfb7j.cloudfront.net/avatars/972691-1434979777.jpg", "background": "https://d2sdvaauesfb7j.cloudfront.net/img/empty.jpeg" }
And compare it to the return object that would look like this after a successful request and being __construct
ed, (*24)
$CONFIG['user_credentials']]); $user = $api->user->find(972691); echo ''; print_r($user); # => RoomstylerUser Object ( [_http_status:RoomstylerModelBase:private] => 200 [_accessible_props:RoomstylerModelBase:private] => ['errors'] [errors:RoomstylerModelBase:private] => RoomstylerError Object ( ) [_settings:protected] => Array ( [protocol] => https [whitelabel] => Array ( ) [user] => Array ( [name] => mysignin@email.com [password] => mysignin_password ) [host] => roomstyler.com [prefix] => api [token] => 64f97c9ee52df2735fsample-tokene6e252d58837d41b05cd [timeout] => 5 [language] => en [connect_timeout] => 30 [request_headers] => Array ( [0] => Content-Type: application/json; charset=utf-8 ) [debug] => [user_agent] => RoomstylerApi/1.0 Type/normal (https://roomstyler.com) ) [_whitelabeled:protected] => [id] => 972691 [username] => Sidney Liebrand [role] => admin [bio] => I'm a 21 year old web developer from the Netherlands, born, raised and still living in the always magnificent Dinteloord. [avatar] => https://d2sdvaauesfb7j.cloudfront.net/avatars/972691-1434979777.jpg [background] => https://d2sdvaauesfb7j.cloudfront.net/img/empty.jpeg ) ?>Now all the properties that start with an underscore (
_
) are also either:private
or:protected
which means we can't access them. If you try accessing this freshly fetched users_whitelabel
property$user->_whitelabel
it would simply returnNotice: Undefined property: RoomstylerUser::$_whitelabel
, (*25)If you tried to access the public (and dynamically populated)
id
on the other hand, you would get eitherNULL
or it's value if it's set. The same goes for all other properties. Normally you would get a notice if you call a property that does not exist on an object ($user->non_existent_prop
):Notice: Undefined property: RoomstylerUser::$non_existent_prop
but since the fields are subject to change this would mean that you could get randomNotice
errors for no reason., (*26)Because of this, all properties that do not exist or aren't public (except
errors
which is made public through__get
) will returnNULL
., (*27)Methods
The
RoomstylerModelBase
class also provides us with some other methods we can use to see wether the object actuallyexists()
(not just an empty object - but actually having properties), or if the object in question has anyerrors
, (*28)This is done (using our
$user
initiated on top) by calling$user->errors
which will return aRoomstylerError
object or$user->exists()
to check if any property is set at all., (*29)Errors
Every object returned from the API will have an accessible
errors
property that has a few methods. To check wether any of the objects contains any error (including http errors), (*30)the maximum limit of the index call is 50. if this is exceeded the server will return a JSON error and an Unprocessable Entity (422) http error, (*31)
$response = $rsapi->rooms->index(['limit' => 100]); # full response # since the API call errored, a single object is returned instead of an array of objects. print_r($response); # if this didn't happen, you'd have to call print_r($response[0]); # to see the errors (which are always bound to every returned object)To access these errors, call the
errors
property on a single entity. It will return an instance of theRoomstylerError
class., (*32)$response->errors # => RoomstylerError{}RoomstylerError
The
RoomstylerError
class has three methods, one toget()
all the errors. One to see if there areany()
errors and a function that loops througheach()
error., (*33)They will be explained using this example request., (*34)
(This request will error out since the max. limit for the
index()
function is50
), (*35)$response = $rsapi->rooms->index(['limit' => 100]);Check if there are errors
The
any()
function will returntrue
if any errors including any http errors occured., (*36)$response->errors->any(); # => trueGet all errors
The
get()
function will return an array of errors including http errors. If there are no errors, an empty array is returned., (*37)The array returned is associative and will contain numeric and string keys. As you might be able to see below, the errors have different depths making it hard to property loop the errors. The solution for this is the builtin
each()
method which is explained later., (*38)$response->errors->get(); # => [0 => 'Some error', 'assoc' => 'too', 'user' => ['nested' => ['errors', 'for a single entity']]]Loop all the errors
If there are errors, you can loop these through the
each()
function. It takes one parameter, which is a closure or callable function that itself takes one (optionally two) parameter(s) The first parameter contains the error message of an error and the second parameter will contain it's parent keys if it was a nested hash., (*39)As we know the above
$response
example gives us two errors, one through a nested json object with anerror
key which contains the error. And a http error since we're requesting too much data., (*40)If we'd print the
get()
function with this example, the errors array would look like this:, (*41)# => [ 'error' => 'Limit should be between 1 and 50', 0 => 'Unprocessable entity' ]Knowing the structure of the errors, if we build something like this:, (*42)
$response->errors->each(function($error, $labels) { if ($labels) $label = join('.', $labels); else $label = 'default_label'; print_r($label": $error"); });The output will look like this:, (*43)
error: Limit should be between 1 and 50 default_label: Limit should be between 1 and 50If the
error
key wasn't an error but instead another hash with say aquantity
key containing the same error, the output for the same request would look like:, (*44)error.quantity: Limit should be between 1 and 50 default_label: Limit should be between 1 and 50API endpoints
Rooms
Aggregation
Getting search meta data
PHP snippet, (*45)
<?php print_r($rsapi->rooms->search_meta()); # => RoomstylerSearchMeta{} ?>Method signature, (*46)
RoomstylerRoomMethods->search_meta();Parameters, (*47)
PHP snippet, (*48)
<?php print_r($rsapi->rooms->index()); # => [RoomstylerRoom{}, RoomstylerRoom{}, ...] ?>
Method signature, (*49)
RoomstylerRoomMethods->index($params = []);
Parameters, (*50)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
limit
- Optional (Default 50
) - A number between (and including) 1 and 50page
- Optional (Default 1
) - A number that defines the page you're on (useful for pagination)category
- Optional (see RoomstylerSearchMeta
) - Filters results within specified categorylast_updated
- Optional - List rooms updated after a given timestampcreated
- Optional - List rooms created after a given timestampskip_last_updated
- Optional (Recommended, Default true
) - skips fetching last updated room, significantly speeds up requestsskip_total
- Optional (Recommended, Default true
) - skips fetching a count of all rooms, significantly speeds up requestsorder
- Optional - Order results based on a room attribute (see a RoomstylerRoom
object for a list of properties)direction
- Required if order
specified - either asc
or desc
user_id
- Optional - fetch rooms owned by this user (requires user access)whitelabel
- Optional - fetch rooms owned by your whitelabel (requires whitelabel access)tag
- Optional - Filter rooms by given tagThis method accepts the same parameters as the non-scoped index
method! The only difference is that the optional whitelabel
parameter is set to the whitelabel user for you, (*51)
PHP snippet, (*52)
<?php # requires whitelabel access print_r($rsapi->wl->rooms->index()); # => [RoomstylerRoom{}, RoomstylerRoom{}, ...] ?>
Method signature and parameters: see Fetching Rooms, (*53)
PHP snippet, (*54)
<?php print_r($rsapi->rooms->find(123456)); # => RoomstylerRoom{} ?>
Method signature, (*55)
RoomstylerRoomMethods->find($id);
Parameters, (*56)
$id
- The id of the room to fetchPHP snippet, (*57)
<?php print_r($rsapi->rooms->search(['q' => 'test'])); # => [RoomstylerRoom{}, RoomstylerRoom{}, ...] ?>
Method signature, (*58)
RoomstylerRoomMethods->search($params = []);
Parameters, (*59)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
q
- Required - A search stringlimit
- Optional (Default 50
) - A number between (and including) 1 and 50page
- Optional (Default 1
) - A number that defines the page you're on (useful for pagination)since
- Optional (see RoomstylerSearchMeta
) - Filters results within specified timeframecategory
- Optional (see RoomstylerSearchMeta
) - Filters results within specified categorystyle
- Optional (see RoomstylerSearchMeta
) - Filters results within specified stylekind
- Optional - If it has the value of own
it will search through the logged in users rooms (requires user access)PHP snippet, (*60)
<?php print_r($rsapi->rooms->panoramas()); # => [RoomstylerRoom{}, RoomstylerRoom{}, ...] ?>
Method signature, (*61)
RoomstylerRoomMethods->panoramas($params = ['limit' => 50, 'page' => 1]);
Parameters, (*62)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
limit
- Optional (Default 50
) - A number between (and including) 1 and 50page
- Optional (Default 1
) - A number that defines the page you're on (useful for pagination)since
- Optional (see RoomstylerSearchMeta
) - Filters results within specified timeframeskip_total
- Optional - skips counting of panorama's, speeds up request slightly if true
Lets say Let's initialize a $room
variable and use that in the following requests like so:, (*63)
<?php $room = $rsapi->rooms->find(123456); ?>
PHP snippet, (*64)
<?php print_r($room->panorama()); # => RoomstylerRoomPanorama{} ?>
Method signature, (*65)
RoomstylerRoom->panorama($params = []);
Parameters, (*66)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
krpano_url
- When supplied, adds a krpano_url property containing a template url to use for rendering a cube image in krpano.PHP snippet, (*67)
<?php # requires user access print_r($room->comment('My awesome comment!')); # => RoomstylerComment{} ?>
Method signature, (*68)
RoomstylerRoom->comment($content);
Parameters, (*69)
$content
- The comment text to be placed on the roomPHP snippet, (*70)
<?php # requires user access print_r($room->toggle_love()); # => RoomstylerRoom{} ?>
Method signature, (*71)
RoomstylerRoom->toggle_love();
Parameters, (*72)
PHP snippet, (*73)
<?php # requires whitelabel access print_r($room->chown(972691)); # => RoomstylerComment{} ?>
Method signature, (*74)
RoomstylerRoom->chown($user_id);
Parameters, (*75)
$user_id
- The target user that will be the new owner of the subject room (should be a user(id) of your whitelabel)PHP snippet, (*76)
delete()); # => RoomstylerRoom{} ?>
Method signature, (*77)
RoomstylerRoom->delete();
Parameters, (*78)
PHP snippet, (*79)
<?php print_r($room->products()); # => [RoomstylerProduct{}, RoomstylerProduct{}, ...] ?>
Method signature, (*80)
RoomstylerRoom->products();
Parameters, (*81)
PHP snippet, (*82)
<?php print_r($room->loved_by()); # => [RoomstylerUser{}, RoomstylerUser{}, ...] ?>
Method signature, (*83)
RoomstylerRoom->loved_by();
Parameters, (*84)
PHP snippet, (*85)
<?php print_r($room->related_rooms()); # => [RoomstylerRoom{}, RoomstylerRoom{}, ...] ?>
Method signature, (*86)
RoomstylerRoom->related_rooms();
Parameters, (*87)
PHP snippet, (*88)
<?php print_r($room->comments()); # => [RoomstylerComment{}, RoomstylerComment{}, ...] ?>
Method signature, (*89)
RoomstylerRoom->comment();
Parameters, (*90)
PHP snippet, (*91)
<?php print_r($room->add_tags(['first-tag', 'second-tag'])); # => RoomstylerRoom{} ?>
OR, (*92)
<?php print_r($room->add_tags('first-tag,second-tag')); # => RoomstylerRoom{} ?>
Method signature, (*93)
RoomstylerRoom->add_tags($tags)
Parameters, (*94)
$tags
- Required - An array of individual tags or a string of comma-seperated tagsPHP snippet, (*95)
<?php print_r($room->remove_tags(['first-tag', 'second-tag'])); # => RoomstylerRoom{} ?>
OR, (*96)
<?php print_r($room->remove_tags('first-tag,second-tag')); # => RoomstylerRoom{} ?>
Method signature, (*97)
RoomstylerRoom->remove_tags($tags)
Parameters, (*98)
$tags
- Required - An array of individual tags or a string of comma-seperated tagsPHP snippet, (*99)
<?php print_r($room->render()); # => RoomstylerRoom{} ?>
Method signature, (*100)
RoomstylerRoom->render($mode = '', $params = [])
Parameters, (*101)
$mode
- Optional (Either 2d
, panorama
or hq
, any other strings will be ignored) - Specify rendering method, if left empty it will render in 3D, otherwise it will render either 2d
, panorama
or hq
$params
- An array containing any the following keys:
width
- Optional (Default value of 960
for normal and 2d
renders, 1920
for hq
and ignored for panorama
) - Width at which to render roomheight
- Optional (Default value of 540
for normal and 2d
renders, 1920
for hq
and ignored for panorama
) - Height at which to render roomsize
- Optional (Default value of 1080
for panorama, ignored for the rest) - Size at which to render cube images for panoramacallback
- Optional (Required if $mode
is set to 2d
.) - A callback url that will receive a POST
request when rendering is donePHP snippet, (*102)
<?php print_r($api->users->find(972691)); # => RoomstylerUser{} ?>
OR, (*103)
<?php print_r($api->users->find([972691, 972691])); # => [RoomstylerUser{}, RoomstylerUser{}, ...] ?>
OR, (*104)
<?php print_r($api->users->find('972691, 972691')); # => [RoomstylerUser{}, RoomstylerUser{}, ...] ?>
Method signature, (*105)
RoomstylerUserMethods->find($ids)
Parameters, (*106)
$ids
- Required - The id
of a user, an array of id
s or a string of comma seperated id
sPHP snippet, (*107)
<?php print_r($api->users->create(['email' => 'my-email@provider.com', 'username' => 'myusername', 'password' => 'mypassword'])); # => RoomstylerUser{} ?>
Method signature, (*108)
RoomstylerUserMethods->create($params = [])
Parameters, (*109)
$params
- Required
email
- Required - Email we want to use for this accountusername
- Requiredpassword
- RequiredIf you read over the user access setup section I showed an example of logging in as a user within the constructor
of the object.
It is however, also possible to login seperately like this, if You didn't login before and call this function manually later, all requests from then on will have
user access., (*110)
This function also returns the token needed to use in other requests such as to comment or love a room., (*111)
Also, if you're already logged in you do not need to use this function., (*112)
PHP snippet, (*113)
<?php print_r($api->users->login('my-email@provider.com', 'mypassword')); # => RoomstylerUser{} ?>
Method signature, (*114)
RoomstylerUserMethods->login($email, $password)
Parameters, (*115)
$email
- Required - Email to use$password
- Required - Password for the accountLet's initialize a $user
variable and use that in the following requests like so:, (*116)
<?php $user = $rsapi->users->find(972691); ?>
Deletes a given user, (*117)
PHP snippet, (*118)
<?php print_r($user->delete()); # => RoomstylerUser{} ?>
Method signature, (*119)
RoomstylerUser->delete()
Parameters, (*120)
PHP snippet, (*121)
<?php print_r($user->loved_rooms()); # => [RoomstylerRoom{}, RoomstylerRoom{}, ...] ?>
Method signature, (*122)
RoomstylerUser->loved_rooms($params = [])
Parameters, (*123)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
page
- Optional - The page on which you're on (sets query offset to (page - 1) * per_page
)per_page
- Optional - The amount of results to display on a pageskip_total
- Optional (Default true
) - skips counting results, speeds up query slightlyPHP snippet, (*124)
<?php print_r($user->collections()); # => [RoomstylerCollection{}, RoomstylerCollection{}, ...] ?>
Method signature, (*125)
RoomstylerUser->collections()
Parameters, (*126)
PHP snippet, (*127)
<?php print_r($user->collection(44)); # => RoomstylerCollection{} ?>
Method signature, (*128)
RoomstylerUser->collection($id)
Parameters, (*129)
$id
- Required - which of the users' collections to fetchPHP snippet, (*130)
<?php print_r($api->contests->index()); # => [RoomstylerContest{}, RoomstylerContest{}, ...] ?>
Method signature, (*131)
RoomstylerContestMethods->index($params = [])
Parameters, (*132)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
per_page
- Optional (Default 25
) - A number between (and including) 1 and 50page
- Optional (Default 1
) - A number that defines the page you're on (useful for pagination)status
- Optional - Either "finished"
, "open"
or "vote"
title
- Optional - Return only contests where given string is contained within their title
PHP snippet, (*133)
<?php print_r($api->contests->find(1317)); # => RoomstylerContest{} ?>
Method signature, (*134)
RoomstylerContestMethods->find($id)
Parameters, (*135)
$id
- Required - the id
of the contest to fetchLet's initialize a $contest
variable and use that in the following requests like so:, (*136)
<?php $contest = $rsapi->contests->find(1317); ?>
PHP snippet, (*137)
<?php print_r($contest->entries()); # => [RoomstylerContestEntry{}, RoomstylerContestEntry{}, ...] ?>
Method signature, (*138)
RoomstylerContest->entries($params = [])
Parameters, (*139)
$params
- Optional (Defaults do get set) - An array containing any the following keys:
per_page
- Optional (Default 25
) - A number between (and including) 1 and 50page
- Optional (Default 1
) - A number that defines the page you're on (useful for pagination)order
- Optional - Attribute to order by and the direction to order byrand_seed
- Optional - If supplied, entries will be returned psuedo-random based on the seed (must be an integer)Let's initialize a $contest_entry
variable and use that in the following requests like so:, (*140)
<?php $contest_entry = $rsapi->contests->find(1317)->entries()[0]; ?>
PHP snippet, (*141)
<?php # requires user access print_r($contest_entry->vote()); # => RoomstylerVote{} ?>
Method signature, (*142)
RoomstylerContestEntry->vote()
Parameters, (*143)
PHP snippet, (*144)
<?php print_r($api->materials->find(3360)); # => RoomstylerMaterial{} ?>
Method signature, (*145)
RoomstylerMaterialMethods->find($id)
Parameters, (*146)
$id
- Required - the id
of the material item to fetchPHP snippet, (*147)
<?php print_r($api->components->find('7b7e830978663ca44cafe62f095ee5f05af7670b')); # => RoomstylerComponent{} ?>
Method signature, (*148)
RoomstylerComponentMethods->find($id)
Parameters, (*149)
$id
- Required - the id
of the component item to fetchPHP snippet, (*150)
<?php print_r($api->categories->index()); # => [RoomstylerCategory{}, RoomstylerCategory{}, ...] ?>
Method signature, (*151)
RoomstylerCategoryMethods->index()
Parameters, (*152)
PHP snippet, (*153)
<?php print_r($api->editor->embed()); # => <iframe...> ?>
Method signature, (*154)
RoomstylerEditor->embed($opts = [], $html_opts = [])
Parameters, (*155)
$opts
- Optional (Defaults do get set) - An array containing any the following keys:
room_url
- Optional - Opens a room_url
(returned from the RoomstylerRoom->url
property)token
- Optional - Log in a user through a tokenlanguage
- Optional - Set the language for the editor to use, en
, es
, nl
, fr
and de
login
- Optional - if false, prevents all logins$html_opts
- Optional (Defaults do get set) - An array consisting of valid html attribute => value pairs
frameborder
- Optional (Default 0
) - HTML prop to hide the border around the editorwidth
- Optional (Default 1024
) - Width of the editor iframeheight
- Optional (Default 768
) - Height of the editor iframe