php - 如何通过 curl 'CURLOPT_POSTFIELDS' 动态传递 PHP 变量
问题描述
我想动态地将电话号码(toNumber 变量)CURLOPT_POSTFIELDS 发送到字段,如何将 CURLOPT_POSTFIELDS 中的 $toNumber 传递到:“”字段?进阶谢谢.....
$mobileNumber = "002233245"; //its example phone number
$prefixCode = "880";
$toNumber = $prefixCode.$mobileNumber;
//now sent message
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sms.to/v1/sms/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>'{"body": "Dear customer, Your application success.","to": "Here_I_want_to_use_dynamically_phoneNumber_variable this $toNumber here","sender_id": "Bank","callback_url": "mydomain.com"}',
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer code",
"Accept: application/json",
"Content-Type: application/json"
),
));
解决方案
使用适当的数据在普通数组中构建发布数据...
$postData = [ "body" => "Dear customer, Your application success.",
"to" => $toNumber,
"sender_id" => "Bank",
"callback_url" => "mydomain.com"
];
将json_encode
d 字符串传递给 post 字段(更新此行)...
CURLOPT_POSTFIELDS => json_encode($postData),
推荐阅读
- r - 在 R 中,并行计算包含 NA 的距离矩阵 - 不是 amap::Dist!在窗户上
- html - Angular 5按钮在输入键按下时提交
- kaa - 使用服务器 REST API 激活 KAA 配置
- javascript - Web 应用程序的异步 Ajax 脚本处理
- javascript - 如何使用 ics.js 将文件作为附件添加到 .ics 文件?
- node.js - npm 错误!代码 UNABLE_TO_VERIFY_LEAF_SIGNATURE
- php - 用 2 个数组创建对,这样就没有重复的对?
- java - 无法从控制器访问主干模型属性
- javascript - 如何通过过渡将搜索栏从中心扩展到两侧
- jquery - jquery addClass 和 removeClass 在 IE 11 浏览器中不起作用?