WebPush.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Lettered\Support;
  3. class WebPush
  4. {
  5. /**
  6. * @var string restUrl
  7. */
  8. protected $restUrl = 'http://webpush.lettered.cn/publish';
  9. private $appKey = null;
  10. private $channel = null;
  11. protected static $instance = null;
  12. /**
  13. * WebPush constructor.
  14. * @param string $key
  15. * @param string $channel
  16. */
  17. private function __construct($key, $channel)
  18. {
  19. $this->appKey = $key;
  20. $this->channel = $channel;
  21. }
  22. /**
  23. * 单例初始化
  24. *
  25. * @author 许祖兴 < zuxing.xu@lettered.cn>
  26. * @date 2020/3/23 16:05
  27. *
  28. * @param $key
  29. * @param $channel
  30. * @return Goeasy|null
  31. */
  32. public static function init($key, $channel)
  33. {
  34. if (is_null(self::$instance)){
  35. self::$instance = new self($key, $channel);
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * 消息推送
  41. *
  42. * @author 许祖兴 < zuxing.xu@lettered.cn>
  43. * @date 2020/3/23 16:01
  44. *
  45. * @param array $content 消息内容
  46. * @return bool|string
  47. */
  48. public function send($content = [])
  49. {
  50. return $this->curlPost($this->restUrl, [
  51. 'token' => $this->appKey,
  52. 'channel' => $this->channel,
  53. 'content' => json_encode($content)
  54. ]);
  55. }
  56. /**
  57. * curlGet
  58. *
  59. * @author 许祖兴 < zuxing.xu@lettered.cn>
  60. * @date 2020/3/19 18:48
  61. *
  62. * @param string $url 请求地址
  63. * @param array $options 选填
  64. * @return bool|string
  65. */
  66. private static function curlGet($url = '', $options = [])
  67. {
  68. $ch = curl_init($url);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  71. if (!empty($options)) {
  72. curl_setopt_array($ch, $options);
  73. }
  74. //https请求 不验证证书和host
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  77. $data = curl_exec($ch);
  78. curl_close($ch);
  79. return $data;
  80. }
  81. /**
  82. * curlPost
  83. *
  84. * @author 许祖兴 < zuxing.xu@lettered.cn>
  85. * @date 2020/3/19 18:48
  86. *
  87. * @param string $url 请求url
  88. * @param string|array $postData 请求数据
  89. * @param array $options 选填
  90. * @return bool|string
  91. */
  92. private function curlPost($url = '', $postData = '', $options = [])
  93. {
  94. if (is_array($postData)) {
  95. $postData = http_build_query($postData);
  96. }
  97. $ch = curl_init();
  98. curl_setopt($ch, CURLOPT_URL, $url);
  99. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  100. curl_setopt($ch, CURLOPT_POST, 1);
  101. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  102. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  103. if (!empty($options)) {
  104. curl_setopt_array($ch, $options);
  105. }
  106. //https请求 不验证证书和host
  107. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  108. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  109. // 设置请求头
  110. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  111. 'Content-type:application/x-www-form-urlencoded'
  112. ]);
  113. $data = curl_exec($ch);
  114. curl_close($ch);
  115. return $data;
  116. }
  117. }