Skip to content

Commit

Permalink
Merge pull request #53 from infusionsoft/feature/rest
Browse files Browse the repository at this point in the history
Adds REST services
  • Loading branch information
MicFai committed Nov 30, 2015
2 parents 7d1ebec + d34190a commit 9716cb7
Show file tree
Hide file tree
Showing 28 changed files with 2,345 additions and 345 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
language: php

sudo: false

php:
- 5.4
- 5.5
- 5.6
- hhvm
- 7

before_script:
- composer self-update
Expand All @@ -15,4 +17,5 @@ script: phpunit
matrix:
allow_failures:
- php: hhvm
- php: 7
fast_finish: true
130 changes: 122 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Infusionsoft PHP SDK (beta)
# Infusionsoft PHP SDK

[![Build Status](https://travis-ci.org/infusionsoft/infusionsoft-php.png?branch=master)](https://travis-ci.org/infusionsoft/infusionsoft-php)
[![Total Downloads](https://poser.pugx.org/infusionsoft/php-sdk/downloads.png)](https://packagist.org/packages/infusionsoft/php-sdk)
Expand All @@ -7,7 +7,13 @@

## Version Notes

This version implements a newer version of the xml-rpc handling library. As such, the minimum PHP version has been increased to >= 5.4
This version implements RESTful endpoints, a new version of Guzzle, and a restructured request handler.

### Breaking Change

If you use the `Orders` or `Products` services, there are now two different classes handling each service - one for REST, one for XML-RPC. *This version of the SDK will load the REST class by default.* If you still need the XML-RPC class, pass `'xml'` as an argument when requesting the object: `$infusionsoft->orders('xml')'`

Kudos to [toddstoker](https://github.com/toddstoker) and [mattmerrill](https://github.com/mattmerrill) for their contributions to this release.

## Install

Expand All @@ -27,7 +33,7 @@ Or manually add it to your composer.json:
}
```

## Usage
## Authentication

The client ID and secret are the key and secret for your OAuth2 application found at the [Infusionsoft Developers](https://keys.developer.infusionsoft.com/apps/mykeys) website.

Expand Down Expand Up @@ -56,15 +62,102 @@ if ($infusionsoft->getToken()) {
// Save the serialized token to the current session for subsequent requests
$_SESSION['token'] = serialize($infusionsoft->getToken());

$infusionsoft->contacts->add(array('FirstName' => 'John', 'LastName' => 'Doe'));
// MAKE INFUSIONSOFT REQUEST
} else {
echo '<a href="' . $infusionsoft->getAuthorizationUrl() . '">Click here to authorize</a>';
}
```

## Making XML-RPC Requests

```php
require_once 'vendor/autoload.php';

//
// Setup your Infusionsoft object here, then set your token either via the request or from storage
//
$infusionsoft->setToken($myTokenObject);

$infusionsoft->contacts->add(array('FirstName' => 'John', 'LastName' => 'Doe'));

```

## Making REST Requests

The PHP SDK is setup to allow easy access to REST endpoints. In general, a single result is returned as a Class representing
that object, and multiple objects are returned as an Infusionsoft Collection, which is simply a wrapper around an array
of results making them easier to manage.

The standard REST operations are mapped to a series of simple functions. We'll use the Tasks service for our examples,
but the operations below work on all documented Infusionsoft REST services.

To retrieve all tasks:

```php
$tasks = $infusionsoft->tasks()->all();
```

To retrieve a single task:

```php
$task = $infusionsoft->tasks()->find($taskId);
```

To query only completed tasks:

```php
$tasks = $infusionsoft->tasks()->where('status', 'completed')->get();
```

You can chain `where()` as many times as you'd like, or you can pass an array:

```php
$tasks = $infusionsoft->tasks()->where(['status' => 'completed', 'user_id' => '45'])->get();
```

To create a task:
```php
$task = $infusionsoft->tasks()->create([
'title' => 'My First Task',
'description' => 'Better get it done!'
]);
```

Then update that task:
```php
$task->title = 'A better task title';
$task->save();
```

And finally, to delete the task:
```php
$task->delete();
```

Several REST services have a `/sync` endpoint, which we provide a helper method for:
```php
$tasks = $infusionsoft->tasks()->sync($syncId);
```

This returns a list of tasks created or updated since the sync ID was last generated.


```php
require_once 'vendor/autoload.php';

//
// Setup your Infusionsoft object here, then set your token either via the request or from storage
//
$infusionsoft->setToken($myTokenObject);

$infusionsoft->tasks()->find('1');

```


### Dates

DateTime objects are now used instead of a DateTime string where the date(time) is a parameter in the method.
DateTime objects are used instead of a DateTime string where the date(time) is a parameter in the method.

```php
$datetime = new \DateTime('now',new \DateTimeZone('America/New_York'));
Expand All @@ -87,14 +180,13 @@ $infusionsoft->getLogs();
You can utilize the powerful logging plugin built into Guzzle by using one of the available adapters. For example, to use the Monolog writer to write to a file:

```php
use Guzzle\Log\MonologLogAdapter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('client');
$logger->pushHandler(new StreamHandler('infusionsoft.log'));

$infusionsoft->setHttpLogAdapter(new MonologLogAdapter($logger));
$infusionsoft->setHttpLogAdapter($logger);
```

## Testing
Expand All @@ -103,7 +195,7 @@ $infusionsoft->setHttpLogAdapter(new MonologLogAdapter($logger));
$ phpunit
```

## Laravel/Lumen Providers
## Laravel 5.1 Service Provider

In config/app.php, register the service provider

Expand Down Expand Up @@ -139,6 +231,28 @@ Access Infusionsoft from the Facade or Binding
$data = app('infusionsoft')->query("Contact",1000,0,['Id' => '123'],['Id','FirstName','LastName','Email']);
```

## Lumen Service Provider

In bootstrap/app.php, register the service provider

```
$app->register(Infusionsoft\FrameworkSupport\Lumen\InfusionsoftServiceProvider::class);
```

Set your env variables (make sure you're loading your env file in app.php)

```
INFUSIONSOFT_CLIENT_ID=xxxxxxxx
INFUSIONSOFT_SECRET=xxxxxxxx
INFUSIONSOFT_REDIRECT_URL=http://localhost/auth/callback
```

Access Infusionsoft from the Binding

```
$data = app('infusionsoft')->query("Contact",1000,0,['Id' => '123'],['Id','FirstName','LastName','Email']);
```

## Contributing

Please see [CONTRIBUTING](https://github.com/infusionsoft/infusionsoft-php/blob/master/CONTRIBUTING.md) for details.
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=5.5",
"ext-curl": "*",
"guzzle/guzzle": "3.8.*",
"lstrojny/fxmlrpc": "0.9.6"
"guzzlehttp/guzzle": "~6.0",
"lstrojny/fxmlrpc": "~0.10",
"egeloen/http-adapter": "~0.6",
"psr/log": "~1.0"
},
"require-dev": {
"mockery/mockery": "dev-master",
Expand Down
Loading

0 comments on commit 9716cb7

Please sign in to comment.