telemessage / web
TeleMessage PHP 库,用于通过 TeleMessage 发送消息
Requires
- php: >=5.5.4
- grinfeld/phpjsonable: ~1.0
This package is not auto-updated.
Last update: 2024-09-28 17:23:05 UTC
README
一个用于与 TeleMessage REST API 通信的 PHP 库,用于发送和查询已发送消息的状态
使用 composer
发送消息
首先,我们需要导入 telemessage 类
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;
然后,我们需要使用 TeleMessage 账户凭证创建对象
$auth = new AuthenticationDetails();
$auth->setUsername("john_donne");
$auth->setPassword("12345678");
让我们填写消息数据。
现在,我们设置文本和主题
$m = new Message();
$m->setSubject("Hello");
$m->setTextmessage("My message");
然后我们创建接收者并将其添加到消息中
$recp = new Recipient();
$recp->setType("SMS");
$recp->setValue("+1xxxxxxxx");
$m->addRecipient($recp);
消息已准备好。现在我们需要将其转换为 JSON 格式并发送到 TeleMessage REST 网关。
首先,我们生成一个对象,该对象将存储 JSON 输出:$output = new StringOutputStream();
然后,我们通过调用 TeleMessage::encode(array($auth, $m), $output);
将我们的请求数据编码为 JSON
现在 JSON 已准备就绪,我们将其发送到 TeleMessage REST 网关。在我们的示例中,我们使用 CURL,但您可以使用您喜欢的包来发送 HTTP POST 请求
$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);
下面几行我们添加 TeleMessage REST 网关 URL 和 JSON 请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $output->toString());
curl_setopt($ch, CURLOPT_URL, TeleMessage::getSendURL());
$postResult = curl_exec($ch);
curl_close($ch);
现在 $postResult
包含从 TeleMessage 收到的响应。最后一个小步骤是解析响应并找到 TeleMessage messageId 和 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();
}
}
我们从 TeleMessage 收到响应并执行了 TeleMessage::decode(new StringInputStream($postResult))
以接收 MessageResponse
。
就是这样 - 消息已发送!
带有附件发送消息
带有附件发送消息与发送简单消息非常相似。您需要做的唯一一件事是使用 addFileMessage 方法添加您的文件
$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);
注意
- 某些文件类型可能不支持您的目标,例如,如果您将 tiff 文件发送到 SMS,附件将不会被添加,但是相同的文件将被发送到传真接收者。
- 您必须将附件文件编码为 Base64。更多信息请在这里找到。
查询状态
太好了!消息已发送,但您刚刚收到的状态说消息“尚未投递”。发送消息可能需要几秒钟,因此我们在我们的 REST API 中添加了 queryStatus
,您可以使用我们的 PHP API 库使用它。看以下示例
首先,我们假设 messageId 和 消息密钥 在消息发送后存储在 $messageId
和 $messageKey
变量中。
同样,我们需要使用 TeleMessage 账户凭证创建对象
$auth = new AuthenticationDetails();
$auth->setUsername("john_donne");
$auth->setPassword("12345678");
我们生成一个对象,该对象将存储 JSON 输出:$output = new StringOutputStream();
我们通过调用 TeleMessage::encode(array($auth, $messageId, $messageKey), $output);
将我们的请求数据编码为 JSON
我们再次使用 CURL 发送 POST 请求到 TeleMessage 网关,但您可以使用您喜欢的包来发送 HTTP POST 请求
$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);
下面几行我们添加 TeleMessage REST 网关 URL 和 JSON 请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $output->toString());
curl_setopt($ch, CURLOPT_URL, TeleMessage::getStatusURL());
$postResult = curl_exec($ch);
curl_close($ch);
现在,我们需要解析响应并将其打印出来
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();
}
}
我们从 TeleMessage 收到响应并执行了 TeleMessage::decode(new StringInputStream($postResult))
以接收 MessageStatusResponse
。
在不使用 composer 的情况下包含 TeleMessage PHP 库
使用不带 composer 的 PHP 库几乎与使用 composer 一样。首先从 Github 下载项目发布版本,解压缩并找到 telemessage_web.phar
主要区别在于:在您的脚本开头,而不是使用 composer 的 require "vendor/autoload.php"
,请写下以下几行
require "telemessage_web.phar";
TMLoader::get();
在这行之后,使用与composer示例中相同的代码
就是这样。希望这对您有所帮助,现在您可以使用TeleMessage服务更加方便。
注意
- 没有composer的旧版本,您可以在这里找到