Introduction
If you are using PHP – communicating with Fortnox is a piece of cake using cURL. In this example we have created one function to manage all our calls.
Before we get started you need to have you Access-Token and Client-Secret available. Didn’t get yours yet? Sign up as a Developer using this form.
You can also download the source code for these examples from our github repository: https://github.com/FortnoxAB/api-example-php. And feel free to contribute there by sending us code for other PHP HTTP libraries!
<?php define('ACCESS_TOKEN', 'your-access-token-here'); define('CLIENT_SECRET', 'your-client-secret-here'); define('CONTENT_TYPE', 'application/json'); define('ACCEPTS', 'application/json'); define('ENDPOINT', 'url-to-fortnox-api'); function apiCall ($requestMethod, $entity, $body = null) { $curl = curl_init(ENDPOINT . $entity); $options = array( 'Access-Token: '. ACCESS_TOKEN .'', 'Client-Secret: '. CLIENT_SECRET .'', 'Content-Type: '. CONTENT_TYPE .'', 'Accept: '. ACCEPTS .'' ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $options); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $requestMethod); if ($requestMethod == 'POST' || $requestMethod == 'PUT') { curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } $curlResponse = curl_exec($curl); curl_close($curl); return $curlResponse; }
Begin by adding your Access-Token, Client-Secret and the API Endpoint at the top of the file. The apiCall function expects the following arguments:
Name | Description | Required |
$requestMethod | The method of your choice, preferably POST, PUT, GET or DELETE | Yes |
$entity | The name of the requested entity e.g. articles or customers | Yes |
$body | The body of the PUT or POST call in whatever format of your call JSON or XML | No |
GET
To echo a list of all articles you do a GET by calling:
echo apiCall('GET', 'articles');
To get a specific article you append the ArticleNumber (1 below) to the entity:
echo apiCall('GET', 'articles/1');
To search for articles with ArticleNumber like 1, we add the articlenumber to the querystring of the entity.
echo apiCall('GET', 'articles/?articlenumber=1');
Check out the documentation for a full list of parameters.
POST
To create your favorite article you need to pass along data to your call in the body argument:
$body = '{"Article":{"Description":"My article description"}}'; echo apiCall('POST', 'articles', $body);
PUT
When updating an article you append the ArticleNumber (1 below) to the entity and pass along the data you intend to update:
$body = '{"Article":{"Description":"My updated article description"}}'; echo apiCall('PUT', 'articles/1', $body);
DELETE
To delete an article, you append the ArticleNumber (1 below) and do a DELETE call:
apiCall('DELETE', 'articles/1');
The same logic applies to all entities throughout the Fortnox API. Please note the lowercase formatting of all entity parameters. Good luck!
Good to know
When developing towards our Acceptance environment, the SSL certificates are causing the cURL request to return HTTP status code 0. You can ask cURL to skip the SSL checks by adding the following code during development only:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);