| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace common\components;
- class push
- {
- private $ghsUrl = 'https://fc-mp-42d1a66f-2aec-4fc5-9d92-4d176bf3d337.next.bspapp.com/ghs/push';
- private $hdUrl = 'https://fc-mp-027cce01-d39a-4ed2-8b09-1f4ff43ec945.next.bspapp.com/hd/push';
- private $url = '';
- private $secret = 'RIVATE_KEY_S^@VJDy7'; // 👈 与云函数中保持一致
- public function __construct($client) {
- if ($client == 'ghs') {
- $this->url = $this->ghsUrl;
- } else if ($client == 'hd') {
- $this->url = $this->hdUrl;
- } else {
- throw new \Exception('client not found');
- }
- }
- public function pushByCloud($cids, $title, $content, $payload = [])
- {
- $sign = md5($title . $content . $this->secret);
- $data = [
- 'cid' => is_array($cids) ? $cids : [$cids],
- 'title' => $title,
- 'content' => $content,
- 'payload' => $payload,
- 'force_notification' => true, // true: 无论是离线推送还是在线推送,都自创建通知栏消息
- 'sign' => $sign
- ];
- $ch = curl_init($this->url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, [
- 'Content-Type: application/json'
- ]);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- $res = curl_exec($ch);
- curl_close($ch);
- return json_decode($res, true);
- }
- }
|