yidas/line-notify-sdk

PHP版的LINE Notify SDK

1.0.2 2020-12-15 04:45 UTC

This package is auto-updated.

Last update: 2024-09-15 12:50:38 UTC


README

LINE Notify SDK for PHP


PHP版的LINE Notify SDK

Latest Stable Version License

概述

演示

LINE Notify 示例代码网站

作为快速入门,使用Auth客户端创建带有回调重定向URL的授权URL

$lineNotifyAuth = new \yidas\lineNotify\Auth([
    'clientId' => 'i5zOKhJ9hGyRYdCk281wJr',
    'clientSecret' => '************************',
]);

$authUrl = $$lineNotifyAuth->getAuthUrl("http://localhost/redirectUrl.php");
// header("Location: {$authUrl}");

接下来,使用Auth客户端在回调URL(redirectUrl.php)上获取accessToken,然后使用Notify客户端使用accessToken发送通知

// Get accessToekn by automatically obtaining callback code from query string
$accessToken = $lineNotifyAuth->getAccessToken();

// Send notification with accessToken (Concurrency supported)
$lineNotify = new \yidas\lineNotify\Notify($accessToken);
$result = $lineNotify->notify('Hello!');

要求

此库需要以下内容

LINE Notify客户端

每个LINE Notify服务都需要以下认证信息以进行集成,如下所示

  • 客户端ID
  • 客户端密钥

获取LINE Notify客户端

  1. LINE Notify - 添加服务注册新的LINE Notify服务,并设置重定向URL。
  2. 注册后,从LINE Notify - 管理已注册的服务(服务提供商)获取您的服务的clientId/clientSecret以进行集成。

安装

在您的项目中运行Composer

composer require yidas/line-notify-sdk ~1.0.0

然后您可以在PHP项目中加载Composer后使用SDK类

require __DIR__ . '/vendor/autoload.php';

use yidas\lineNotify\Auth;
use yidas\lineNotify\Notify;

使用方法

在使用任何API方法之前,首先您需要使用配置创建一个客户端,然后使用客户端访问LINE Notify API方法。

认证客户端

使用API认证创建LINE Notify认证客户端

$lineNotifyAuth = new \yidas\lineNotify\Auth([
    'clientId' => 'Your LINE Notify service's client ID',
    'clientSecret' => 'Your LINE Notify service's client Secret',
    // 'debug' => true,
    // 'log' => true,
]);
参数
  • array $config:

getAuthUrl()

获取LINE Notify OAuth授权URL

public string getAuthUrl(string $redirectUrl=null, string $state='none', string $responseMode=null)

示例

// Set redirectUrl to `/callback` from the same path of current URL
define('LINE_NOTIFY_REDIRECT_URL', \yidas\lineNotify\Auth::getWebPath() . "/callback");
$authUrl = $lineNotifyAuth->getAuthUrl(LINE_NOTIFY_REDIRECT_URL);

getAccessToken()

使用重定向URL和回调的代码获取AccessToken

public string getAccessToken(string $redirectUri=false, string $code=false, boolean $stateForVerify=false)

示例

$accessToken = $lineNotifyAuth->getAccessToken(LINE_NOTIFY_REDIRECT_URL, $_GET['code'], 'CSRF state for verifying');

getCode()

在回调重定向URL上获取code

static public string getCode(string $stateForVerify=false)

getWebPath()

获取当前web URL路径

static public string getWebPath()

通知客户端

使用accessToekn设置创建LINE Notify客户端

$lineNotify = new \yidas\lineNotify\Notify('HkyggKbHymoS*****************sFuVfa0mlcBNPI', [
    // 'debug' => true,
    // 'log' => true,
]);
参数
  • string|array $accessTokens: 支持单个或多个accessToken进行通知
  • array $config:

notify()

根据accessToken并发发送通知

public integer notify(string $message, array $options=[], string|array $accessTokens=false)

返回值:成功通知的数量

示例

// Send single notification with one accessToken
$lineNotify = new \yidas\lineNotify\Notify('HkyggKbHymoS*****************sFuVfa0mlcBNPI');
$result = $lineNotify->notify('Hello!');

// Send notification for multiple accessTokens concurrently
$lineNotify = new \yidas\lineNotify\Notify(['GymoS****', 'Afa0****']);
$sccessNum = $lineNotify->notify('Hello everyone!');
选项

示例

$lineNotify = new \yidas\lineNotify\Notify(['HkyggKbHymoS*****************sFuVfa0mlcBNPI']);

// Send notification with image URL options
$successNum = $lineNotify->notify(false, [
    'message' => 'Image Notify',
    'imageThumbnail'=>'https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/LINE_logo.svg/220px-LINE_logo.svg.png',
    'imageFullsize'=>'https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/LINE_logo.svg/440px-LINE_logo.svg.png',
    ]);
    
// Send notification with image upload options
$successNum = $lineNotify->notify(false, [
    'message' => 'Image Notify',
    'imageFile' => '/tmp/filename.png',
    ]);
    
// Send notification with sticker options
$successNum = $lineNotify->notify(false, [
    'message' => 'Sticker Notify',
    'stickerPackageId' => 1,
    'stickerId' => 100,
    ]);

imageFile需要是字符串类型的文件路径

status()

检查accessToken连接状态

public array status(string $accessToken)

示例

$response = $lineNotify->status('HkyggKbHymoS*****************sFuVfa0mlcBNPI');
$statusCode = $response['status'];

revoke()

在连接的服务端撤销accessToken

public  revoke(string $accessToken)

示例

$result = $lineNotify->revoke('HkyggKbHymoS*****************sFuVfa0mlcBNPI');

setAccessTokens()

为发送通知设置AccessTokens

public self setAccessTokens(array $accessTokens=[])

addAccessToken()

为发送通知添加AccessToken

public self addAccessToken(string $accessToken)

getRateLimit()

获取最后响应的Rate Limit信息

public array getRateLimit()

共享方法

getResponseLogs()

在启用日志模式时获取响应日志

public array getResponseLogs()

资源

LINE Notify(英文)

LINE Notify API文档(英文)