Developer Docs

Flybase PHP SDK

Build mobile and web apps in minutes using the Flybase REST API and PHP.

Overview

Flybase provides a complete backend solution for your application. Generally, our goal is to eliminate the need for managing servers and writing server-side code. Our PHP SDK is for those applications and situations where server-side code is necessary, or simply preferred.

The Flybase PHP SDK is fully open source, and anyone can contribute to make it better, or make their own changes if necessary. Check out the GitHub repository for more information.

The Flybase PHP SDK requires version 5.4 or greater of the PHP runtime.

Installation

Get Composer, the PHP package manager. Then create a composer.json file in your projects root folder, containing:


	{
		"require": {
			"flybase/flybase": "dev-master"
		}
	}
	

Run "composer install" to download the SDK and set up the autoloader, and then require it from your PHP script:


	require 'vendor/autoload.php';
		

Note: The Flybase PHP SDK requires PHP 5.4 or newer.

Getting Started

After including the required files from the SDK, you need to initialize the Flybase Client using your Flybase API key and app name:


	$flybase = new \Flybase\Client( "YOUR-API-KEY","YOUR-DATAMCFLY-APP" );
			

Selecting a collection is just a matter of referencing the collection name:


	$users = $flybase->Users; // where Users is your collection
			

Calls to $users will now only affect changes to your Flybase app's collection that is named Users

Using the PHP SDK in your code isn't that different from calling standard database systems, in fact, you can swap out a regular database system and just use Flybase for all of your data seamlessly.

Inserting Data

Adding data is accomplished by passing an array containing your document's data via the insert() function


	$inserted_id = $users->insert(array(
		'Name' => 'Test User',
		'PhoneNumber' => '123-456-1234',
		'DateCreated'  => date('Y-m-d H:i:s')
	));
			
Querying Data

Now, we can query our data to get all records:


	$ret = $users->get();
	var_dump( $ret );
			

We can also get a record based on a single _id:


	$ret = $users->get('544d8df66865edoc371206478');
	var_dump( $ret );
			

Or, you can query based on other fields:


	$ret = $users->find( array('PhoneNumber'=>'6486490392721') );
	var_dump( $ret );
			
Updating Data

We can also update our records easily:


	$result = $users->find( array('PhoneNumber'=>'6486490392721') );
	foreach( $result as $row ){
		$row['Role'] = 'Nobody';
		$ret = $users->updatebyid($row,$row['_id']);
	}
	
	$ret = $users->find( array('PhoneNumber'=>'6486490392721') );
	var_dump( $ret );
			

Or, you can update this way:


	$row = array(
		"Role"=>"Teacher",
	);
	$ret = $users->update(array("Name"=>"Lorenza Huels"),$row);
			
Deleting Data

Deleting records is a matter of calling the delete() function with a _id:


	$users->delete('544d8df63cffddoc853542530');
			
Triggering Events

Your apps can listen for custom events, and the PHP library can push notifications to these events:

		
	$users->trigger("custom_event","Hello!");