承接 telemessage/web 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

telemessage/web

Composer 安装命令:

composer require telemessage/web

包简介

TeleMessage PHP library for sending messages via TeleMessage

README 文档

README

A PHP library for communicating with the TeleMessage REST API for sending and querying status for sent messages

Using composer

Sending Message

First we need to import telemessage classes:

use grinfeld\phpjsonable\utils\streams\StringInputStream;
use grinfeld\phpjsonable\utils\streams\StringOutputStream;
use telemessage\web\services\AuthenticationDetails;
use telemessage\web\services\FileMessage;
use telemessage\web\services\Message;
use telemessage\web\services\Recipient;
use telemessage\web\TeleMessage;

Than we need to create object with TeleMessage account credentials:

$auth = new AuthenticationDetails();
$auth->setUsername("john_donne");
$auth->setPassword("12345678");

Let’s fill in the message data.

Now, we set text and subject:

$m = new Message();
$m->setSubject("Hello");
$m->setTextmessage("My message");

Than we create recipient and add it to message:

$recp = new Recipient();
$recp->setType("SMS");
$recp->setValue("+1xxxxxxxx");
$m->addRecipient($recp);

Message is ready. Now we need to convert it to JSON format and send to TeleMessage REST Gateway.

First we generate object which will store JSON output: $output = new StringOutputStream();

Than we encode our request data into JSON, by calling TeleMessage::encode(array($auth, $m), $output);

Now JSON is ready and we send it to TeleMessage REST Gateway. In our example we use CURL, but you can use your favourite package to send http post requests:

$myHeader = array(
    "MIME-Version: 1.0",
    "Content-type: text/json; charset=utf-8" // define content type JSON
);
//creating and initiating curl
$ch = curl_init();
//setting curl/http headers
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

Next lines we add TeleMessage REST Gateway URL and JSON request:

curl_setopt($ch, CURLOPT_POSTFIELDS, $output->toString());
curl_setopt($ch, CURLOPT_URL, TeleMessage::getSendURL());
$postResult = curl_exec($ch); 
curl_close($ch);

Now $postResult contains response received from TeleMessage. The last small step is to parse response and find TeleMessage messageId and messageKey:

if ($postResult != "") {
    $res = TeleMessage::decode(new StringInputStream($postResult));
    echo "Result code: " . $res->getResultCode();
    if ($res->getResultCode() == TeleMessage::SUCCESS_SEND) {
        echo ", Message key: " . $res->getMessageKey() . ", message id: " . $res->getMessageID();
    } else {
        echo ", Result description: " . $res->getResultDescription();
    }
}

We received response from TeleMessage and executed TeleMessage::decode(new StringInputStream($postResult)) to receive MessageResponse.

That’s it – message is sent!

Sending Messages with Attachments

Sending message with attachments is very similar to sending a simple message. The only thing you need to do is add your file by using the addFileMessage method:

$fm = new FileMessage();
$fm->setFilename("file.png");
$fm->setMimetype("image/png");
$imgPath = pathinfo(__FILE__, PATHINFO_DIRNAME) . "/file.png";
$fm->setValue(base64_encode(file_get_contents($imgPath)));

$m->addFilemessage($fm);

Note:

  1. Some file types won’t be supported by your destination, e.g. if you are sending tiff file to SMS, the attachment won’t be added, however the same file will be delivered to a fax recipient.
  2. You must encode your attachment file into Base64. Find more here.

Query Status

Great! Message is sent, but status you've just received, saying that message "Not delivered yet". Sending message could take few seconds, so we added queryStatus in our REST API and you can use it with our PHP API Library. See how:

First, we assume that messageId and message key are stored in $messageId and $messageKey variables after message has been sent.

Again we need to create object with TeleMessage account credentials:

$auth = new AuthenticationDetails();
$auth->setUsername("john_donne");
$auth->setPassword("12345678");

We generate object which will store JSON output: $output = new StringOutputStream();

We encode our request data into JSON, by calling TeleMessage::encode(array($auth, $messageId, $messageKey), $output);

Again we are using CURL for POSTing request to TeleMessage gateway, but you can use your favourite package to send http post requests:

$myHeader = array(
    "MIME-Version: 1.0",
    "Content-type: text/json; charset=utf-8" // define content type JSON
);
//creating and initiating curl
$ch = curl_init();
//setting curl/http headers
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

Next lines we add TeleMessage REST Gateway URL and JSON request:

curl_setopt($ch, CURLOPT_POSTFIELDS, $output->toString());
curl_setopt($ch, CURLOPT_URL, TeleMessage::getStatusURL());
$postResult = curl_exec($ch); 
curl_close($ch);

Now, we need to parse response and print it out:

if ($postResult != "") {
    $res = TeleMessage::decode(new StringInputStream($postResult));
    echo "Result code: " . $res->getResultCode();
    if ($res->getResultCode() == TeleMessage::SUCCESS_REQUEST) {
        $recipients = $res->getRecipientStatus();
        if (count($recipients) > 0) {
            foreach ($recipients as $status) {
                echo ", Message sent to  " . $status->getRecipient()->getValue() .
                    " with status " . $status->getStatus() . " that means " . $status->getDescription() .
                    " at " . date("y/m/d i:h:s", $status->getStatusDate());
            }
        }
    } else {
        echo "Result description: " . $res->getResultDescription();
    }
}

We received response from TeleMessage and executed TeleMessage::decode(new StringInputStream($postResult)) to receive MessageStatusResponse.

Include TeleMessage PHP library without using composer

Using PHP library without composer is almost the same as with composer. First download project release from Github, extract and find telemessage_web.phar

The main difference is: at the beginning of your script instead of require "vendor/autoload.php" used by composer write following lines:

require "telemessage_web.phar";
TMLoader::get();

After those lines use same code as in composer example

That’s it. We hope it was helpful and now you can use TeleMessage services more easily.

Note:

  • Older version without composer you can find here

telemessage/web 适用场景与选型建议

telemessage/web 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 425 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sms」 「send」 「IP」 「voice」 「mms」 「fax」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 telemessage/web 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-29