php DEMO

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

  1. <?php
  2. header("Content-Type:text/html;charset=utf-8");
  3. $apikey = "xxxxxxxxxxx"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
  4. $mobile = "xxxxxxxxxxx"; //请用自己的手机号代替
  5. $text="【云片网】您的验证码是1234";
  6. $ch = curl_init();
  7. /* 设置验证方式 */
  8. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
  9. 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  10. /* 设置返回结果为流 */
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  12. /* 设置超时时间*/
  13. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  14. /* 设置通信方式 */
  15. curl_setopt($ch, CURLOPT_POST, 1);
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  17. // 取得用户信息
  18. $json_data = get_user($ch,$apikey);
  19. $array = json_decode($json_data,true);
  20. echo '<pre>';print_r($array);
  21. // 发送短信
  22. $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
  23. $json_data = send($ch,$data);
  24. $array = json_decode($json_data,true);
  25. echo '<pre>';print_r($array);
  26. // 发送模板短信
  27. // 需要对value进行编码
  28. $data = array('tpl_id' => '1', 'tpl_value' => ('#code#').
  29. '='.urlencode('1234').
  30. '&'.urlencode('#company#').
  31. '='.urlencode('欢乐行'), 'apikey' => $apikey, 'mobile' => $mobile);
  32. print_r ($data);
  33. $json_data = tpl_send($ch,$data);
  34. $array = json_decode($json_data,true);
  35. echo '<pre>';print_r($array);
  36. // 发送语音验证码
  37. $data=array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);
  38. $json_data =voice_send($ch,$data);
  39. $array = json_decode($json_data,true);
  40. echo '<pre>';print_r($array);
  41. // 发送语音通知,务必要报备好模板
  42. /*
  43. 模板: 课程#name#在#time#开始。 最终发送结果: 课程深度学习在14:00开始
  44. */
  45. $tpl_id = '123456';//你自己后台报备的模板id
  46. $tpl_value = urlencode('name=深度学习&time=14:00');
  47. $data = array('tpl_id'=>$tpl_id,'tpl_value'=>$tpl_value,'apikey'=>$apikey,'mobile'=>$mobile);
  48. $json_data = notify_send($ch,$data);
  49. $array = json_decode($json_data,true);
  50. print_r($array);
  51. curl_close($ch);
  52. /************************************************************************************/
  53. //获得账户
  54. function get_user($ch,$apikey){
  55. curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
  57. $result = curl_exec($ch);
  58. $error = curl_error($ch);
  59. checkErr($result,$error);
  60. return $result;
  61. }
  62. function send($ch,$data){
  63. curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
  64. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  65. $result = curl_exec($ch);
  66. $error = curl_error($ch);
  67. checkErr($result,$error);
  68. return $result;
  69. }
  70. function tpl_send($ch,$data){
  71. curl_setopt ($ch, CURLOPT_URL,
  72. 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  74. $result = curl_exec($ch);
  75. $error = curl_error($ch);
  76. checkErr($result,$error);
  77. return $result;
  78. }
  79. function voice_send($ch,$data){
  80. curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
  81. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  82. $result = curl_exec($ch);
  83. $error = curl_error($ch);
  84. checkErr($result,$error);
  85. return $result;
  86. }
  87. function notify_send($ch,$data){
  88. curl_setopt ($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
  89. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  90. $result = curl_exec($ch);
  91. $error = curl_error($ch);
  92. checkErr($result,$error);
  93. return $result;
  94. }
  95. function checkErr($result,$error) {
  96. if($result === false)
  97. {
  98. echo 'Curl error: ' . $error;
  99. }
  100. else
  101. {
  102. //echo '操作完成没有任何错误';
  103. }
  104. }
  105. ?>