php - twilio whatsapp api 响应状态不正确
问题描述
我正在使用 Twilio WhatsApp API。我有沙盒帐户,并且可以从 Twilio 向 WhatsApp 号码发送消息,但是我面临一个问题,即当消息未发送给收件人时,我从 API 获得的状态与我获得成功的状态相同.
这是我从 api 得到的响应,没有指定成功或失败。
{
"sid": "xxxxx",
"date_created": "Tue, 08 Jan 2019 09:42:38 +0000",
"date_updated": "Tue, 08 Jan 2019 09:42:38 +0000",
"date_sent": null,
"account_sid": "xxxxxx",
"to": "Whatsapp:xxxxxxx",
"from": "Whatsapp:+xxxxxxx",
"messaging_service_sid": null,
"body": "test",
"status": "queued",
"num_segments": "1",
"num_media": "0",
"direction": "outbound-api",
"api_version": "2010-04-01",
"price": null,
"price_unit": null,
"error_code": null,
"error_message": null,
"uri": "/2010-04-01/Accounts/xxxxxxxxxxxxxx/Messages/xxxxxxxxxxxxxxxxxx.json",
"subresource_uris": {
"media": "/2010-04-01/Accounts/xxxxxxxxxxxxxxxxxx/Messages/xxxxxxxxxxxxxxxxxxxx/Media.json"
}
}
我正在使用这个API。
如果需要,这是我的代码。
public function sendWhatsappMsg() {
if ( ! empty( $this->apiKey ) && $this->apiKey != null && $this->apiKey != '' ) {
if($this->apiKey == $_POST['apiKey']) {
try {
$to = $_POST['to'];
$msg = $_POST['msg'];
if ( empty( $to ) && empty( $msg ) ) {
return array( 'Error' => 'Phone number and message is required!' );
}
if ( empty( $to ) ) {
return array( 'Error' => 'Phone number is required!' );
}
if ( empty( $msg ) ) {
return array( 'Error' => 'Message is required!' );
}
if(!empty($to) && !empty($msg)){
$isVaid = $this->verifyPhoneNumber($to);
if($isVaid['valid'] == false){
return array( 'Error' => 'Phone number should be valid!' );
}else{
$twilio = new Client( $this->sid, $this->token );
$message = $twilio->messages
->create( "whatsapp:" . $isVaid['international_format'], // to
array(
"from" => "whatsapp:+14155238886",
"body" => $msg,
"statusCallback" => "http://adwtpoc.digitalgravity.ae/Api.php?method=sendWhatsappMsgCallback"
)
);
return (array('accountSid'=>$message->accountSid,'messageServiceSid'=>$message->messagingServiceSid,'sid'=>$message->sid,'status'=>$message->status));
}
}
} //catch exception
catch ( Exception $e ) {
echo 'Exception: ' . $e->getMessage();
}
}else{
header("HTTP/1.1 403 Forbidden" );
die('403 Forbidden!');
}
}else{
return array( 'Error' => 'No Api Key Defined!' );
}
}
解决方案
我已经对其进行了排序,我得到messageSid
了响应,所以我使用另一个 API 通过它的 sid 获取消息详细信息,我得到了我想要的响应。
这是获取消息详细信息的代码
public function getMessageStatus($messageSid){
// Initialize CURL:
$ch = curl_init('https://api.twilio.com/2010-04-01/Accounts/'.$this->AccountSid.'/Messages/'.$messageSid.'.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->AccountSid . ":" . $this->token);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
return json_decode($json, true);
}
推荐阅读
- python - 如何过滤django中的项目
- python - 如何识别数据框中的唯一项目?
- php - 谷歌驱动器php在下载前获取文件名
- python - 为什么它在 CentOS wx.html2 中不可用。WebView控件?
- android - Dagger - Hilt:我们是否需要用@AndroidEntryPoint 标记所有活动
- azure - 2021 年,导出 Azure 自动化 AzureRunAsConnection 用于本地调试的证书的最快/最简单方法是什么?
- python - 没有输出 tkinter
- c++ - 在 C++ 中处理大哈希表
- python - 我为我的网站制作了 Hashtag 系统,但是在使用了“'”或“;”等特殊字符后,它在 Django 中创建了#hashtag
- session - 如何在 Postman 中获取 Cookie?