luongtran / infusionsoft-php
Infusionsoft的Beta PHP SDK
Requires
- php: >=5.5
- ext-curl: *
- guzzle/guzzle: ^3.9.2
- guzzlehttp/guzzle: ~6.1
- lstrojny/fxmlrpc: 0.10
- luongtran/http-adapter: 0.7.3
- psr/log: ~1.0
Requires (Dev)
- codeception/aspect-mock: 0.5.1
- mockery/mockery: dev-master
- phpunit/phpunit: ~4
- squizlabs/php_codesniffer: 1.*
README
版本说明
本版本实现了RESTful端点,Guzzle的新版本以及重构的请求处理器。
重大变更
如果您使用Orders
或Products
服务,现在有两个不同的类来处理每个服务 - 一个用于REST,一个用于XML-RPC。此SDK版本将默认加载REST类。如果您仍然需要XML-RPC类,请在请求对象时传递'xml'
作为参数:$infusionsoft->orders('xml')
感谢 toddstoker 和 mattmerrill 对此次发布的贡献。
安装
使用composer CLI
composer require infusionsoft/php-sdk
或手动将其添加到您的composer.json文件中
{ "require": { "infusionsoft/php-sdk": "1.2.*" } }
身份验证
客户端ID和密钥是在Infusionsoft开发者网站找到的OAuth2应用的密钥和密钥。
require_once 'vendor/autoload.php'; $infusionsoft = new \Infusionsoft\Infusionsoft(array( 'clientId' => 'XXXXXXXXXXXXXXXXXXXXXXXX', 'clientSecret' => 'XXXXXXXXXX', 'redirectUri' => 'http://example.com/', )); // If the serialized token is available in the session storage, we tell the SDK // to use that token for subsequent requests. if (isset($_SESSION['token'])) { $infusionsoft->setToken(unserialize($_SESSION['token'])); } // If we are returning from Infusionsoft we need to exchange the code for an // access token. if (isset($_GET['code']) and !$infusionsoft->getToken()) { $infusionsoft->requestAccessToken($_GET['code']); } if ($infusionsoft->getToken()) { // Save the serialized token to the current session for subsequent requests $_SESSION['token'] = serialize($infusionsoft->getToken()); // MAKE INFUSIONSOFT REQUEST } else { echo '<a href="' . $infusionsoft->getAuthorizationUrl() . '">Click here to authorize</a>'; }
制作XML-RPC请求
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'));
制作REST请求
PHP SDK已配置,允许轻松访问REST端点。通常,单个结果以表示该对象的类返回,多个对象以Infusionsoft Collection返回,它简单地对结果数组进行包装,使其更易于管理。
标准REST操作映射到一系列简单函数。我们将使用Tasks服务作为示例,但以下操作适用于所有记录的Infusionsoft REST服务。
检索所有任务
$tasks = $infusionsoft->tasks()->all();
检索单个任务
$task = $infusionsoft->tasks()->find($taskId);
仅查询已完成任务
$tasks = $infusionsoft->tasks()->where('status', 'completed')->get();
您可以根据需要多次使用where()
,或者传递一个数组
$tasks = $infusionsoft->tasks()->where(['status' => 'completed', 'user_id' => '45'])->get();
创建一个任务
$task = $infusionsoft->tasks()->create([ 'title' => 'My First Task', 'description' => 'Better get it done!' ]);
然后更新该任务
$task->title = 'A better task title'; $task->save();
最后,删除该任务
$task->delete();
几个REST服务都有一个/sync
端点,我们为此提供了一个辅助方法
$tasks = $infusionsoft->tasks()->sync($syncId);
这将返回自上次生成同步ID以来创建或更新的任务列表。
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');
日期
当日期(时间)是方法参数时,使用DateTime对象而不是DateTime字符串。
$datetime = new \DateTime('now',new \DateTimeZone('America/New_York'));
调试
要启用请求和响应的调试,您需要使用以下方式设置调试标志
$infusionsoft->setDebug(true);
一旦启用,日志将默认写入一个数组,可以通过以下方式访问
$infusionsoft->getLogs();
您可以通过使用可用的适配器利用Guzzle内置的强大日志插件。例如,要使用Monolog writer将日志写入文件
use Monolog\Handler\StreamHandler; use Monolog\Logger; $logger = new Logger('client'); $logger->pushHandler(new StreamHandler('infusionsoft.log')); $infusionsoft->setHttpLogAdapter($logger);
测试
$ phpunit
Laravel 5.1 Service Provider
在config/app.php中注册服务提供者
Infusionsoft\FrameworkSupport\Laravel\InfusionsoftServiceProvider::class,
注册外观(可选)
'Infusionsoft' => Infusionsoft\FrameworkSupport\Laravel\InfusionsoftFacade::class
发布配置
php artisan vendor:publish --provider="Infusionsoft\FrameworkSupport\Laravel\InfusionsoftServiceProvider"
设置您的环境变量
INFUSIONSOFT_CLIENT_ID=xxxxxxxx
INFUSIONSOFT_SECRET=xxxxxxxx
INFUSIONSOFT_REDIRECT_URL=http://localhost/auth/callback
通过外观或绑定访问Infusionsoft
$data = Infusionsoft::query("Contact",1000,0,['Id' => '123'],['Id','FirstName','LastName','Email'], 'Id', false);
$data = app('infusionsoft')->query("Contact",1000,0,['Id' => '123'],['Id','FirstName','LastName','Email'], 'Id', false);
Lumen Service Provider
在bootstrap/app.php中注册服务提供者
$app->register(Infusionsoft\FrameworkSupport\Lumen\InfusionsoftServiceProvider::class);
设置您的环境变量(确保您在app.php中加载了环境文件)
INFUSIONSOFT_CLIENT_ID=xxxxxxxx
INFUSIONSOFT_SECRET=xxxxxxxx
INFUSIONSOFT_REDIRECT_URL=http://localhost/auth/callback
通过绑定访问Infusionsoft
$data = app('infusionsoft')->query("Contact",1000,0,['Id' => '123'],['Id','FirstName','LastName','Email'], 'Id', false);
贡献
请参阅CONTRIBUTING以获取详细信息。
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。