Skip to content
This repository was archived by the owner on Feb 26, 2018. It is now read-only.

Examples

pluijm edited this page Jul 27, 2012 · 48 revisions

Technical documentation can be found in the code and the generated docs in the docs folder. If you have any questions or find a bug, please contact me on twitter (@sjorsvdp) or create a new issue.

Setting up
First, we need a connection with your Moneybird accout. You’re advised to create a new user for the API connection. Moneybird also supports oAuth (see below).

An autoloader is included if you need it:

<?php
spl_autoload_register('Moneybird\ApiConnector::autoload');
?>

To connect with Moneybird, we need to create the ApiConnector object:

<?php
// Create a Transport
$transport = new Moneybird\HttpClient();
$transport->setAuth(
	$config['username'], // put your username here
	$config['password']  // put your password here
);
$connector = new Moneybird\ApiConnector(
	$config['clientname'], // put your client name here (<...>.moneybird.nl)
	$transport, 
	new Moneybird\XmlMapper() // create a mapper
);
?>

That’s it! If you don’t get an exception thrown at you, you’ve done the right thing. If you do get an exception, you probably filled in the wrong credentials or client name.

The basics – services
To get, save or delete contacts, recurring templates, invoices, etc, you need to get their service first:

<?php
$contactService = $connector->getService('Contact');
$invoiceService = $connector->getService('Invoice');
?>

The basics – getAll()
To get all contacts, simply call getAll() on the service:

<?php
$contacts = $contactService->getAll();
foreach ($contacts as $contact) {
	echo $contact->name . '<br />';
}
?>

Some services allow a filter for getAll().

<?php
// Get all invoices
$invoices = $invoicService->getAll();
// Get all invoices draft invoices
// A list of available filters is defined in ApiConnector::$filters 
$invoices = $invoicService->getAll('draft);
// Get all invoices that match your saved advanced filter with id 12345
// Warning: if the filter is not found, all invoices will be returned!
$invoices = $invoicService->getAll(12345);
// Get all late invoices of a contact
$invoices = $contact->getInvoices($invoiceService, 'late');
?>

The basics – getById()
To get a contact by it’s id, use getById():

<?php
$contact = $contactService->getById(12345);
?>

Note that the id of a contact is not it’s customerId. To get a customer by it’s customerId, use Contact_Service::getByCustomerId(). Use Invoice_Service::getByInvoiceId() to get an invoice by it’s number.

oAuth
To use the library to connect through oAuth you need a token and token secret. See the oAuth website for information about oAuth. How oAuth works in Zend Framework (with good explanation) can be found here.

Using oAuth is just a matter of using another Transport.

<?php
$transport = new Moneybird\HttpClient_Oauth();
$consumer = new Moneybird\Lib\OAuthConsumer(
	$config['oauth']['consumerKey'], 
	$config['oauth']['consumerSecret'], 
	NULL
);
$token = new Moneybird\Lib\OAuthConsumer(
	$config['oauth']['token'], 
	$config['oauth']['tokenSecret']
);
$transport->setConsumerAndToken($consumer, $token);
$connector = new Moneybird\ApiConnector(
	$config['clientname'], // put your client name here (<...>.moneybird.nl)
	$transport, 
	new Moneybird\XmlMapper() // create a mapper
);
?>

Requests limit
Moneybird limits the number of requests (350/h). You can find out how many requests you have left using Moneybird\ApiConnector::requestsLeft().

<?php
echo 'Requests left: ' . $connector->requestsLeft();
?>

Clone this wiki locally