使用txtlocal api检查消息的状态

时间:2016-03-03 03:18:36

标签: php

这是使用txtlocal

发送消息的代码
$message = urlencode($message);
    $data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
    $ch = curl_init('http://api.txtlocal.com/send/?');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch); // This is the result from the API
    echo $result;
    curl_close($ch);

但我怎样才能检查一些错误。我正在使用

echo $result;

打印输出结果

它给了我这样的东西

{"balance":94.5,"batch_id":309268453,"cost":0.5,"num_messages":1,"message":{"num_parts":1,"sender":"Audrey","content":"This is a test message from the PHP API script."},"receipt_url":"","custom":"","messages":[{"id":"1356746460","recipient":639000000000}],"status":"success"}

我可以获得成功消息的最后一个字符串。但如果输出不成功怎么办?

1 个答案:

答案 0 :(得分:0)

  

但如果输出不成功怎么办?

由你决定......

Their documentation声明:

  

每个回复都包含一个“状态”字段,可以是“成功”或“失败”。此字段可用于确定您的请求是否成功。

因此,如果您想检查它,可以执行like so

$result = json_decode($result);
if ($result->status == 'success') {
    echo 'It worked...';
} else {
    echo "It didn't work.";
}