DDpusher.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 用Socket向DDpush服务器发送消息
  4. * 相关文档,请参考http://www.ddpush.net
  5. * @author Wang Wenbing<binny_w@qq.com>
  6. */
  7. class DDpusher {
  8. /* Socket resource */
  9. private $socket = null;
  10. /**
  11. * 构造函数
  12. * @param string $strHost
  13. * @param int $intPort
  14. * @throws Exception
  15. */
  16. public function __construct($strHost, $intPort = 9999) {
  17. $strHost = strval($strHost);
  18. $intPort = intval($intPort);
  19. if (empty($strHost) || !$intPort) {
  20. throw new Exception('Wrong strHost or Wrong intPort');
  21. } elseif (($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
  22. throw new Exception('Error at socket_create(): ' . socket_strerror(socket_last_error()));
  23. } elseif (socket_connect($this->socket, $strHost, $intPort) === false) {
  24. throw new Exception('Error at socket_connect(): ' . socket_strerror(socket_last_error()));
  25. }
  26. }
  27. /**
  28. * 生成32位的UUID,可以重写此函数
  29. * @param string $strUser
  30. * @return string(32)
  31. */
  32. private function getUUID($strUser) {
  33. $strUser = trim($strUser);
  34. //return strlen($strUser) ? md5($strUser) : false;
  35. return $strUser;
  36. }
  37. /**
  38. * 检查Version和Appid参数
  39. * @param int $intVersion
  40. * @param int $intAppid
  41. * @return boolen
  42. */
  43. private function checkVersionAndAppid($intVersion, $intAppid) {
  44. return ($intVersion > 0 && $intVersion < 256 && $intAppid > 0 && $intAppid < 256);
  45. }
  46. /**
  47. * 发送通知
  48. * @param string $strUser
  49. * @return boolean $blnRe
  50. */
  51. public function push0x10($strUUID, $intVersion = 1, $intAppid = 1) {
  52. $blnRe = false;
  53. $intVersion = intval($intVersion);
  54. $intAppid = intval($intAppid);
  55. if ($this->checkVersionAndAppid($intVersion, $intAppid) && $strUUID !== false && $this->socket) {
  56. $strBin = pack('CCCH32n', $intVersion, $intAppid, 16, $strUUID, 0);
  57. socket_write($this->socket, $strBin, strlen($strBin)) && $blnRe = (bindec(socket_read($this->socket, 1)) == 0);
  58. } else {
  59. throw new Exception('Error at push0x10()');
  60. }
  61. return $blnRe;
  62. }
  63. /**
  64. * 发送分类信息
  65. * @param string $strUser
  66. * @param string $strHex 16位长的16进制字符
  67. * @param int $intVersion
  68. * @param int $intAppid
  69. * @return boolen $blnRe
  70. */
  71. public function push0x11($strUUID, $strHex, $intVersion = 1, $intAppid = 1) {
  72. $blnRe = false;
  73. $intVersion = intval($intVersion);
  74. $intAppid = intval($intAppid);
  75. $strHex = trim($strHex);
  76. if ($this->checkVersionAndAppid($intVersion, $intAppid) && $strUUID !== false && $this->socket && strlen($strHex) == 16) {
  77. $strBin = pack('CCCH32nH16', $intVersion, $intAppid, 17, $strUUID, 8, $strHex);
  78. socket_write($this->socket, $strBin, strlen($strBin)) && $blnRe = (bindec(socket_read($this->socket, 1)) == 0);
  79. } else {
  80. throw new Exception('Error at push0x11()');
  81. }
  82. return $blnRe;
  83. }
  84. /**
  85. * 发送500字节以内的字符消息
  86. * @param string $strUser
  87. * @param string $strMsg 必须是utf8编码的字符
  88. * @param int $intVersion
  89. * @param int $intAppid
  90. * @return boolen $blnRe
  91. * @throws Exception
  92. */
  93. public function push0x20($strUUID, $strMsg, $intVersion = 1, $intAppid = 1) {
  94. $blnRe = false;
  95. $intVersion = intval($intVersion);
  96. $intAppid = intval($intAppid);
  97. // $strMsg = mb_convert_encoding($strMsg, 'utf8', 'gbk');
  98. $strMsg = trim($strMsg);
  99. $intLen = strlen($strMsg);
  100. $blnTemp = ($intLen > 0 && $intLen <= 500);
  101. if ($this->checkVersionAndAppid($intVersion, $intAppid) && $strUUID !== false && $this->socket && $blnTemp) {
  102. $strBin = pack('CCCH32nA' . $intLen, $intVersion, $intAppid, 32, $strUUID, $intLen, $strMsg);
  103. socket_write($this->socket, $strBin, strlen($strBin)) && $blnRe = (bindec(socket_read($this->socket, 1)) == 0);
  104. } else {
  105. throw new Exception('Error at push0x20()');
  106. }
  107. return $blnRe;
  108. }
  109. /**
  110. * 断开连接
  111. */
  112. public function __destruct() {
  113. if ($this->socket) {
  114. socket_close($this->socket);
  115. $this->socket = null;
  116. }
  117. }
  118. }
  119. //try {
  120. // $obj = new DDpusher('127.0.0.1');
  121. // //$obj->push0x10('99000316797317') && print('通知已发送<br />');
  122. // // $obj->push0x11('99000316797317', '0000000000000001') && print('分类已发送<br />');
  123. // $obj->push0x20('162434c1879fcc3d0c098d9a6af4da6a', '提示内容@http://www.baidu.com@3') && print('字符串消息已发送<br />');
  124. //} catch (Exception $ex) {
  125. // echo $ex->getMessage();
  126. //}