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:
- 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.
- 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 telemessage/web 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
TeleSign SDK for PHP
TeleSign Enterprise SDK
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Extensible library for building notifications and sending them via different delivery channels.
yii2-sms expand
统计信息
- 总下载量: 425
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-29