brunohccz/infusionsoft-php

Infusionsoft 的 PHP SDK

dev-master 2021-03-26 12:33 UTC

This package is auto-updated.

Last update: 2024-09-26 20:30:03 UTC


README

Build Status Total Downloads Latest Stable Version

版本说明

本版本实现了 RESTful 端点,Guzzle 的新版本以及重构的请求处理器。

截至版本 1.4,需要 PHP 7+。

重大变更

如果您使用 ContactsOrdersProducts 服务,现在有两个不同的类处理每个服务 - 一个用于 REST,一个用于 XML-RPC。本 SDK 版本默认加载 REST 类。如果您仍然需要 XML-RPC 类,在请求对象时传递 'xml' 作为参数:$infusionsoft->orders('xml')

感谢 toddstokermattmerrill 对本版本的贡献。

安装

使用 composer CLI

composer require infusionsoft/php-sdk

或将它手动添加到您的 composer.json 文件中

{
    "require": {
        "infusionsoft/php-sdk": "1.4.*"
    }
}

认证

客户端 ID 和密钥是您在 Infusionsoft 开发者 网站中找到的 OAuth2 应用程序的密钥和密钥。

if(empty(session_id();)) session_start();

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()) {
	$_SESSION['token'] = serialize($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
// As of v1.3 contacts defaults to rest
$infusionsoft->setToken($myTokenObject);

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

制作 REST 请求

PHP SDK 配置为允许轻松访问 REST 端点。通常,单个结果以表示该对象的类返回,多个对象以 Infusionsoft Collection 返回,它只是一个结果数组的包装,使它们更容易管理。

标准 REST 操作映射到一系列简单函数。我们将使用任务服务作为示例,但以下操作适用于所有文档化的 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 服务提供者

在 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=https:///auth/callback

通过外观或绑定访问 Infusionsoft

 $data = Infusionsoft::data()->query("Contact",1000,0,['Id' => '123'],['Id','FirstName','LastName','Email'], 'Id', false);

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

Lumen 服务提供者

在 bootstrap/app.php 中注册服务提供者

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

设置您的环境变量(确保在 app.php 中加载您的环境文件)

INFUSIONSOFT_CLIENT_ID=xxxxxxxx
INFUSIONSOFT_SECRET=xxxxxxxx
INFUSIONSOFT_REDIRECT_URL=https:///auth/callback

通过绑定访问 Infusionsoft

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

贡献

请参阅CONTRIBUTING以获取详细信息。

许可协议

MIT 许可协议(MIT)。请参阅许可文件以获取更多信息。