hebinet/websms-client

一个轻量级的PHP客户端库,用于使用websms.com短信服务。

v2.0.5 2022-05-08 08:08 UTC

This package is auto-updated.

Last update: 2024-09-08 13:12:44 UTC


README

一个轻量级的PHP客户端库,用于使用websms.com短信服务。简化了客户端与短信网关之间的网络通信复杂性,帮助商业客户节省时间,集中精力在业务逻辑上。

为什么创建这个分支

重新编写了完整的SDK,以满足最新的编码标准并利用现代库。

以下为添加的改进

  • 将最低要求的PHP版本提升到8.0+(对于PHP <8.0请使用v1.0.7.7)
  • 使用命名空间
  • 将属性和方法从kebab-case转换为CamelCase
  • 使用知名的GuzzleHttp库代替plain Curl或fsockopen
  • 添加PHPDoc风格注释
  • 在方法中使用参数类型提示以确保特定的变量类型,并删除不必要的类型检查
  • 移除了JSON.phps依赖,并使用本地的php json扩展
  • 将客户端版本提升到原始README文件中提到的版本

但最重要的是,我这样做是为了乐趣 :)

安装

composer require hebinet/websms-client

示例

// First create the WebSms client

// via username/password
$client = new WebSms\Client($gatewayUrl, $username, $password);
// or via TokenAuth
$client = new WebSms\Client($gatewayUrl, $authToken, null, AuthenticationMode::ACCESS_TOKEN);


// Then create an Message object with the recipients and the message
$message = new WebSms\TextMessage(['4366412345678'], 'Test Message');

// To send the message, just call the send method on the client
$response = $client->send($message);

以下是原始send_sms.php文件内容转换为新的类

<?php
use WebSms\AuthenticationMode;
use WebSms\BinaryMessage;
use WebSms\Client;
use WebSms\Exception\ApiException;
use WebSms\Exception\AuthorizationFailedException;
use WebSms\Exception\HttpConnectionException;
use WebSms\Exception\ParameterValidationException;
use WebSms\Exception\UnknownResponseException;
use WebSms\TextMessage;

# Modify these values to your needs
$username = 'your_username';
$password = 'your_password';
// OR (optional)
$accessToken = 'your_access_token';
$gatewayUrl = 'https://api.websms.com/';

$recipientAddressList = array("4367612345678");
$utf8MessageText = "Willkommen zur BusinessPlatform SDK von websms.com! Diese Nachricht enthält 160 Zeichen. Sonderzeichen: äöüß. Eurozeichen: €. Das Ende wird nun ausgezählt43210";

$maxSmsPerMessage = 1;
$test = false; // true: do not send sms for real, just test interface

try {

    // 1.) -- create sms client (once) ------
    $smsClient = new Client($gatewayUrl, $username, $password);

    // 1.) -- Alternatively authenticate over access token
    // $smsClient = new Client($gateway_url, $accessToken, null, AuthenticationMode::ACCESS_TOKEN);

    //$smsClient->setVerbose(true);
    //$smsClient->setSslVerifyHost(false); // needed if you want to disable the SSL check completely. (Default: true)

    // 2.) -- create text message ----------------
    $message = new TextMessage($recipientAddressList, $utf8MessageText);
    //$message = binarySmsSample($recipientAddressList);
    //$maxSmsPerMessage = null;  //needed if binary messages should be send

    // 3.) -- send message ------------------
    $response = $smsClient->send($message, $maxSmsPerMessage, $test);
    // $response is now a class with all the api specific methods and maps all other methods magically to the Guzzle Response object
    

    // show success
    print_r([
        "Status          : " . $response->getApiStatusCode(),
        "StatusMessage   : " . $response->getApiStatusMessage(),
        "TransferId      : " . $response->getTransferId(),
        "ClientMessageId : " . (($response->getClientMessageId()) ?
            $response->getClientMessageId() : '<NOT SET>'),
    ]);

    // catch everything that's not a successfully sent message
} catch (ParameterValidationException $e) {
    exit("ParameterValidationException caught: " . $e->getMessage() . "\n");

} catch (AuthorizationFailedException $e) {
    exit("AuthorizationFailedException caught: " . $e->getMessage() . "\n");

} catch (ApiException $e) {
    echo $e; // possibility to handle API status codes $e->getCode()
    exit("ApiException Exception\n");

} catch (HttpConnectionException $e) {
    exit("HttpConnectionException caught: " . $e->getMessage() . "HTTP Status: " . $e->getCode() . "\n");

} catch (UnknownResponseException $e) {
    exit("UnknownResponseException caught: " . $e->getMessage() . "\n");

} catch (Exception $e) {
    exit("Exception caught: " . $e->getMessage() . "\n");
}

//-- end of main

//-----------------------------------------------
// binary_message_content
//-----------------------------------------------
function binarySmsSample($recipientAddressList)
{

    //| Working messageContent sample of PDU sms containing content "Zusammengefügt."
    //| sent as 2 SMS segments: ("Zusammen","gefügt.").
    //| First 6 Bytes per segment are sample UDH. See http://en.wikipedia.org/wiki/Concatenated_SMS

    //$messageContentSegments = array(
    //    "BQAD/AIBWnVzYW1tZW4=", // 0x05,0x00,0x03,0xfc,0x02,0x01, 0x5a,0x75,0x73,0x61,0x6d,0x6d,0x65,0x6e
    //    "BQAD/AICZ2Vmw7xndC4="  // 0x05,0x00,0x03,0xfc,0x02,0x02, 0x67,0x65,0x66,0xc3,0xbc,0x67,0x74,0x2e
    //);

    // bytewise rebuilt sample. (identical to above):
    $messageContentSegments = [
        base64_encode(pack("c*", 0x05, 0x00, 0x03, 0xfc, 0x02, 0x01, 0x5a, 0x75, 0x73, 0x61, 0x6d, 0x6d, 0x65, 0x6e)),
        base64_encode(pack("c*", 0x05, 0x00, 0x03, 0xfc, 0x02, 0x02, 0x67, 0x65, 0x66, 0xc3, 0xbc, 0x67, 0x74, 0x2e))
    ];
    $userDataHeaderPresent = true; # Binary Data includes UserDataHeader for e.G.: PDU sms (Concatenation)

    $message = new BinaryMessage($recipientAddressList, $messageContentSegments, $userDataHeaderPresent);

    return $message;
}

贡献

有关详细信息,请参阅CONTRIBUTING

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件