【问题标题】:smsgateway.me store message status in php variablesmsgateway.me 将消息状态存储在 php 变量中
【发布时间】:2017-11-15 10:22:08
【问题描述】:

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

任何人都可以告诉我如何获得一个 if sent ok -> 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(sizeof($result['result']['success'])==0){echo'failed'}else{ //成功的东西 }
  • 您好迈克尔,感谢您的快速回复。 Thise 代码总是显示失败,我将 //success 的内容更改为 echo 'success',但总是在成功和失败时打印失败。有什么想法吗?
  • 试试 print_r($result['result']['success']) 看看你得到了什么,问题可能是有 2 个成功键,一个是帖子的成功状态,另一个是另一个是响应中的成功数据

标签: php arrays sms gateway sms-gateway


【解决方案1】:

如果失败,这会成功:

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';
}

谢谢迈克尔,你的回复帮我改了一下,搞明白了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2021-06-22
    • 2014-03-24
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多