SendSMSService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class SendSMSService extends Service{
  3. /**
  4. * url 为服务的url地址
  5. * query 为请求串
  6. */
  7. public function sock_post($url,$query){
  8. // if(ENVIRONMENT != 'product'){
  9. // return false;
  10. // }
  11. $data = "";
  12. $info=parse_url($url);
  13. $fp=fsockopen($info["host"],80,$errno,$errstr,30);
  14. if(!$fp){
  15. return $data;
  16. }
  17. $head="POST ".$info['path']." HTTP/1.0\r\n";
  18. $head.="Host: ".$info['host']."\r\n";
  19. $head.="Referer: http://".$info['host'].$info['path']."\r\n";
  20. $head.="Content-type: application/x-www-form-urlencoded\r\n";
  21. $head.="Content-Length: ".strlen(trim($query))."\r\n";
  22. $head.="\r\n";
  23. $head.=trim($query);
  24. $write=fputs($fp,$head);
  25. $header = "";
  26. while ($str = trim(fgets($fp,4096))) {
  27. $header.=$str;
  28. }
  29. while (!feof($fp)) {
  30. $data .= fgets($fp,4096);
  31. }
  32. return $data;
  33. }
  34. /**
  35. * 模板接口发短信
  36. * apikey 为云片分配的apikey
  37. * tpl_id 为模板id
  38. * tpl_value 为模板值
  39. * mobile 为接受短信的手机号
  40. */
  41. public function tpl_send_sms($tpl_id, $tpl_value, $mobile){
  42. $t_mobile = array('18521093629');
  43. if(ENVIRONMENT != 'product' && !in_array($mobile,$t_mobile)){
  44. return false;
  45. // $mobile = '18521093629';
  46. }
  47. //$apikey = '181e7c60a605e96d6a0166579cbb76a4';
  48. $apikey = 'a9a0eae716ebc9da2d13b1ed161ade7a';
  49. $url="http://yunpian.com/v1/sms/tpl_send.json";
  50. $encoded_tpl_value = urlencode("$tpl_value");
  51. $post_string="apikey=$apikey&tpl_id=$tpl_id&tpl_value=$encoded_tpl_value&mobile=$mobile";
  52. return $this->sock_post($url, $post_string);
  53. }
  54. /**
  55. * 普通接口发短信
  56. * apikey 为云片分配的apikey
  57. * text 为短信内容
  58. * mobile 为接受短信的手机号
  59. */
  60. public function send_sms($text, $mobile){
  61. $t_mobile = array('18521093629');
  62. if(ENVIRONMENT != 'product' && !in_array($mobile,$t_mobile)){
  63. return false;
  64. // $mobile = '18521093629';
  65. }
  66. //$apikey = '181e7c60a605e96d6a0166579cbb76a4';
  67. $apikey = 'a9a0eae716ebc9da2d13b1ed161ade7a';
  68. $url="http://yunpian.com/v1/sms/send.json";
  69. $encoded_text = urlencode($text);
  70. $post_string="apikey=$apikey&text=$encoded_text&mobile=$mobile";
  71. return $this->sock_post($url, $post_string);
  72. }
  73. }
  74. ?>