使用smsgateway.me发送消息我使用默认代码和我自己的变量,这些变量工作正常。但是,有时SMS不发送,如果失败,我想将SMS存储在数据库中。 为此,我认为我需要$ result数组中的值,但我不知道如何开始。我尝试了许多不同的代码而没有任何结果。
任何人都可以告诉我如何获得如果发送确定 - >根据下面的代码和变量做某事/做其他事情? 非常感谢!
文档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"],
}
]
}
}
答案 0 :(得分:0)
如果失败,这就成功了:
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';
}
谢谢迈克尔,你的回复帮助我改变了一点并弄明白了!