Back to blog home page

Using Backand with PHP

Posted by on Aug 19, 2015

PHP is one of the most common server-side programming languages, as evidenced by the popularity of WordPress and MediaWiki, for example.

Backand provides a Backend as a Service (BaaS) by automatically constructing a REST API for a database schema specified as a JSON. It is natural to interact with your Backand data from PHP through the Backand REST API.

Using Backand REST API With PHP

Using the Backand REST API is a two step process:

1. Obtain the authorization token
2. Make GET/POST calls to fetch/update/create/delete data

All calls follow the pattern demonstrated in the REST API Playground that is provided once you create a database in Backand.

Calls send JSON parameters and return JSON results.

Here we describe two ways to use Backand from PHP, using Curl and Httpful.

Using Curl

Obtaining the Authorization Token

To get the token, use your username, password and app name.
Make a ‘POST’ call to the ‘TOKEN_URL’.
The response is a JSON object with fields ‘access_token’ and ‘token_type’.

Now that we have the access token, we can make authenticated calls to the REST API.

Making API GET/POST Calls

API calls use the ‘REST_URL’.

In both types of calls you need to set the ‘Authorization’ and ‘AppName’.

For ‘GET’ calls, we add a query string to the url.

For ‘POST’ calls, we send body parameters as JSON (using ‘json_encode’).

Make GET Call

Now we will fetch the first first page of rows in items table.

The result has two fields, ‘totalRows’ giving the number of rows retrieved, and ‘data’ holding the retrieved rows.

We loop on the rows and print them:

Make POST Call

We create a new row in ‘items’ table by doing a POST on the ‘REST_URL’:

The result has a ‘__metadata’ field with the ‘id’ of the new row:

Using Httpful

Httpful is a simple, chainable, readable PHP library intended to make speaking HTTP sane.

To use Httpful, install Httpful using Composer.

By using this ‘composer.json’ file,

You now have execute from the command line in the project folder,

This will create a ‘vendor’ folder in your project folder.

In your PHP file include ‘bootstrap.php’ as:

Httpful calls follow the same pattern as Curl calls and use the same URLs. So we will just show the code here:

Obtaining the Authorization Token

Make GET Call

Make POST Call

Get a free hosted AngularJS backend with features such as user management, social signin, payment integration, security and more – GET STARTED NOW.

  • you can make annonymous calls like so: (I’m useing httpful)

    $app_name = ‘***’;
    $token_type = “anonymoustoken”;
    $access_token = “*****”; // Anonymous Token found in app config

    $response = HttpfulRequest::get(REST_URL . ‘/1/objects/objects/’. $ID)
    ->addHeader(‘Accept’, ‘application/json’)
    ->addHeader(‘Content-Type’,’application/json’)
    ->addHeader(‘anonymoustoken’, $access_token)
    ->addHeader(‘AppName’, $app_name)
    ->send();

  • Daniel Palazón

    Thanks for the article. It’s really useful. But, Is there any way to uploads images with php? Any example?