SendSms.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. include_once("../untils/conn.php");// 引入数据库连接配置文件
  3. // 腾讯云短信发送 API 接口
  4. function sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign) {
  5. $timestamp= $_POST['time'] ;
  6. $time = date('Y-m-d H:i:s', $timestamp);
  7. // 短信模板中的变量及值,按照模板格式填写
  8. $params = array(
  9. '{1}' => $_POST['oderid'],
  10. '{2}' => $time,
  11. );
  12. // 生成当前时间戳
  13. $currentTimeStamp = time();
  14. $random = rand(1000000, 9999999); // 生成一个随机数
  15. // 按照腾讯云 API 要求构造请求参数
  16. $requestParms = array(
  17. 'tel' => array(
  18. 'nationcode' => '86',
  19. 'mobile' => $phoneNumber,
  20. ),
  21. 'sig' => hash("sha256", "appkey=$appKey&random=$random&time=$currentTimeStamp&mobile=$phoneNumber", false),
  22. 'tpl_id' => $templateId,
  23. 'params' => array_values($params),
  24. 'sign' => $smsSign,
  25. 'time' => $currentTimeStamp,
  26. 'extend' => '',
  27. 'ext' => ''
  28. );
  29. // 转为 JSON 格式
  30. $requestJson = json_encode($requestParms);
  31. // 发送请求
  32. $url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=$appId&random=$random";
  33. $ch = curl_init();
  34. curl_setopt($ch, CURLOPT_URL, $url);
  35. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  37. curl_setopt($ch, CURLOPT_POST, true);
  38. curl_setopt($ch, CURLOPT_POSTFIELDS, $requestJson);
  39. $result = curl_exec($ch);
  40. // if (curl_errno($ch)) {
  41. // echo 'Error:' . curl_error($ch);
  42. // } else {
  43. // echo $result;
  44. // }
  45. // curl_close($ch);
  46. // return $result;
  47. }
  48. $id = '1';
  49. $sql = "SELECT * FROM sms WHERE id =".$id;
  50. $result = mysqli_query($con, $sql);
  51. if ($result->num_rows > 0) {
  52. // 输出数据
  53. while($row = $result->fetch_assoc()) {
  54. // 获取每个字段的值
  55. $appId = $row["appid"];
  56. $appKey = $row["appkey"];
  57. $phoneNumber = $row["admin_phone"];
  58. $templateId = $row["te_id"];
  59. $smsSign = $row["sign"];
  60. }
  61. } else {
  62. // echo "未找到匹配的记录";
  63. }
  64. // 调用函数发送短信
  65. // sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);