首页 > 解决方案 > 如何通过 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"
 ),
));

标签: phpapicurl

解决方案


使用适当的数据在普通数组中构建发布数据...

$postData = [ "body" => "Dear customer, Your application success.",
    "to" => $toNumber,
    "sender_id" => "Bank",
    "callback_url" => "mydomain.com"
];

json_encoded 字符串传递给 post 字段(更新此行)...

CURLOPT_POSTFIELDS => json_encode($postData),

推荐阅读