人人商城

SMSClient.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. class SMSClient
  3. {
  4. /**
  5. * 网关地址
  6. */
  7. public $url;
  8. /**
  9. * 序列号,请通过亿美销售人员获取
  10. */
  11. public $serialNumber;
  12. /**
  13. * 密码,请通过亿美销售人员获取
  14. */
  15. public $password;
  16. /**
  17. * 登录后所持有的SESSION KEY,即可通过login方法时创建
  18. */
  19. public $sessionKey;
  20. /**
  21. * webservice客户端
  22. */
  23. public $soap;
  24. /**
  25. * 默认命名空间
  26. */
  27. public $namespace = 'http://sdkhttp.eucp.b2m.cn/';
  28. /**
  29. * 往外发送的内容的编码,默认为 GBK
  30. */
  31. public $outgoingEncoding = 'GBK';
  32. /**
  33. * 往外发送的内容的编码,默认为 GBK
  34. */
  35. public $incomingEncoding = '';
  36. /**
  37. * @param string $url 网关地址
  38. * @param string $serialNumber 序列号,请通过亿美销售人员获取
  39. * @param string $password 密码,请通过亿美销售人员获取
  40. * @param string $sessionKey 登录后所持有的SESSION KEY,即可通过login方法时创建
  41. *
  42. * @param string $proxyhost 可选,代理服务器地址,默认为 false ,则不使用代理服务器
  43. * @param string $proxyport 可选,代理服务器端口,默认为 false
  44. * @param string $proxyusername 可选,代理服务器用户名,默认为 false
  45. * @param string $proxypassword 可选,代理服务器密码,默认为 false
  46. * @param string $timeout 连接超时时间,默认0,为不超时
  47. * @param string $response_timeout 信息返回超时时间,默认30
  48. *
  49. *
  50. */
  51. public function SMSClient($url, $serialNumber, $password, $sessionKey = '', $proxyhost = false, $proxyport = false, $proxyusername = false, $proxypassword = false, $timeout = 0, $response_timeout = 30)
  52. {
  53. $this->url = $url;
  54. $this->serialNumber = $serialNumber;
  55. $this->password = $password;
  56. if ($sessionKey != '') {
  57. $this->sessionKey = $sessionKey;
  58. }
  59. $this->soap = new nusoap_client($url, false, $proxyhost, $proxyport, $proxyusername, $proxypassword, $timeout, $response_timeout);
  60. $this->soap->soap_defencoding = $this->outgoingEncoding;
  61. $this->soap->decode_utf8 = false;
  62. }
  63. /**
  64. * 设置发送内容 的字符编码
  65. * @param string $outgoingEncoding 发送内容字符集编码
  66. */
  67. public function setOutgoingEncoding($outgoingEncoding)
  68. {
  69. $this->outgoingEncoding = $outgoingEncoding;
  70. $this->soap->soap_defencoding = $this->outgoingEncoding;
  71. }
  72. /**
  73. * 设置接收内容 的字符编码
  74. * @param string $incomingEncoding 接收内容字符集编码
  75. */
  76. public function setIncomingEncoding($incomingEncoding)
  77. {
  78. $this->incomingEncoding = $incomingEncoding;
  79. $this->soap->xml_encoding = $this->incomingEncoding;
  80. }
  81. public function setNameSpace($ns)
  82. {
  83. $this->namespace = $ns;
  84. }
  85. public function getSessionKey()
  86. {
  87. return $this->sessionKey;
  88. }
  89. public function getError()
  90. {
  91. return $this->soap->getError();
  92. }
  93. /**
  94. *
  95. * 指定一个 session key 并 进行登录操作
  96. *
  97. * @param string $sessionKey 指定一个session key
  98. * @return int 操作结果状态码
  99. *
  100. * 代码如:
  101. *
  102. * $sessionKey = $client->generateKey(); //产生随机6位数 session key
  103. *
  104. * if ($client->login($sessionKey)==0)
  105. * {
  106. * //登录成功,并且做保存 $sessionKey 的操作,用于以后相关操作的使用
  107. * }else{
  108. * //登录失败处理
  109. * }
  110. *
  111. *
  112. */
  113. public function login($sessionKey = '')
  114. {
  115. if ($sessionKey != '') {
  116. $this->sessionKey = $sessionKey;
  117. }
  118. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey, 'arg2' => $this->password);
  119. $result = $this->soap->call('registEx', $params, $this->namespace);
  120. return $result;
  121. }
  122. /**
  123. * 注销操作 (注:此方法必须为已登录状态下方可操作)
  124. *
  125. * @return int 操作结果状态码
  126. *
  127. * 之前保存的sessionKey将被作废
  128. * 如需要,可重新login
  129. */
  130. public function logout()
  131. {
  132. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  133. print_r($params);
  134. $result = $this->soap->call('logout', $params, $this->namespace);
  135. return $result;
  136. }
  137. /**
  138. * 获取版本信息
  139. * @return string 版本信息
  140. */
  141. public function getVersion()
  142. {
  143. $result = $this->soap->call('getVersion', array(), $this->namespace);
  144. return $result;
  145. }
  146. /**
  147. * 短信发送 (注:此方法必须为已登录状态下方可操作)
  148. *
  149. * @param array $mobiles 手机号, 如 array('159xxxxxxxx'),如果需要多个手机号群发,如 array('159xxxxxxxx','159xxxxxxx2')
  150. * @param string $content 短信内容
  151. * @param string $sendTime 定时发送时间,格式为 yyyymmddHHiiss, 即为 年年年年月月日日时时分分秒秒,例如:20090504111010 代表2009年5月4日 11时10分10秒
  152. * 如果不需要定时发送,请为'' (默认)
  153. *
  154. * @param string $addSerial 扩展号, 默认为 ''
  155. * @param string $charset 内容字符集, 默认GBK
  156. * @param int $priority 优先级, 默认5
  157. * @return int 操作结果状态码
  158. */
  159. public function sendSMS($mobiles = array(), $content, $sendTime = '', $addSerial = '', $charset = 'GBK', $priority = 5)
  160. {
  161. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey, 'arg2' => $sendTime, 'arg4' => $content, 'arg5' => $addSerial, 'arg6' => $charset, 'arg7' => $priority);
  162. foreach ($mobiles as $mobile) {
  163. array_push($params, new soapval('arg3', false, $mobile));
  164. }
  165. $result = $this->soap->call('sendSMS', $params, $this->namespace);
  166. return $result;
  167. }
  168. /**
  169. * 余额查询 (注:此方法必须为已登录状态下方可操作)
  170. * @return double 余额
  171. */
  172. public function getBalance()
  173. {
  174. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  175. $result = $this->soap->call('getBalance', $params, $this->namespace);
  176. return $result;
  177. }
  178. /**
  179. * 取消短信转发 (注:此方法必须为已登录状态下方可操作)
  180. * @return int 操作结果状态码
  181. */
  182. public function cancelMOForward()
  183. {
  184. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  185. $result = $this->soap->call('cancelMOForward', $params, $this->namespace);
  186. return $result;
  187. }
  188. /**
  189. * 短信充值 (注:此方法必须为已登录状态下方可操作)
  190. * @param string $cardId [充值卡卡号]
  191. * @param string $cardPass [密码]
  192. * @return int 操作结果状态码
  193. *
  194. * 请通过亿美销售人员获取 [充值卡卡号]长度为20内 [密码]长度为6
  195. */
  196. public function chargeUp($cardId, $cardPass)
  197. {
  198. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey, 'arg2' => $cardId, 'arg3' => $cardPass);
  199. $result = $this->soap->call('chargeUp', $params, $this->namespace);
  200. return $result;
  201. }
  202. /**
  203. * 查询单条费用 (注:此方法必须为已登录状态下方可操作)
  204. * @return double 单条费用
  205. */
  206. public function getEachFee()
  207. {
  208. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  209. $result = $this->soap->call('getEachFee', $params, $this->namespace);
  210. return $result;
  211. }
  212. /**
  213. * 得到上行短信 (注:此方法必须为已登录状态下方可操作)
  214. *
  215. * @return array 上行短信列表, 每个元素是Mo对象, Mo对象内容参考最下面
  216. *
  217. *
  218. * 如:
  219. *
  220. * $moResult = $client->getMO();
  221. * echo "返回数量:".count($moResult);
  222. * foreach($moResult as $mo)
  223. * {
  224. * //$mo 是位于 Client.php 里的 Mo 对象
  225. * echo "发送者附加码:".$mo->getAddSerial();
  226. * echo "接收者附加码:".$mo->getAddSerialRev();
  227. * echo "通道号:".$mo->getChannelnumber();
  228. * echo "手机号:".$mo->getMobileNumber();
  229. * echo "发送时间:".$mo->getSentTime();
  230. * echo "短信内容:".$mo->getSmsContent();
  231. * }
  232. *
  233. *
  234. */
  235. public function getMO()
  236. {
  237. $ret = array();
  238. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  239. $result = $this->soap->call('getMO', $params, $this->namespace);
  240. if (is_array($result) && 0 < count($result)) {
  241. if (is_array($result[0])) {
  242. foreach ($result as $moArray) {
  243. $ret[] = new Mo($moArray);
  244. }
  245. }
  246. else {
  247. $ret[] = new Mo($result);
  248. }
  249. }
  250. return $ret;
  251. }
  252. /**
  253. * 得到状态报告 (注:此方法必须为已登录状态下方可操作)
  254. * @return array 状态报告列表, 一次最多取5个
  255. */
  256. public function getReport()
  257. {
  258. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  259. $result = $this->soap->call('getReport', $params, $this->namespace);
  260. return $result;
  261. }
  262. /**
  263. * 企业注册 [邮政编码]长度为6 其它参数长度为20以内
  264. *
  265. * @param string $eName 企业名称
  266. * @param string $linkMan 联系人姓名
  267. * @param string $phoneNum 联系电话
  268. * @param string $mobile 联系手机号码
  269. * @param string $email 联系电子邮件
  270. * @param string $fax 传真号码
  271. * @param string $address 联系地址
  272. * @param string $postcode 邮政编码
  273. *
  274. * @return int 操作结果状态码
  275. *
  276. */
  277. public function registDetailInfo($eName, $linkMan, $phoneNum, $mobile, $email, $fax, $address, $postcode)
  278. {
  279. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey, 'arg2' => $eName, 'arg3' => $linkMan, 'arg4' => $phoneNum, 'arg5' => $mobile, 'arg6' => $email, 'arg7' => $fax, 'arg8' => $address, 'arg9' => $postcode);
  280. $result = $this->soap->call('registDetailInfo', $params, $this->namespace);
  281. return $result;
  282. }
  283. /**
  284. * 修改密码 (注:此方法必须为已登录状态下方可操作)
  285. * @param string $newPassword 新密码
  286. * @return int 操作结果状态码
  287. */
  288. public function updatePassword($newPassword)
  289. {
  290. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey, 'arg2' => $this->password, 'arg3' => $newPassword);
  291. $result = $this->soap->call('serialPwdUpd', $params, $this->namespace);
  292. return $result;
  293. }
  294. /**
  295. *
  296. * 短信转发
  297. * @param string $forwardMobile 转发的手机号码
  298. * @return int 操作结果状态码
  299. *
  300. */
  301. public function setMOForward($forwardMobile)
  302. {
  303. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey, 'arg2' => $forwardMobile);
  304. $result = $this->soap->call('setMOForward', $params, $this->namespace);
  305. return $result;
  306. }
  307. /**
  308. * 短信转发扩展
  309. * @param array $forwardMobiles 转发的手机号码列表, 如 array('159xxxxxxxx','159xxxxxxxx');
  310. * @return int 操作结果状态码
  311. */
  312. public function setMOForwardEx($forwardMobiles = array())
  313. {
  314. $params = array('arg0' => $this->serialNumber, 'arg1' => $this->sessionKey);
  315. foreach ($forwardMobiles as $mobile) {
  316. array_push($params, new soapval('arg2', false, $mobile));
  317. }
  318. $result = $this->soap->call('setMOForwardEx', $params, $this->namespace);
  319. return $result;
  320. }
  321. /**
  322. * 生成6位随机数
  323. */
  324. public function generateKey()
  325. {
  326. return rand(100000, 999999);
  327. }
  328. }
  329. class Mo
  330. {
  331. /**
  332. * 发送者附加码
  333. */
  334. public $addSerial;
  335. /**
  336. * 接收者附加码
  337. */
  338. public $addSerialRev;
  339. /**
  340. * 通道号
  341. */
  342. public $channelnumber;
  343. /**
  344. * 手机号
  345. */
  346. public $mobileNumber;
  347. /**
  348. * 发送时间
  349. */
  350. public $sentTime;
  351. /**
  352. * 短信内容
  353. */
  354. public $smsContent;
  355. public function Mo(&$ret = array())
  356. {
  357. $this->addSerial = $ret[addSerial];
  358. $this->addSerialRev = $ret[addSerialRev];
  359. $this->channelnumber = $ret[channelnumber];
  360. $this->mobileNumber = $ret[mobileNumber];
  361. $this->sentTime = $ret[sentTime];
  362. $this->smsContent = $ret[smsContent];
  363. }
  364. public function getAddSerial()
  365. {
  366. return $this->addSerial;
  367. }
  368. public function getAddSerialRev()
  369. {
  370. return $this->addSerialRev;
  371. }
  372. public function getChannelnumber()
  373. {
  374. return $this->channelnumber;
  375. }
  376. public function getMobileNumber()
  377. {
  378. return $this->mobileNumber;
  379. }
  380. public function getSentTime()
  381. {
  382. return $this->sentTime;
  383. }
  384. public function getSmsContent()
  385. {
  386. return $this->smsContent;
  387. }
  388. }
  389. require_once dirname(__FILE__) . '/nusoaplib/nusoap.php';
  390. ?>