Skip to content

Commit

Permalink
Merge pull request #3 from BillOTei/master
Browse files Browse the repository at this point in the history
__construct change
  • Loading branch information
rtconner committed Nov 23, 2014
2 parents 8760a09 + bf6ebca commit 07201be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
51 changes: 13 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ PHP wrapper for the FreshBooks API. Simplifies FreshBooks API XML structure into

The XML tag parameters you see on the freshbooks API page are the ones you pass to $fb->post() (as an array)

Statically :
```php
$domain = 'your-subdomain'; // https://your-subdomain.freshbooks.com/
$token = '1234567890'; // your api token found in your account
Freshbooks\FreshBooksApi::init($domain, $token);
Freshbooks\FreshBooksApi::init($domain, $token);
```

Or using _construct and object instance:

```php
$domain = 'your-subdomain'; // https://your-subdomain.freshbooks.com/
$token = '1234567890'; // your api token found in your account
$fb = new Freshbooks\FreshBooksApi($domain, $token);
```

Example: list clients with an email of some@email.com

```php
// Method names are the same as found on the freshbooks API
// Statically
$fb = new Freshbooks\FreshBooksApi('client.list');
// Or
$fb->setMethod('client.list');

// For complete list of arguments see FreshBooks docs at http://developers.freshbooks.com
$fb->post(array(
Expand All @@ -46,43 +58,6 @@ if($fb->success()) {
}
```

If you're creating a recurring profile with multiple line items, it might look something like this:

```php
// Create a recurring profile with multiple line items
$fb = new Freshbooks\FreshBooksApi('recurring.create');
$fb->post(array(
'recurring' => array(
'client_id' => 41,
'lines' => array(
'line' => array(
array(
'name' => 'A prod name',
'description' => 'The description',
'unit_cost' => 10,
'quantity' => 2
),
array(
'name' => 'Another prod name',
'description' => 'The other description',
'unit_cost' => 20,
'quantity' => 1
)
)
)
)
));

var_dump($fb->getGeneratedXML()); // You can view what the XML looks like that we're about to send over the wire

$fb->request();

if($fb->success()) {
$res = $fb->getResponse();
var_dump($res['recurring_id']);
}
```

#### Credits

- Jordan Boesch - http://boedesign.com/
Expand Down
31 changes: 19 additions & 12 deletions src/Freshbooks/FreshBooksApi.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Freshbooks;
<?php

namespace Freshbooks;

/**
* A simple PHP API wrapper for the FreshBooks API.
Expand All @@ -17,11 +19,13 @@ class FreshBooksApi {
* The domain you need when making a request
*/
protected static $_domain = '';
protected $domain = '';

/*
* The API token you need when making a request
*/
protected static $_token = '';
protected $token = '';

/*
* The API url we're hitting. {{ DOMAIN }} will get replaced with $domain
Expand Down Expand Up @@ -68,15 +72,17 @@ public static function init($domain, $token)
self::$_token = $token;
}

/*
* Set up the request object and assign a method name
/**
* Initialize the and store the domain/token for making requests as init
* does instead of doing what setMethod does
*
* @param string $method The method name from the API, like 'client.update' etc
* @return null
* @param string $domain The subdomain like 'yoursite'.freshbooks.com
* @param string $token The token found in your account settings area
*/
public function __construct($method)
public function __construct($domain, $token)
{
$this->_method = $method;
$this->domain = $domain;
$this->token = $token;
}

/*
Expand Down Expand Up @@ -160,20 +166,21 @@ public function getGeneratedXML()
*/
public function request()
{

if(!self::$_domain || !self::$_token)
if(!$this->domain || !$this->token)
{
throw FreshBooksApiException('You need to call FreshBooksApi::init($domain, $token) with your domain and token.');
throw new FreshBooksApiException(
'You need to call new FreshBooksApi($domain, $token) or FreshBooksApi::init($domain, $token) with your domain and token.'
);
}

$post_data = $this->getGeneratedXML();
$url = str_replace('{{ DOMAIN }}', self::$_domain, $this->_api_url);
$url = str_replace('{{ DOMAIN }}', $this->domain, $this->_api_url);
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 40); // times out after 40s
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // add POST fields
curl_setopt($ch, CURLOPT_USERPWD, self::$_token . ':X');
curl_setopt($ch, CURLOPT_USERPWD, $this->token . ':X');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
Expand Down

0 comments on commit 07201be

Please sign in to comment.