spydertext/php-sdk

dev-master 2017-10-31 17:39 UTC

This package is not auto-updated.

Last update: 2024-09-19 19:12:37 UTC


README

当前SpyderText PHP Sdk处于Beta阶段,并且仅构建了一部分SDK。随着我们为SpyderText提供的附加API添加支持,这个库将持续更新。

入门指南

使用composer只需执行

composer require spydertext/php-sdk "dev-master"

完整文档可在以下位置找到: https://api.spydertext.com/docs/getting-started

身份验证

您需要使用SDK生成一个设备令牌,该令牌将用于所有其他API调用。此令牌应保存在数据库中、环境变量中或与您的其他机密信息一起放置。

use SpyderText\SpyderText;

SpyderText::setApiKey("YOUR_API_KEY_HERE");

$deviceToken = SpyderText::Authentication()->createDeviceToken();
//store on disk, or in the database for usage for all other API calls
file_put_contents('./.deviceToken', $deviceToken['device_id']);

echo $deviceToken;

账户SDK

示例位于examples/account目录中,凭据应放置在examples/common.php中

获取账户集合

use SpyderText\SpyderText;

SpyderText::setApiKey(API_KEY);
SpyderText::setDeviceToken(DEVICE_TOKEN);

$collection = SpyderText::Account()->collection([]);

/*
test searching by query
$collection = SpyderText::Account()->collection([
    'q' => 'test'
]);
*/

//will automatically continue looping through and doing API calls for additional pages until it reaches the end
foreach($collection as $account)
{
    echo $account['id'] . "\n";
    echo $account['name'] . "\n\n";
}

获取账户

use SpyderText\SpyderText;

SpyderText::setApiKey(API_KEY);
SpyderText::setDeviceToken(DEVICE_TOKEN);

//get account by ID
$account = SpyderText::Account()->get(4);
var_dump($account);

保存账户

use SpyderText\SpyderText;

SpyderText::setApiKey(API_KEY);
//should already have a device token, if not, see authentication/create-device-token.php 
SpyderText::setDeviceToken(DEVICE_TOKEN);

$account = SpyderText::Account()->create([
    'name' => 'Jane Doe',
    'email' => 'jane@example.com',
    'mobile_phone' => '15551231234'
]);

删除账户

use SpyderText\SpyderText;

SpyderText::setApiKey(API_KEY);
//should already have a device token, if not, see authentication/create-device-token.php 
SpyderText::setDeviceToken(DEVICE_TOKEN);

//delete account by ID
$result = SpyderText::Account()->delete(4);
if($result && $result['success']) {
    echo "Successfully deleted account!\n";
}

程序SDK

示例位于examples/program目录中,凭据应放置在examples/common.php中

获取程序集合

SpyderText::setApiKey(API_KEY);
SpyderText::setDeviceToken(DEVICE_TOKEN);

$collection = SpyderText::Program()->collection([]);

//will automatically continue looping through and doing API calls for additional pages until it reaches the end
foreach($collection as $program)
{
    echo $program['id'] . "\n";
    echo $program['name'] . "\n\n";
}

添加参与者

use SpyderText\SpyderText;

SpyderText::setApiKey(API_KEY);
SpyderText::setDeviceToken(DEVICE_TOKEN);

$programId = 1;
$accountId = 1;

$result = SpyderText::Program()->addParticipant($programId, $accountId);

if(!$result || !$result['success']) {
    echo "Failed to add participant\n";
    exit;
}

echo "Successfully added participant to program\n";