one2joe / line-notify-sdk-php-no-ssl
LINE Notify SDK for PHP
dev-master
2022-09-30 03:11 UTC
Requires
- php: >=5.4
- guzzlehttp/guzzle: >=5.3.1
This package is auto-updated.
Last update: 2024-09-29 06:25:58 UTC
README
LINE Notify SDK for PHP
LINE Notify SDK for PHP
目录
DEMONSTRATION
作为快速入门,使用 Auth 客户端创建带有回调重定向URL的授权URL
$lineNotifyAuth = new \yidas\lineNotify\Auth([ 'clientId' => 'i5zOKhJ9hGyRYdCk281wJr', 'clientSecret' => '************************', ]); $authUrl = $$lineNotifyAuth->getAuthUrl("https:///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!');
需求
此库需要以下内容
- PHP >= 5.4.0
- guzzlehttp/guzzle >= 5.3.1
- LINE Notify 服务客户端
LINE Notify 客户端
每个LINE Notify服务都需要集成时的认证信息,如下所示
- 客户端ID
- 客户端密钥
获取LINE Notify客户端
- 从 LINE Notify - 添加服务 注册新的LINE Notify服务,并设置重定向URL。
- 注册后,从 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和回调的code获取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(s)并发发送通知
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()
获取最后响应的速率限制信息
public array getRateLimit()
共享方法
getResponseLogs()
启用日志模式时获取响应日志
public array getResponseLogs()