smsgateway.me 将消息状态存储在 php 变量中

smsgateway.me store message status in php variable

使用smsgateway.me发送消息我使用默认代码和我自己的变量,这些变量工作正常。但是,有时 SMS 不会发送,如果失败,我想将 SMS 存储在数据库中。 为此,我想我需要 $result 数组中的一个值,但我不知道如何开始。我尝试了很多不同的代码都没有结果。

任何人都可以告诉我如何根据下面的代码和变量获得 if send ok -> do something / else do something else 吗? 非常感谢!

文档 here

这是发送消息的默认代码(带有默认变量):

<?php
include "smsGateway.php";
$smsGateway = new SmsGateway('demo@smsgateway.me', 'password');

$deviceID = 1;
$number = '+44771232343';
$message = 'Hello World!';

$options = [
'send_at' => strtotime('+10 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the message is not yet sent
];

//Please note options is no required and can be left out
$result = $smsGateway->sendMessageToNumber($number, $message, $deviceID, $options);
?>

成功内容:

{
"success": true,
"result": {
"success": [
{
"id": "308",
"device_id": "4",
"message": "hello world!",
"status": "pending",
"send_at": "1414624856",
"queued_at": "0",
"sent_at": "0",
"delivered_at": "0",
"expires_at": "1414634856",
"canceled_at": "0",
"failed_at": "0",
"received_at": "0",
"error": "None",
"created_at": "1414624856",
"contact": {
"id": "14",
"name": "Phyllis Turner",
"number": "+447791064713"
}
}
],
"fails": [
]
}
}

错误内容:

{
"success": true,
"result": {
"success": [
],
"fails": [
"number": "+44771232343"
"message": "hello world!",
"device": 1
"errors": {
"device": ["The selected device is invalid"],
}
]
}
}

如果失败了,这就成功了:

if (isset($result['response']['result']['fails'][0]['number']) === TRUE) {
     echo 'failed';
else{
     echo 'success';
}

这对成功有效:

if(isset($result['response']['result']['success'][0]['id']) === TRUE){
    echo 'success';
}else{
    echo 'failed';
}

谢谢 Michael,您的回复帮助我稍微修改了一下并想通了!