定制 hebinet/websms-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hebinet/websms-client

Composer 安装命令:

composer require hebinet/websms-client

包简介

A lightweight PHP-client-library for using websms.com SMS services.

README 文档

README

A lightweight PHP-client-library for using websms.com SMS services. Reduces the complexity of network-communication between client and SMS gateway, to help business-customer save time and money for focusing on their business logic.

Why this fork

Rewrote complete SDK to meet the newest coding standards and utilize modern libraries.

Added the following improvements:

  • Raised minimum required PHP version to 8.0+ (For PHP <8.0 use v1.0.7.7)
  • Use Namespaces
  • Switched from kebab-case to CamelCase for properties and methods
  • Used famous and well tested GuzzleHttp library instead of plain Curl or fsockopen
  • Added PHPDoc style comments
  • Used parameter type hints in methods to ensure certain var types and removed unnecessary type checking
  • Removed JSON.phps dependency and used native php json extension instead
  • Raised client version to that one mentioned in the original readme file

But most of all, I did it for fun :)

Installation

composer require hebinet/websms-client

Example

// 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);

and here the content of the original send_sms.php file "translated" to the new classes

<?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

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

hebinet/websms-client 适用场景与选型建议

hebinet/websms-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 449 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hebinet/websms-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hebinet/websms-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 hebinet/websms-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 449
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-08