smsapi.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * 短信平台API接口类
  4. */
  5. class smsapi {
  6. public $userid;
  7. public $statuscode;
  8. private $productid, $sms_key, $smsapi_url;
  9. /**
  10. *
  11. * 初始化接口类
  12. * @param int $userid 用户id
  13. * @param int $productid 产品id
  14. * @param string $sms_key 密钥
  15. */
  16. public function __construct($userid = '', $productid = '', $sms_key = '') {
  17. $this->smsapi_url = 'http://sms.phpip.com/api.php?';
  18. $this->userid = $userid;
  19. $this->productid = $productid;
  20. $this->sms_key = $sms_key;
  21. }
  22. /**
  23. *
  24. * 获取短信产品列表信息
  25. */
  26. public function get_price() {
  27. $this->param = array('op'=>'sms_get_productlist');
  28. $res = $this->pc_file_get_contents();
  29. return !empty($res) ? json_decode($res, 1) : array();
  30. }
  31. /**
  32. *
  33. * 获取短信产品购买地址
  34. */
  35. public function get_buyurl($productid = 0) {
  36. return 'http://sms.phpip.com/index.php?m=sms_service&c=center&a=buy&sms_pid='.$this->productid.'&productid='.$productid;
  37. }
  38. public function show_qf_url() {
  39. return $this->smsapi_url.'op=sms_qf_url&sms_uid='.$this->userid.'&sms_pid='.$this->productid.'&sms_key='.$this->sms_key;
  40. }
  41. /**
  42. * 获取短信剩余条数和限制短信发送ip
  43. */
  44. public function get_smsinfo() {
  45. $this->param = array('op'=>'sms_get_info');
  46. $res = $this->pc_file_get_contents();
  47. return !empty($res) ? json_decode($res, 1) : array();
  48. }
  49. /**
  50. * 获取充值记录
  51. */
  52. public function get_buyhistory() {
  53. $this->param = array('op'=>'sms_get_paylist');
  54. $res = $this->pc_file_get_contents();
  55. return !empty($res) ? json_decode($res, 1) : array();
  56. }
  57. /**
  58. * 获取消费记录
  59. * @param int $page 页码
  60. */
  61. public function get_payhistory($page=1) {
  62. $this->param = array('op'=>'sms_get_report','page'=>$page);
  63. $res = $this->pc_file_get_contents();
  64. return !empty($res) ? json_decode($res, 1) : array();
  65. }
  66. /**
  67. * 获取短信api帮助
  68. */
  69. public function get_sms_help() {
  70. $this->param = array('op'=>'sms_help','page'=>$page);
  71. $res = $this->pc_file_get_contents();
  72. return !empty($res) ? json_decode($res, 1) : array();
  73. }
  74. /**
  75. *
  76. * 批量发送短信
  77. * @param array $mobile 手机号码
  78. * @param string $content 短信内容
  79. * @param datetime $send_time 发送时间
  80. * @param string $charset 短信字符类型 gbk / utf-8
  81. * @param string $id_code 唯一值 、可用于验证码
  82. */
  83. public function send_sms($mobile='', $content='', $send_time='', $charset='gbk',$id_code = '',$tplid = '',$return_code = 0) {
  84. //短信发送状态
  85. $status = $this->_sms_status();
  86. if(is_array($mobile)){
  87. $mobile = implode(",", $mobile);
  88. }
  89. $content = safe_replace($content);
  90. if(strtolower($charset)=='utf-8') {
  91. $send_content = iconv('utf-8','gbk',$content);//官网IS GBK
  92. }else{
  93. $send_content = $content;
  94. }
  95. $send_time = strtotime($send_time);
  96. $data = array(
  97. 'sms_pid' => $this->productid,
  98. 'sms_passwd' => $this->sms_key,
  99. 'sms_uid' => $this->userid,
  100. 'charset' => CHARSET,
  101. 'send_txt' => urlencode($send_content),
  102. 'mobile' => $mobile,
  103. 'send_time' => $send_time,
  104. 'tplid' => $tplid,
  105. );
  106. $post = '';
  107. foreach($data as $k=>$v) {
  108. $post .= $k.'='.$v.'&';
  109. }
  110. $smsapi_senturl = $this->smsapi_url.'op=sms_service_new';
  111. $return = $this->_post($smsapi_senturl, 0, $post);
  112. $arr = explode('#',$return);
  113. $this->statuscode = $arr[0];
  114. //增加到本地数据库
  115. $sms_report_db = pc_base::load_model('sms_report_model');
  116. $send_userid = param::get_cookie('_userid') ? intval(param::get_cookie('_userid')) : 0;
  117. $ip = ip();
  118. $new_content = $content;
  119. if(isset($this->statuscode)) {
  120. $sms_report_db->insert(array('mobile'=>$mobile,'posttime'=>SYS_TIME,'id_code'=>$id_code,'send_userid'=>$send_userid,'status'=>$this->statuscode,'msg'=>$new_content,'return_id'=>$return,'ip'=>$ip));
  121. } else {
  122. $sms_report_db->insert(array('mobile'=>$mobile,'posttime'=>SYS_TIME,'send_userid'=>$send_userid,'status'=>'-2','msg'=>$new_content,'ip'=>$ip));
  123. }
  124. if($this->statuscode==0) {
  125. $barr = explode(':',$arr[1]);
  126. if($barr[0]=='KEY') {
  127. return '短信已提交,请等待审批!审批时间为:9:00-18:00。 法定假日不审批!如需帮助,请联系phpcms.cn官网!';
  128. }
  129. }
  130. //end
  131. if($return_code) {
  132. return $arr[0];
  133. } else {
  134. return isset($status[$arr[0]]) ? $status[$arr[0]] : $arr[0];
  135. }
  136. }
  137. /**
  138. *
  139. * 获取远程内容
  140. * @param $timeout 超时时间
  141. */
  142. public function pc_file_get_contents($timeout=30) {
  143. $this->setting = array(
  144. 'sms_uid'=>$this->userid,
  145. 'sms_pid'=>$this->productid,
  146. 'sms_passwd'=>$this->sms_key,
  147. );
  148. $this->param = array_merge($this->param, $this->setting);
  149. $url = $this->smsapi_url.http_build_query($this->param);
  150. $stream = stream_context_create(array('http' => array('timeout' => $timeout)));
  151. return @file_get_contents($url, 0, $stream);
  152. }
  153. /**
  154. * post数据
  155. * @param string $url post的url
  156. * @param int $limit 返回的数据的长度
  157. * @param string $post post数据,字符串形式username='dalarge'&password='123456'
  158. * @param string $cookie 模拟 cookie,字符串形式username='dalarge'&password='123456'
  159. * @param string $ip ip地址
  160. * @param int $timeout 连接超时时间
  161. * @param bool $block 是否为阻塞模式
  162. * @return string 返回字符串
  163. */
  164. private function _post($url, $limit = 0, $post = '', $cookie = '', $ip = '', $timeout = 30, $block = true) {
  165. $return = '';
  166. $matches = parse_url($url);
  167. $host = $matches['host'];
  168. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  169. $port = !empty($matches['port']) ? $matches['port'] : 80;
  170. $siteurl = $this->_get_url();
  171. if($post) {
  172. $out = "POST $path HTTP/1.1\r\n";
  173. $out .= "Accept: */*\r\n";
  174. $out .= "Referer: ".$siteurl."\r\n";
  175. $out .= "Accept-Language: zh-cn\r\n";
  176. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  177. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  178. $out .= "Host: $host\r\n" ;
  179. $out .= 'Content-Length: '.strlen($post)."\r\n" ;
  180. $out .= "Connection: Close\r\n" ;
  181. $out .= "Cache-Control: no-cache\r\n" ;
  182. $out .= "Cookie: $cookie\r\n\r\n" ;
  183. $out .= $post ;
  184. } else {
  185. $out = "GET $path HTTP/1.1\r\n";
  186. $out .= "Accept: */*\r\n";
  187. $out .= "Referer: ".$siteurl."\r\n";
  188. $out .= "Accept-Language: zh-cn\r\n";
  189. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  190. $out .= "Host: $host\r\n";
  191. $out .= "Connection: Close\r\n";
  192. $out .= "Cookie: $cookie\r\n\r\n";
  193. }
  194. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  195. if(!$fp) return '';
  196. stream_set_blocking($fp, $block);
  197. stream_set_timeout($fp, $timeout);
  198. @fwrite($fp, $out);
  199. $status = stream_get_meta_data($fp);
  200. if($status['timed_out']) return '';
  201. while (!feof($fp)) {
  202. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) break;
  203. }
  204. $stop = false;
  205. while(!feof($fp) && !$stop) {
  206. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  207. $return .= $data;
  208. if($limit) {
  209. $limit -= strlen($data);
  210. $stop = $limit <= 0;
  211. }
  212. }
  213. @fclose($fp);
  214. //部分虚拟主机返回数值有误,暂不确定原因,过滤返回数据格式
  215. $return_arr = explode("\n", $return);
  216. if(isset($return_arr[1])) {
  217. $return = trim($return_arr[1]);
  218. }
  219. unset($return_arr);
  220. return $return;
  221. }
  222. /**
  223. * 获取当前页面完整URL地址
  224. */
  225. private function _get_url() {
  226. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  227. $php_self = $_SERVER['PHP_SELF'] ? $this->_safe_replace($_SERVER['PHP_SELF']) : $this->_safe_replace($_SERVER['SCRIPT_NAME']);
  228. $path_info = isset($_SERVER['PATH_INFO']) ? $this->_safe_replace($_SERVER['PATH_INFO']) : '';
  229. $relate_url = isset($_SERVER['REQUEST_URI']) ? $this->_safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$this->_safe_replace($_SERVER['QUERY_STRING']) : $path_info);
  230. return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  231. }
  232. /**
  233. * 安全过滤函数
  234. *
  235. * @param $string
  236. * @return string
  237. */
  238. private function _safe_replace($string) {
  239. $string = str_replace('%20','',$string);
  240. $string = str_replace('%27','',$string);
  241. $string = str_replace('%2527','',$string);
  242. $string = str_replace('*','',$string);
  243. $string = str_replace('"','&quot;',$string);
  244. $string = str_replace("'",'',$string);
  245. $string = str_replace('"','',$string);
  246. $string = str_replace(';','',$string);
  247. $string = str_replace('<','&lt;',$string);
  248. $string = str_replace('>','&gt;',$string);
  249. $string = str_replace("{",'',$string);
  250. $string = str_replace('}','',$string);
  251. $string = str_replace('\\','',$string);
  252. return $string;
  253. }
  254. /**
  255. *
  256. * 接口短信状态
  257. */
  258. private function _sms_status() {
  259. pc_base::load_app_func('global','sms');
  260. return sms_status(0,1);
  261. }
  262. }
  263. ?>