My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
UsageSnippets  

Featured, Example, Usage
Updated Mar 13, 2010 by rukavina...@gmail.com

At the beginning of a script always include proper initiation block

Initiation

//include particular file for entity you need (Client, Invoice, Category…)
include_once “library/FreshBooks/Client.php”;

//you API url and token obtained from freshbooks.com
$url = “your-url-please-replace”;
$token = “your-token-please-replace”;

//init singleton FreshBooks_HttpClient
FreshBooks_HttpClient::init($url,$token);

Get Client Data

//new Client object
$client = new FreshBooks_Client();

//try to get client with client_id 3
if(!$client->get(3)){
    //no data – read error
    echo $client->lastError;
}
else{
    //investigate populated data
    print_r($client);
}

New Client

//new Client object
$client = new FreshBooks_Client();

//populate client’s properties
$client->email = ‘test@test.com’;
$client->firstName = ‘Chad’;
$client->lastName = ‘Smith’;

//all other required properties should be populated

//try to create new client with provided data on FB server
if(!$client->create()){
    //read error
    echo $client->lastError;
}
else{
    //investigate populated data
    print_r($client);
}

New Invoice

//new Invoice object
$invoice= new FreshBooks_Invoice();
//populate data
$invoice->clientId = 123;
$invoice->date = ‘2009-06-03′; //mysql format

//all other required properties should be populated

//lines (items) is array of asoc. arrays
$invoice->lines = array(
    //1st item
    array(
        ‘name’=>’xyz’,
        ‘unitCost’=>99.99,
        //all other required properties should be populated
    ),
    //2nd item
    array(
        ‘name’=>’yyy’,
        ‘unitCost’=>199.99,
    )
);

//try to create new invoice with provided data on FB server
if(!$invoice->create()){
    //read error
    echo $invoice->lastError;
}
else{
    //investigate populated data
    print_r($invoice);
}

List/Search Projects

//create new Project object
$project = new FreshBooks_Project();

//listing function definition void   listing  ( &$rows,  &$resultInfo, [ $page = 1], [ $perPage = 25], [ $filters = array()])
//get projects, 25 per page, page 1, where project belong to client id 3
$project->listing($rows,$resultInfo,1,25,array('client_id'=>3));

//dump result data
print_r($rows);
print_r($resultInfo);
Comment by kou...@gmail.com, Oct 26, 2010

How do you protect creating more than one client or organization record?

Comment by chillfac...@gmail.com, Feb 8, 2011

In the final example there is an error - 'client_id' should be 'clientId'.

Comment by dogwal...@gmail.com, Jul 31, 2011

Need help using "listing" with client. Can i use $client->listing(); ???

Comment by supho...@gmail.com, Sep 12, 2011

Hi dogwal...@gmail.com,

Got the same question, and I had try Yes you can :)

$client->listing($rows,$resultInfo,1,25); print_r($rows); print_r($resultInfo);

Comment by skoot...@gmail.com, Jan 2, 2012

Trying to list all invoices for a particular client. I assumed it would be the same as listing projects but my code:

// Get and list invoices associated with this person (from FreshBooks)

// We need to get FreshBooksRequest-PHP-API for reading the details
// include particular file for entity you need (Client, Invoice, Category…)
include_once ('lib/FreshBooks/Invoice.php');
//your API url and token obtained from freshbooks.com
$url = <deleted>;
$token = <deleted>;
//init singleton FreshBooks_HttpClient
FreshBooks_HttpClient::init($url,$token);

        //new Invoice object
        $invoice = new FreshBooks_Invoice();

//listing function definition void   listing  ( &$rows,  &$resultInfo, [ $page = 1], [ $perPage = 25], [ $filters = array()])
//get invoices, 25 per page, page 1, where invoices belong to this client id
$invoice->listing($rows,$resultInfo,1,25,array('clientId'=>11));
print_r($rows);
print_r($resultInfo);
foreach ($rows as $key => $value) {
        $$key = $value;
}

only gives me

Array ( ) Array ( [page] => 1 [perPage] => 25 [pages] => 0 [total] => 0 )

Any ideas what I may be doing wrong?


Sign in to add a comment
Powered by Google Project Hosting