lifeboat/php-sdk

Lifeboat PHP SDK

dev-main 2023-12-28 11:34 UTC

This package is auto-updated.

Last update: 2024-09-28 13:14:45 UTC


README

Build Status Latest Stable Version License

此SDK旨在帮助开发者构建访问Lifeboat API的应用程序。
lifeboat.app

开发中

此SDK仍在开发中。

Lifeboat PHP SDK允许网站所有者和应用程序开发者利用Lifeboat API在其软件中。

要求

PHP 7.2或更高版本

Composer

您可以通过Composer安装此SDK。运行以下命令

composer require lifeboat/php-sdk

要使用此SDK,您需要使用Composer自动加载。

require_once('vendor/autoload.php');

设置

  1. dev.lifeboat.app上创建一个开发者账户
  2. 注册应用程序

注意

永远不要分享您的应用程序凭证

入门指南

调用客户端

初始化SDK并将用户重定向到Lifeboat OAuth服务。OAuth服务将自动登录并捕获用户授权使用您的应用程序。

// The SDK requires sessions to store and cache data
session_start();

$client = new \Lifeboat\App(
    '<APP ID>',
    '<APP SECRET>'
);

// The SDK will create a strong challenge key
// and automatically store it in the session
$challenge = $client->getAPIChallenge();

// Redirect the user to the auth screen
$redirect_url = $client->getAuthURL(
    'https://my.app/oauth/process_url',
    'https://my.app/oauth/error_url',
    $challenge
);
header("Location: {$redirect_url}");
exit;
处理页面
// https://my.app/oauth/process_url
session_start();
$client = new \Lifeboat\App(
    '<APP ID>',
    '<APP SECRET>'
);

// This code will be returned by the oauth service to provide
// you with temporary access to the logged in user account.
$code = $_GET['code'];

// Get an access token
// The SDK will automatically store the access token in sessions
//
// If the user has access to multiple stores
// the SDK will also automatically select the active store
// based on the user's selection durin OAuth
$access_token = $client->fetchAccessToken($code);

在不与用户交互的情况下使用客户端

有时您可能需要在用户未与您的应用程序积极交互的情况下执行用户存储上的操作。一个常见的用例是在cron中检索产品以进行分析或其他检查。

为此,您需要知道两个重要参数

  • site_key:您要访问的存储的site_key
  • site_host:存储的master域名

注意:在您可以使用此方法之前,用户必须首先授权您的应用程序

$client = new \Lifeboat\App(
    '<APP ID>',
    '<APP SECRET>'
);

// Let the SDK know which store you'll be interacting with
$client->setActiveSite($site_host, $site_key);

从现在开始,您可以像用户已登录一样继续使用$client。SDK和Lifeboat OAuth将自动检查您的应用程序是否已由用户授权。

基本用法

从现在开始创建、操作和删除对象完全通过SDK完成。

$product = $client->product->create([
    'Title' => 'MyProduct',
    'SKU' => 'xxx',
    'TrackStock' => true
]);

// Makes the necessary calls to the API to store the object
$product->save();

示例

您可以在此存储库的examples目录中看到一个工作示例。

  • Auth.php - 前端示例认证控制器
  • Cron.php - 示例cron控制器
  • Store.php - 示例对象以保存商店信息

Latest Unstable Version codecov Scrutinizer Code Quality