php - 在codeigniter中发送短信
问题描述
当我发送短信响应总是在邮递员中显示一个空的 JsonNode
我的代码是
function sendmessage(){
$mobileNo=$this->input->post('mobile');
$channel='';
$pass='';
$password=base64_encode($pass);
$message='Hello, This is a test message';
$postData=array([
'channel' =>array(
'channel' => $channel,
'password' => $password,
),
'messages'=>array(
'text'=>$message,
'msisdn'=>$mobileNo,
"source"=>"littlebloom",
)
]);
$dataread=json_encode($postData);
/* API URL*/
$url="https://secure-gw.fasthub.co.tz/fasthub/messaging/json/api";
/* init the resource */
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $dataread,
));
/* Ignore SSL certificate verification */
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
/* get response */
$output = curl_exec($ch);
/* Print error if any */
if(curl_errno($ch))
{
$array=array( 'error'=> curl_error($ch));
echo json_encode($array);
}
else{
$array = array(
"message" => $output
);
}
curl_close($ch);
//MESSAGE CODE END
echo $output;
}
这是我在 codeigniter 中发送短信的功能。客户端提供 FastHub 短信服务提供的 url,并提供下面给出的 Json 请求代码。我在邮递员中使用它,当我在给定的 url 使用这个 json 作为行时,它给出了成功的 msg,但是当我使用我的 codeigniter 代码时,它显示错误“Empty JsonNode”。我如何在我的代码中定义这个 json。
短信服务提供商是 FastHub
它的json
要求就像
"channel":{
"channel":113474,
"password":"Base64(sha-256(plain_password))"
},
"messages":[
{
"text":"test message",
"msisdn":"255754088816",
"source":"TEST"
}
]
}
如何在 $postData 中发送这种类型的 json...
解决方案
尝试将内容类型设置为 JSON 将其添加到 $ch 变量下。在其他 setopts 之后或之前的某个地方
$headers = array( "Content-Type: application/json", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
推荐阅读
- python-3.x - 无法消除 Python for 循环中的条目
- excel - SAS Excel 插件
- sql-server - 如何从链接服务器查询 Azure Master DB
- node.js - 基于所需的 Mongoose 子文档验证
- sql-server - SQL 按值排序,但将来自同一用户的数据保存在一起
- sql - 如何将字符串值转换为 unpivot 语句中的列?
- ionic-framework - 选择没有 Image-Picker 插件 ionic 的多个图像
- spring-boot - Spring Boot 控制器路径在 websphere 9 上给出 404
- android - 如何在手机中使用 NFC 读卡器接收付款
- json - 使用 jq 解析 JIRA Insights API JSON