ippanel/php-rest-sdk

IPPanel REST API客户端

v2.0.4 2022-10-15 09:53 UTC

This package is not auto-updated.

Last update: 2024-09-14 21:31:26 UTC


README

Build Status

安装

使用composer

composer require ippanel/php-rest-sdk

如果您不想使用composer,可以直接下载

wget https://github.com/ippanel/php-rest-sdk/archive/master.zip

示例

要使用SDK,您需要创建一个客户端实例,该实例提供了API上的可用方法

require 'autoload.php';

// you api key that generated from panel
$apiKey = "api-key";

$client = new \IPPanel\Client($apiKey);

...

信用检查

# return float64 type credit amount
$credit = $client->getCredit();

一对多发送

对于发送短信,显然您需要originator号码、recipientsmessage

$messageId = $client->send(
    "+9810001",          // originator
    ["98912xxxxxxx"],    // recipients
    "ippanel is awesome",// message
    "description"        // is logged
);

如果发送成功,将返回一个唯一的跟踪代码,您可以使用该代码跟踪消息状态。

获取消息摘要

$messageId = "message-tracking-code";

$message = $client->get_message($messageId);

echo $message->state;   // get message status
echo $message->cost;     // get message cost
echo $message->returnCost;  // get message payback

获取消息投递状态

$messageId = "message-tracking-code"

list($statuses, $paginationInfo) = $client->fetchStatuses($messageId, 0, 10)

// you can loop in messages statuses list
foreach($statuses as status) {
    echo sprintf("Recipient: %s, Status: %s", $status->recipient, $status->status);
}

echo sprintf("Total: ", $paginationInfo->total);

收件箱抓取

抓取收件箱消息

list($messages, $paginationInfo) = $client->fetchInbox(0, 10);

foreach($messages as $message) {
    echo sprintf("Received message %s from number %s in line %s", $message->message, $message->from, $message->to);
}

模式创建

对于发送预定义模式的消息(例如验证码等),您必须创建一个模式。一个模式至少有一个参数。

$patternVariables = [
    "name" => "string",
    "code" => "integer",
];

$code = $client->createPattern("%name% is awesome, your code is %code%", "description", 
    $patternVariables, '%', False);

echo $code;

使用模式发送

$patternValues = [
    "name" => "IPPANEL",
];

$messageId = $client->sendPattern(
    "t2cfmnyo0c",    // pattern code
    "+9810001",      // originator
    "98912xxxxxxx",  // recipient
    $patternValues,  // pattern values
);

错误检查

use IPPanel\Errors\Error;
use IPPanel\Errors\HttpException;

try{
    $messageId = $client->send("9810001", ["98912xxxxx"], "ippanel is awesome");
} catch (Error $e) { // ippanel error
    var_dump($e->unwrap()); // get real content of error
    echo $e->getCode();

    // error codes checking
    if ($e->code() == ResponseCodes::ErrUnprocessableEntity) {
        echo "Unprocessable entity";
    }
} catch (HttpException $e) { // http error
    var_dump($e->getMessage()); // get stringified error
    echo $e->getCode();
}