├── .gitignore ├── examples ├── jiguang.png ├── chatroom_examples.php ├── admin │ ├── admins_examples.php │ └── admin_register_examples.php ├── config.php.example ├── message │ ├── text_message_examples.php │ ├── retract_message_examples.php │ └── image_message_examples.php ├── resource_examples.php ├── user │ ├── blacklist_examples.php │ ├── user_register_examples.php │ ├── users_examples.php │ └── nodisturb_examples.php ├── friend_examples.php ├── report_example.php ├── group_examples.php └── sensitiveword_examples.php ├── autoload.php ├── src └── JMessage │ ├── IM │ ├── Admin.php │ ├── Resource.php │ ├── Blacklist.php │ ├── Friend.php │ ├── SensitiveWord.php │ ├── ChatRoom.php │ ├── Group.php │ ├── Report.php │ ├── Message.php │ └── User.php │ ├── JMessage.php │ ├── IM.php │ ├── Cross │ ├── Member.php │ ├── Nodisturb.php │ ├── Blacklist.php │ ├── Message.php │ └── Friend.php │ └── Http.php ├── composer.json ├── LICENSE ├── README.md └── docs ├── REPORT.md ├── CROSS.md └── GUIDE.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.phar 3 | composer.lock 4 | examples/config.php 5 | -------------------------------------------------------------------------------- /examples/jiguang.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jpush/jmessage-api-php-client/HEAD/examples/jiguang.png -------------------------------------------------------------------------------- /examples/chatroom_examples.php: -------------------------------------------------------------------------------- 1 | listAll(10); 8 | print_r($response); 9 | -------------------------------------------------------------------------------- /examples/admin/admins_examples.php: -------------------------------------------------------------------------------- 1 | listAll(10); 10 | print_r($response); 11 | -------------------------------------------------------------------------------- /examples/config.php.example: -------------------------------------------------------------------------------- 1 | 'admin', 9 | 'password' => 'password' 10 | ]; 11 | 12 | $response = $admin->register($info); 13 | 14 | print_r($response); 15 | -------------------------------------------------------------------------------- /examples/message/text_message_examples.php: -------------------------------------------------------------------------------- 1 | 'admin', 9 | 'type' => 'admin' 10 | ]; 11 | 12 | $target = [ 13 | 'id' => 'user_10', 14 | 'type' => 'single' 15 | ]; 16 | $msg = [ 17 | 'text' => 'Hello World' 18 | ]; 19 | $response = $message->sendText(1, $from, $target, $msg); 20 | print_r($response); 21 | 22 | -------------------------------------------------------------------------------- /examples/message/retract_message_examples.php: -------------------------------------------------------------------------------- 1 | 'admin', 9 | 'type' => 'admin' 10 | ]; 11 | 12 | $target = [ 13 | 'id' => 'user_10', 14 | 'type' => 'single' 15 | ]; 16 | $msg = [ 17 | 'text' => 'Hello World' 18 | ]; 19 | $response = $message->sendText(1, $from, $target, $msg); 20 | print_r($response); 21 | 22 | $msgid = $response['body']['msg_id']; 23 | 24 | $response = $message->retract($msgid, 'admin'); 25 | print_r($response); 26 | -------------------------------------------------------------------------------- /examples/resource_examples.php: -------------------------------------------------------------------------------- 1 | upload('image', $image); 11 | print_r($response); 12 | echo "\n"; 13 | 14 | echo "upload file: \n"; 15 | $response = $rescource->upload('file', $image); 16 | print_r($response); 17 | echo "\n"; 18 | 19 | $mediaId = $response['body']['media_id']; 20 | 21 | echo "download file: \n"; 22 | $response = $rescource->download($mediaId); 23 | print_r($response); 24 | echo "\n"; 25 | 26 | -------------------------------------------------------------------------------- /examples/user/blacklist_examples.php: -------------------------------------------------------------------------------- 1 | add($user, ['user_1']); 10 | print_r($response); 11 | echo "\n"; 12 | 13 | echo "get blacklists: \n"; 14 | $response = $blacklist->listAll($user); 15 | print_r($response); 16 | echo "\n"; 17 | 18 | echo "remove blacklist: \n"; 19 | $response = $blacklist->remove($user, ['user_1']); 20 | print_r($response); 21 | echo "\n"; 22 | 23 | echo "get blacklists: \n"; 24 | $response = $blacklist->listAll($user); 25 | print_r($response); 26 | echo "\n"; 27 | -------------------------------------------------------------------------------- /src/JMessage/IM/Admin.php: -------------------------------------------------------------------------------- 1 | post($uri, $body); 13 | return $response; 14 | } 15 | 16 | public function listAll($count, $start = 0) { 17 | $uri = self::BASE_URI; 18 | $query = [ 19 | 'start' => $start, 20 | 'count' => $count 21 | ]; 22 | $response = $this->get($uri, $query); 23 | return $response; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/JMessage/IM/Resource.php: -------------------------------------------------------------------------------- 1 | $mediaId ]; 12 | $response = $this->get($uri, $query); 13 | return $response; 14 | } 15 | 16 | public function upload($type, $path) { 17 | $uri = self::BASE_URI . '?' . http_build_query(['type' => $type]); 18 | $body = ['path' => $path]; 19 | $response = $this->getClient()->upload($uri, $body); 20 | return $response; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /examples/message/image_message_examples.php: -------------------------------------------------------------------------------- 1 | upload('image', $image); 13 | print_r($response); 14 | echo "\n"; 15 | 16 | $body = $response['body']; 17 | 18 | $from = [ 19 | 'id' => 'admin', 20 | 'type' => 'admin' 21 | ]; 22 | 23 | $target = [ 24 | 'id' => 'user_10', 25 | 'type' => 'single' 26 | ]; 27 | 28 | $msg = $body; 29 | 30 | $response = $message->sendImage(1, $from, $target, $msg); 31 | print_r($response); 32 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jiguang/jmessage", 3 | "type": "library", 4 | "version": "1.1.13", 5 | "description": "JMessage's officially supported PHP client library for accessing JMessage APIs.", 6 | "keywords": ["jiguang", "jmessage", "API Client"], 7 | "homepage": "https://github.com/jpush/jmessage-api-php-client", 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "JiGuang", 12 | "email": "support@jpush.cn", 13 | "homepage": "https://www.jiguang.cn" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.4", 18 | "ext-curl": "*" 19 | }, 20 | "autoload" : { 21 | "psr-4": {"JMessage\\": "src/JMessage/"} 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/JMessage/JMessage.php: -------------------------------------------------------------------------------- 1 | appKey = $appKey; 12 | $this->masterSecret = $masterSecret; 13 | $this->options = $options; 14 | } 15 | 16 | public function getAuth() { 17 | return $this->appKey . ':' . $this->masterSecret; 18 | } 19 | 20 | public function disableSsl() { 21 | if (isset($this->options['disable_ssl']) 22 | && (bool) $this->options['disable_ssl']) { 23 | return true; 24 | } 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/JMessage/IM.php: -------------------------------------------------------------------------------- 1 | client = Http::getInstance($client); 11 | } 12 | 13 | public function get($uri, array $query = []) { 14 | return $this->client->get($uri, $query); 15 | } 16 | 17 | public function post($uri, array $body = []) { 18 | return $this->client->post($uri, $body); 19 | } 20 | 21 | public function put($uri, array $body = []) { 22 | return $this->client->put($uri, $body); 23 | } 24 | 25 | public function del($uri, array $body = []) { 26 | return $this->client->delete($uri, $body); 27 | } 28 | 29 | protected function getClient() { 30 | return $this->client; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/JMessage/IM/Blacklist.php: -------------------------------------------------------------------------------- 1 | put($uri, $body); 13 | return $response; 14 | } 15 | 16 | public function remove($user, array $usernames) { 17 | $uri = self::BASE_URI . $user . '/blacklist'; 18 | $body = $usernames; 19 | $response = $this->del($uri, $body); 20 | return $response; 21 | } 22 | 23 | public function listAll($user) { 24 | $uri = self::BASE_URI . $user . '/blacklist'; 25 | $response = $this->get($uri); 26 | return $response; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /examples/user/user_register_examples.php: -------------------------------------------------------------------------------- 1 | 'user_0', 'password' => 'password'], 9 | ['username' => 'user_1', 'password' => 'password'], 10 | ['username' => 'user_2', 'password' => 'password'], 11 | ['username' => 'user_3', 'password' => 'password'], 12 | ['username' => 'user_4', 'password' => 'password'], 13 | ['username' => 'user_5', 'password' => 'password'], 14 | ['username' => 'user_6', 'password' => 'password'], 15 | ['username' => 'user_7', 'password' => 'password'], 16 | ['username' => 'user_8', 'password' => 'password'], 17 | ['username' => 'user_9', 'password' => 'password'], 18 | ['username' => 'user_10', 'password' => 'password'] 19 | ]; 20 | 21 | $response = $user->batchRegister($users); 22 | print_r($response); 23 | 24 | $username = 'username_20'; 25 | $password = 'password'; 26 | $response = $user->register($username, $password); 27 | print_r($response); 28 | -------------------------------------------------------------------------------- /examples/user/users_examples.php: -------------------------------------------------------------------------------- 1 | listAll(100); 12 | print_r($response['body']); 13 | echo "\n"; 14 | 15 | echo "get user info: \n"; 16 | $response = $user->show($username); 17 | print_r($response); 18 | echo "\n"; 19 | 20 | echo "update user info: \n"; 21 | $response = $user->update($username, ['nickname' => 'user_nickname_0', 'gender' => 2]); 22 | print_r($response); 23 | echo "\n"; 24 | 25 | echo "get user stat: \n"; 26 | $response = $user->stat($username); 27 | print_r($response); 28 | echo "\n"; 29 | 30 | echo "change user password: \n"; 31 | $response = $user->updatePassword($username, 'password_0'); 32 | print_r($response); 33 | echo "\n"; 34 | 35 | // echo "delete user: \n"; 36 | // $response = $user->delete('user_10'); 37 | // print_r($response); 38 | // echo "\n"; 39 | 40 | echo "get user's groups: \n"; 41 | $response = $user->groups($username); 42 | print_r($response); 43 | echo "\n"; 44 | -------------------------------------------------------------------------------- /src/JMessage/IM/Friend.php: -------------------------------------------------------------------------------- 1 | post($uri, $body); 13 | return $response; 14 | } 15 | 16 | public function remove($user, array $friends) { 17 | $uri = self::BASE_URI . $user . '/friends'; 18 | $body = $friends; 19 | $response = $this->del($uri, $body); 20 | return $response; 21 | } 22 | 23 | public function updateNotename($user, array $options) { 24 | $uri = self::BASE_URI . $user . '/friends'; 25 | $body = $options; 26 | $response = $this->put($uri, $body); 27 | return $response; 28 | } 29 | 30 | public function listAll($user) { 31 | $uri = self::BASE_URI . $user . '/friends'; 32 | $response = $this->get($uri); 33 | return $response; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/JMessage/Cross/Member.php: -------------------------------------------------------------------------------- 1 | $appKey, 12 | 'add' => $usernames 13 | ]]; 14 | return $this->update($gid, $body); 15 | } 16 | 17 | public function remove($gid, $appKey, array $usernames) { 18 | $body = [[ 19 | 'appkey' => $appKey, 20 | 'remove' => $usernames 21 | ]]; 22 | return $this->update($gid, $body); 23 | } 24 | 25 | public function update($gid, array $options) { 26 | $uri = self::BASE_URI . $gid . '/members'; 27 | $body = $options; 28 | $response = $this->post($uri, $body); 29 | return $response; 30 | } 31 | 32 | public function listAll($gid) { 33 | $uri = self::BASE_URI . $gid . '/members'; 34 | $response = $this->get($uri); 35 | return $response; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 极光推送/IM JPush/JMesage 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /examples/friend_examples.php: -------------------------------------------------------------------------------- 1 | listAll($user); 11 | print_r($response); 12 | echo "\n"; 13 | 14 | echo "add friends: \n"; 15 | $response = $friend->add($user, $friends); 16 | print_r($response); 17 | echo "\n"; 18 | 19 | echo "list friends: \n"; 20 | $response = $friend->listAll($user); 21 | print_r($response); 22 | echo "\n"; 23 | 24 | echo "update notename of friends: \n"; 25 | $response = $friend->updateNotename($user, [ 26 | [ 27 | 'username' => 'user_1', 28 | 'note_name' => 'user_1_alias', 29 | 'others' => 'good friend' 30 | ], [ 31 | 'username' => 'user_2', 32 | 'note_name' => 'user_2_alias', 33 | 'others' => 'normal friend' 34 | ] 35 | ]); 36 | print_r($response); 37 | echo "\n"; 38 | 39 | echo "list friends: \n"; 40 | $response = $friend->listAll($user); 41 | print_r($response); 42 | echo "\n"; 43 | 44 | echo "remove friends: \n"; 45 | $response = $friend->remove($user, $friends); 46 | print_r($response); 47 | echo "\n"; 48 | -------------------------------------------------------------------------------- /src/JMessage/Cross/Nodisturb.php: -------------------------------------------------------------------------------- 1 | $appKey ]; 11 | if (!isset($options['add'])) { 12 | $body['single']['add'] = $options['add']; 13 | } 14 | if (!isset($options['remove'])) { 15 | $body['single']['remove'] = $options['remove']; 16 | } 17 | 18 | return $this->nodisturb($user, [$body]); 19 | } 20 | 21 | public function group($user, $appKey, array $options) { 22 | $body = [ 'appkey' => $appKey ]; 23 | if (!isset($options['add'])) { 24 | $body['group']['add'] = $options['add']; 25 | } 26 | if (!isset($options['remove'])) { 27 | $body['group']['remove'] = $options['remove']; 28 | } 29 | 30 | return $this->nodisturb($user, [$body]); 31 | } 32 | 33 | public function nodisturb($user, array $options ) { 34 | $uri = self::BASE_URI . $user . '/nodisturb'; 35 | $body = $options; 36 | $response = $this->post($uri, $body); 37 | return $response; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/JMessage/Cross/Blacklist.php: -------------------------------------------------------------------------------- 1 | $appKey, 12 | 'usernames' => $usernames 13 | ]]; 14 | return $this->batchAdd($user, $body); 15 | } 16 | 17 | public function batchAdd($user, array $options) { 18 | $uri = self::BASE_URI . $user . '/blacklist'; 19 | $body = $options; 20 | $response = $this->put($uri, $body); 21 | return $response; 22 | } 23 | 24 | public function remove($user, $appKey, array $usernames) { 25 | $body = [[ 26 | 'appkey' => $appKey, 27 | 'usernames' => $usernames 28 | ]]; 29 | return $this->batchRemove($user, $body); 30 | } 31 | 32 | public function batchRemove($user, array $options) { 33 | $uri = self::BASE_URI . $user . '/blacklist'; 34 | $body = $options; 35 | $response = $this->del($uri, $body); 36 | return $response; 37 | } 38 | 39 | public function listAll($user) { 40 | $uri = self::BASE_URI . $user . '/blacklist'; 41 | $response = $this->get($uri); 42 | return $response; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/JMessage/Cross/Message.php: -------------------------------------------------------------------------------- 1 | $appKey]); 9 | return parent::sendText($version, $from, $target, $msg, $notification, $opts); 10 | } 11 | 12 | public function sendImage($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []) { 13 | $opts = array_merge($options, ['target_appkey' => $appKey]); 14 | return parent::sendImage($version, $from, $target, $msg, $notification, $opts); 15 | } 16 | 17 | public function sendVoice($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []) { 18 | $opts = array_merge($options, ['target_appkey' => $appKey]); 19 | return parent::sendVoice($version, $from, $target, $msg, $notification, $opts); 20 | } 21 | 22 | public function sendCustom($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []) { 23 | $opts = array_merge($options, ['target_appkey' => $appKey]); 24 | return parent::sendCustom($version, $from, $target, $msg, $notification, $opts); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /examples/user/nodisturb_examples.php: -------------------------------------------------------------------------------- 1 | addSingleNodisturb($username, ['user_1']); 12 | print_r($response); 13 | echo "\n"; 14 | 15 | echo "remove single nodisturb: \n"; 16 | $response = $user->removeSingleNodisturb($username, ['user_1']); 17 | print_r($response); 18 | echo "\n"; 19 | 20 | echo "open global nodisturb: \n"; 21 | $response = $user->openGlobalNodisturb($username); 22 | print_r($response); 23 | echo "\n"; 24 | 25 | echo "close global nodisturb: \n"; 26 | $response = $user->closeGlobalNodisturb($username); 27 | print_r($response); 28 | echo "\n"; 29 | 30 | // 自定义免打扰的设置参数比较复杂,建议使用上面所述的 6 种方式设置免打扰。 31 | // echo "custom nodisturb setting: \n"; 32 | // $touser = 'user'; 33 | // $user0 = 'user_0'; 34 | // $user1 = 'user_1'; 35 | // $user2 = 'user_2'; 36 | // $user3 = 'user_3'; 37 | // $gid0 = '10000'; 38 | // $gid1 = '10001'; 39 | // $gid2 = '10002'; 40 | // $gid3 = '10003'; 41 | 42 | // $options = [ 43 | // "single" => [ 44 | // "add" => [$user0, $user1], 45 | // "remove" => [$user2, $user3], 46 | // ], 47 | // "group" => [ 48 | // "add" => [$gid0, $gid1], 49 | // "remove" => [$gid2, $gid3], 50 | // ], 51 | // "global" => 1 52 | // ]; 53 | 54 | // $response = $user->nodisturb($touser, $options); 55 | // print_r($response); 56 | // echo "\n"; 57 | -------------------------------------------------------------------------------- /src/JMessage/IM/SensitiveWord.php: -------------------------------------------------------------------------------- 1 | $start, 13 | 'count' => $count 14 | ]; 15 | 16 | $response = $this->get($uri, $query); 17 | return $response; 18 | } 19 | 20 | public function add(array $words) { 21 | $uri = self::BASE_URI; 22 | $body = $words; 23 | $response = $this->post($uri, $body); 24 | return $response; 25 | } 26 | 27 | public function delete($word) { 28 | $uri = self::BASE_URI; 29 | $body = ['word' => $word]; 30 | $response = $this->del($uri, $body); 31 | return $response; 32 | } 33 | 34 | public function update($old, $new) { 35 | $uri = self::BASE_URI; 36 | $body = [ 37 | 'old_word' => $old, 38 | 'new_word' => $new 39 | ]; 40 | $response = $this->put($uri, $body); 41 | return $response; 42 | } 43 | 44 | public function getStatus() { 45 | $uri = self::BASE_URI . '/status'; 46 | 47 | $response = $this->get($uri); 48 | return $response; 49 | } 50 | 51 | public function updateStatus($opened) { 52 | $uri = self::BASE_URI . '/status?status=' . (int)$opened; 53 | 54 | $response = $this->put($uri); 55 | return $response; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/JMessage/Cross/Friend.php: -------------------------------------------------------------------------------- 1 | $appKey, 13 | 'users' => $friendnames 14 | ]; 15 | $response = $this->post($uri, $body); 16 | return $response; 17 | } 18 | 19 | public function remove($user, $appKey, array $friendnames) { 20 | $uri = self::BASE_URI . $user . '/friends'; 21 | $body = [ 22 | 'appkey' => $appKey, 23 | 'users' => $friendnames 24 | ]; 25 | $response = $this->del($uri, $body); 26 | return $response; 27 | } 28 | 29 | public function updateNotename($user, $appKey, $friendname, array $options) { 30 | $body = [ 31 | 'appkey' => $appKey, 32 | 'username' => $friendname 33 | ]; 34 | 35 | if (isset($options['note_name'])) { 36 | $body['note_name'] = $options['note_name']; 37 | } 38 | 39 | if (isset($options['others'])) { 40 | $body['others'] = $options['others']; 41 | } 42 | 43 | return $this->batchUpdateNotename($user, [$body]); 44 | } 45 | 46 | public function batchUpdateNotename($user, array $options) { 47 | $uri = self::BASE_URI . $user . '/friends'; 48 | $response = $this->put($uri, $options); 49 | return $response; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /examples/report_example.php: -------------------------------------------------------------------------------- 1 | getMessagesV1(0, 100, '2015-11-17 10:10:10', '2017-12-17 10:10:10'); 12 | print_r($response); 13 | 14 | $response = $report->getUserMessagesV1($user, 0, 100); 15 | print_r($response); 16 | 17 | 18 | # -------------------------------------------------------------------------- 19 | # V2 20 | 21 | $response = $report->getMessages(100, '2017-12-10 10:10:10', '2017-12-17 10:10:10'); 22 | print_r($response); 23 | 24 | // $cursor = $response['body']['cursor']; 25 | // $response = $report->getNextMessages($cursor); 26 | // print_r($response); 27 | 28 | $response = $report->getUserMessages($user, 100, '2017-12-10 10:10:10', '2017-12-17 10:10:10'); 29 | print_r($response); 30 | 31 | // $cursor = $response['body']['cursor']; 32 | // $response = $report->getNextUserMessages($user, $cursor); 33 | // print_r($response); 34 | 35 | $gid = 'VALID_GID'; 36 | 37 | $response = $report->getGroupMessages($gid, 100, '2017-12-10 10:10:10', '2017-12-17 10:10:10'); 38 | print_r($response); 39 | 40 | // $cursor = $response['body']['cursor']; 41 | // $response = $report->getNextGroupMessages($gid, $cursor); 42 | // print_r($response); 43 | 44 | $response = $report->messages('DAY', 2017-12-01, 6); 45 | print_r($response); 46 | 47 | $response = $report->users('DAY', 2017-12-01, 6); 48 | print_r($response); 49 | 50 | $response = $report->groups('DAY', 2017-12-01, 6); 51 | print_r($response); 52 | -------------------------------------------------------------------------------- /examples/group_examples.php: -------------------------------------------------------------------------------- 1 | create($owner, $name, $desc, $members); 16 | print_r($response); 17 | echo "\n"; 18 | 19 | $gid = $response['body']['gid']; 20 | 21 | echo "show group: \n"; 22 | $response = $group->show($gid); 23 | print_r($response); 24 | echo "\n"; 25 | 26 | echo "update group: \n"; 27 | $name = 'new jiguang'; 28 | $desc = 'new jiguang gtoup'; 29 | $response = $group->update($gid, $name, $desc); 30 | print_r($response); 31 | echo "\n"; 32 | 33 | echo "group list: \n"; 34 | $response = $group->listAll(10); 35 | print_r($response); 36 | echo "\n"; 37 | 38 | #### members start 39 | 40 | echo "list members in group: \n"; 41 | $response = $group->members($gid); 42 | print_r($response); 43 | echo "\n"; 44 | 45 | echo "add members to group: \n"; 46 | $response = $group->addMembers($gid, $mems); 47 | print_r($response); 48 | echo "\n"; 49 | 50 | echo "list members in group: \n"; 51 | $response = $group->members($gid); 52 | print_r($response); 53 | echo "\n"; 54 | 55 | echo "remove members from group: \n"; 56 | $response = $group->removeMembers($gid, $mems); 57 | print_r($response); 58 | echo "\n"; 59 | 60 | echo "list members in group: \n"; 61 | $response = $group->members($gid); 62 | print_r($response); 63 | echo "\n"; 64 | 65 | #### members end 66 | echo "delete group: \n"; 67 | $response = $group->delete($gid); 68 | print_r($response); 69 | echo "\n"; 70 | -------------------------------------------------------------------------------- /examples/sensitiveword_examples.php: -------------------------------------------------------------------------------- 1 | listAll(10); 13 | print_r($response['body']); 14 | echo "\n"; 15 | 16 | echo "add sensitiveword: $word_0 and $word_1 \n"; 17 | $response = $sensitiveword->add([$word_0, $word_1]); 18 | print_r($response['body']); 19 | echo "\n"; 20 | 21 | echo "list sensitiveword: \n"; 22 | $response = $sensitiveword->listAll(10); 23 | print_r($response['body']); 24 | echo "\n"; 25 | 26 | echo "update sensitiveword: $word_0 => $word_2 \n"; 27 | $response = $sensitiveword->update($word_0, $word_2); 28 | print_r($response['body']); 29 | echo "\n"; 30 | 31 | echo "list sensitiveword: \n"; 32 | $response = $sensitiveword->listAll(10); 33 | print_r($response['body']); 34 | echo "\n"; 35 | 36 | echo "delete sensitiveword: $word_1 \n"; 37 | $response = $sensitiveword->delete($word_1); 38 | print_r($response['body']); 39 | echo "\n"; 40 | 41 | echo "list sensitiveword: \n"; 42 | $response = $sensitiveword->listAll(10); 43 | print_r($response['body']); 44 | echo "\n"; 45 | 46 | echo "delete sensitiveword: $word_2 \n"; 47 | $response = $sensitiveword->delete($word_2); 48 | print_r($response['body']); 49 | echo "\n"; 50 | 51 | echo "list sensitiveword: \n"; 52 | $response = $sensitiveword->listAll(10); 53 | print_r($response['body']); 54 | echo "\n"; 55 | 56 | 57 | echo "get sensitiveword status: \n"; 58 | $response = $sensitiveword->getStatus(); 59 | print_r($response['body']); 60 | echo "\n"; 61 | 62 | echo "update sensitiveword status: \n"; 63 | $response = $sensitiveword->updateStatus(true); 64 | print_r($response); 65 | echo "\n"; 66 | 67 | echo "get sensitiveword status: \n"; 68 | $response = $sensitiveword->getStatus(); 69 | print_r($response['body']); 70 | echo "\n"; 71 | 72 | echo "update sensitiveword status: \n"; 73 | $response = $sensitiveword->updateStatus(false); 74 | print_r($response); 75 | echo "\n"; 76 | 77 | echo "get sensitiveword status: \n"; 78 | $response = $sensitiveword->getStatus(); 79 | print_r($response['body']); 80 | echo "\n"; 81 | -------------------------------------------------------------------------------- /src/JMessage/IM/ChatRoom.php: -------------------------------------------------------------------------------- 1 | $start, 13 | 'count' => $count 14 | ]; 15 | $response = $this->get($uri, $query); 16 | return $response; 17 | } 18 | 19 | public function create($name, $owner, array $members = [], $description = null ) { 20 | $uri = self::BASE_URI; 21 | $body = [ 22 | 'name' => $name, 23 | 'owner_username' => $owner 24 | ]; 25 | if (!empty($members)) { 26 | $body['members_username'] = $members; 27 | } 28 | if (!is_null($description)) { 29 | $body['description'] = $description; 30 | } 31 | $response = $this->post($uri, $body); 32 | return $response; 33 | } 34 | 35 | public function show($roomId) { 36 | return $this->showBatch([$roomId]); 37 | } 38 | 39 | public function showBatch(array $roomIds) { 40 | $uri = self::BASE_URI . 'batch'; 41 | $body = $roomIds; 42 | $response = $this->post($uri, $body); 43 | return $response; 44 | } 45 | 46 | public function update($roomId, array $options) { 47 | $uri = self::BASE_URI . $roomId; 48 | $body = $options; 49 | $response = $this->put($uri, $body); 50 | return $response; 51 | } 52 | 53 | public function delete($roomId) { 54 | $uri = self::BASE_URI . $roomId; 55 | $response = $this->del($uri); 56 | return $response; 57 | } 58 | 59 | public function forbiddenUser($roomId, $user, $enabled) { 60 | $status = (bool)$enabled ? 1 : 0; 61 | $uri = self::BASE_URI . $roomId . '/forbidden/' . $user . '?status=' . $status; 62 | $response = $this->put($uri); 63 | return $response; 64 | } 65 | 66 | public function members($roomId, $count, $start = 0) { 67 | $uri = self::BASE_URI . $roomId. '/members'; 68 | $query = [ 69 | 'start' => $start, 70 | 'count' => $count 71 | ]; 72 | $response = $this->get($uri, $query); 73 | return $response; 74 | } 75 | 76 | public function addMembers($roomId, array $members) { 77 | $uri = self::BASE_URI . $roomId . '/members'; 78 | $body = $members; 79 | $response = $this->put($uri, $body); 80 | return $response; 81 | } 82 | public function removeMembers($roomId, array $members) { 83 | $uri = self::BASE_URI . $roomId . '/members'; 84 | $body = $members; 85 | $response = $this->del($uri, $body); 86 | return $response; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JMessage API PHP Client 2 | 3 | 这是 JMessage REST API 的 PHP 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能。 4 | 5 | 对应的 REST API 文档: https://docs.jiguang.cn/jmessage/server/rest_api_im/ 6 | 7 | > 支持的 PHP 版本: 5.4 ~ 5.6.x, 7 8 | 9 | ## Installation 10 | 11 | #### 使用 Composer 安装 12 | 13 | - 在项目中的 `composer.json` 文件中添加 jmessage 依赖: 14 | 15 | ```json 16 | "require": { 17 | "jiguang/jmessage": "~1.1" 18 | } 19 | ``` 20 | 21 | - 执行 `$ php composer.phar install` 或 `$ composer install` 进行安装。 22 | 23 | #### 直接下载源码安装 24 | 25 | > 直接下载源代码也是一种安装 SDK 的方法,不过因为有版本更新的维护问题,所以这种安装方式**十分不推荐**,但由于种种原因导致无法使用 Composer,所以我们也提供了这种情况下的备选方案。 26 | 27 | - 下载源代码包,解压到项目中 28 | - 在项目中引入 autoload(在源码根目录下): 29 | 30 | ```php 31 | require 'path_to_sdk/autoload.php'; 32 | ``` 33 | 34 | ## Usage 35 | 36 | * [JMessage Client](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#jmessage-client) 37 | * [证书问题](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#证书问题) 38 | * [User 用户](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#user-用户) 39 | * [Admin 管理员](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#admin-管理员) 40 | * [Blacklist 黑名单](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#blacklist-黑名单) 41 | * [Group 群组](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#group-群组) 42 | * [Friend 好友](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#friend-好友) 43 | * [Resource 媒体资源](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#resource-媒体资源) 44 | * [消息相关](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#消息相关) 45 | * [SensitiveWord 敏感词](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#sensitiveword-敏感词) 46 | * [ChatRoom 聊天室](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/GUIDE.md#chatroom-聊天室) 47 | * [跨应用](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/CROSS.md#cross-跨应用) 48 | * [Report](https://github.com/jpush/jmessage-api-php-client/blob/master/docs/REPORT.md) 49 | 50 | ## Examples 51 | 52 | **注意: 这只是使用样例, 不应该直接用于实际环境中!!** 53 | 54 | 在项目的 [examples](https://github.com/jpush/jmessage-api-php-client/tree/master/examples) 文件夹中有简单的使用示例代码, 开发者可以参考其中的样例快速了解该库的使用方法。 55 | 56 | **注:所下载的样例代码不可马上使用,需要在** `examples/config.php` **文件中填入相关的必要参数,或者设置相关环境变量,不进行这个操作则示例运行会失败。** 另外为保护开发者隐私 `examples/config.php` 文件不在版本控制中,需要使用如下命令手动复制: 57 | 58 | ```php 59 | $ cp examples/config.php.example examples/config.php 60 | ``` 61 | 62 | **示例简单使用方法** 63 | 64 | 若要运行 friend_examples.php 中的示例代码: 65 | 66 | ```bash 67 | # 假定当前目录为 JMessage 源码所在的根目录 68 | $ php examples/friend_examples.php 69 | ``` 70 | > 当然也可编辑相关的示例文件,更改参数查看执行效果 71 | 72 | ## ErrorCode 73 | 74 | JMessage 服务器端报的错误码。有可能出现在返回值中,可在这里查询含义: https://docs.jiguang.cn/jmessage/client/im_errorcode_server/ 75 | 76 | ## Contributing 77 | 78 | Bug reports and pull requests are welcome on GitHub at https://github.com/jpush/jmessage-api-php-client. 79 | 80 | ## License 81 | 82 | The library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 83 | -------------------------------------------------------------------------------- /src/JMessage/IM/Group.php: -------------------------------------------------------------------------------- 1 | $owner, 14 | 'name' => $name, 15 | 'desc' => $desc, 16 | 'avatar' => $avatar, 17 | 'flag' => $flag 18 | ]; 19 | if (!is_null($members) || !empty($members)) { 20 | $body['members_username'] = $members; 21 | } 22 | $response = $this->post($uri, $body); 23 | return $response; 24 | } 25 | 26 | public function show($gid) { 27 | $uri = self::BASE_URI . $gid; 28 | $response = $this->get($uri); 29 | return $response; 30 | } 31 | 32 | public function update($gid, $name = null, $desc = null, $avatar = null) { 33 | $uri = self::BASE_URI . $gid; 34 | 35 | $body = []; 36 | if (!is_null($name)) { $body['name'] = $name; } 37 | if (!is_null($desc)) { $body['desc'] = $desc; } 38 | if (!is_null($avatar)) { $body['avatar'] = $avatar; } 39 | 40 | $response = $this->put($uri, $body); 41 | return $response; 42 | } 43 | 44 | public function delete($gid) { 45 | $uri = self::BASE_URI . $gid; 46 | $response = $this->del($uri); 47 | return $response; 48 | } 49 | 50 | public function listAll($count, $start = 0) { 51 | $uri = self::BASE_URI; 52 | $query = [ 53 | 'start' => $start, 54 | 'count' => $count 55 | ]; 56 | $response = $this->get($uri, $query); 57 | return $response; 58 | } 59 | 60 | public function addMembers($gid, array $add) { 61 | return $this->updateMembers($gid, [ 'add' => $add ]); 62 | } 63 | public function removeMembers($gid, array $remove) { 64 | return $this->updateMembers($gid, [ 'remove' => $remove ]); 65 | } 66 | public function updateMembers($gid, array $options) { 67 | $uri = self::BASE_URI . $gid . '/members'; 68 | $body = $options; 69 | $response = $this->post($uri, $body); 70 | return $response; 71 | } 72 | 73 | public function members($gid) { 74 | $uri = self::BASE_URI . $gid . '/members'; 75 | $response = $this->get($uri); 76 | return $response; 77 | } 78 | 79 | public function addSilence($gid, $usernames) { 80 | $uri = self::BASE_URI . 'messages/' . $gid . '/silence?status=true'; 81 | $response = $this->put($uri, $usernames); 82 | return $response; 83 | } 84 | 85 | public function removeSilence($gid, $usernames) { 86 | $uri = self::BASE_URI . 'messages/' . $gid . '/silence?status=false'; 87 | $response = $this->put($uri, $usernames); 88 | return $response; 89 | } 90 | 91 | public function updateOwner($gid, $username, $appkey = null) { 92 | $uri = self::BASE_URI . 'owner/' . $gid; 93 | $body = ['username' => $username]; 94 | if(!is_null($appkey)) { 95 | $body['appkey'] = $appkey; 96 | } 97 | $response = $this->put($uri, $body); 98 | return $response; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /docs/REPORT.md: -------------------------------------------------------------------------------- 1 | # Report 2 | 3 | ```php 4 | use JMessage\IM\Report; 5 | $report = new Report($jm); 6 | ``` 7 | 8 | ## 获取消息 9 | 10 | ```php 11 | $report->getMessages($count, $beginTime, $endTime) 12 | $report->getNextMessages($cursor) 13 | ``` 14 | 15 | **参数:** 16 | 17 | > $count: 每次查询的总条数 一次最多 1000 18 | 19 | > $beginTime: 记录开始时间 格式 yyyy-MM-dd HH:mm:ss 设置筛选条件大于等于 $beginTime 20 | 21 | > $endTime: 记录结束时间 格式 yyyy-MM-dd HH:mm:ss 设置筛选条件下于等于 $endTime, $beginTime $endTime 之间最大范围不得超过 7 天 22 | 23 | > $cursor: 当第一次请求后如果后面有数据,会返回一个cursor回来用这个获取接下来的消息 24 | 25 | **示例:** 26 | 27 | ```php 28 | $response = $report->getMessages(100, '2017-12-10 10:10:10', '2017-12-17 10:10:10'); 29 | 30 | $cursor = 'xxxx'; 31 | $response = $report->getNextMessages($cursor); 32 | ``` 33 | 34 | ## 获取用户消息 35 | 36 | ```php 37 | $report->getUserMessages($username, $count, $beginTime, $endTime) 38 | $report->getNextUserMessages($username, $cursor) 39 | ``` 40 | 41 | **参数:** 42 | 43 | > $username: 表示想要获取其用户消息的用户名 44 | 45 | > $count: 每次查询的总条数 一次最多 1000 46 | 47 | > $beginTime: 记录开始时间 格式 yyyy-MM-dd HH:mm:ss 设置筛选条件大于等于 $beginTime 48 | 49 | > $endTime: 记录结束时间 格式 yyyy-MM-dd HH:mm:ss 设置筛选条件下于等于 $endTime, $beginTime $endTime 之间最大范围不得超过 7 天 50 | 51 | > $cursor: 当第一次请求后如果后面有数据,会返回一个cursor回来用这个获取接下来的消息 52 | 53 | **示例:** 54 | 55 | ```php 56 | $response = $report->getUserMessages('user_0', 100, '2017-12-10 10:10:10', '2017-12-17 10:10:10'); 57 | 58 | $cursor = 'xxxx'; 59 | $response = $report->getNextUserMessages('user_0', $cursor); 60 | ``` 61 | 62 | ## 获取群组消息 63 | 64 | ```php 65 | $report->getGroupMessages($gid, $count, $beginTime, $endTime) 66 | $report->getNextGroupMessages($gid, $cursor) 67 | ``` 68 | 69 | **参数:** 70 | 71 | > $gid: 表示想要获取其群组消息的 gid 72 | 73 | > $count: 每次查询的总条数 一次最多 1000 74 | 75 | > $beginTime: 记录开始时间 格式 yyyy-MM-dd HH:mm:ss 设置筛选条件大于等于 $beginTime 76 | 77 | > $endTime: 记录结束时间 格式 yyyy-MM-dd HH:mm:ss 设置筛选条件下于等于 $endTime, $beginTime $endTime 之间最大范围不得超过 7 天 78 | 79 | > $cursor: 当第一次请求后如果后面有数据,会返回一个cursor回来用这个获取接下来的消息 80 | 81 | **示例:** 82 | 83 | ```php 84 | $gid = 'xxxx'; 85 | 86 | $response = $report->getGroupMessages($gid, 100, '2017-12-10 10:10:10', '2017-12-17 10:10:10'); 87 | 88 | $cursor = 'xxxx'; 89 | $response = $report->getNextGroupMessages($gid, $cursor); 90 | ``` 91 | 92 | ## 用户统计 93 | 94 | ```php 95 | $report->users($timeUnit, $start, $duration); 96 | ``` 97 | 98 | **参数:** 99 | 100 | > $timeUnit: 查询维度,目前只有 DAY 101 | 102 | > $start: 开始时间 timeUnit 为 DAY 的时候格式为 yyyy-MM-dd 103 | 104 | > $duration: 请求时的持续时长,DAY 最大为 60 天 105 | 106 | **示例:** 107 | 108 | ```php 109 | $response = $report->users('DAY', 2017-12-01, 6); 110 | ``` 111 | 112 | ## 消息统计 113 | 114 | ```php 115 | $report->messages($timeUnit, $start, $duration); 116 | ``` 117 | 118 | **参数:** 119 | 120 | > $timeUnit: 查询维度,目前有 HOUR DAY MONTH 三个维度可以选 121 | 122 | > $start: 开始时间 timeUnit 为 HOUR 时 格式为 yyyy-MM-dd HH,DAY 的时候格式为yyyy-MM-dd,MONTH 的时候格式为 yyyy-MM 123 | 124 | > $duration: 请求时的持续时长,HOUR 只支持查询当天的统计, DAY 最大为 60 天 ,MOTH 为两个月 125 | 126 | **示例:** 127 | 128 | ```php 129 | $response = $report->messages('DAY', 2017-12-01, 6); 130 | ``` 131 | 132 | ## 群组统计 133 | 134 | ```php 135 | $report->groups($timeUnit, $start, $duration); 136 | ``` 137 | 138 | **参数:** 139 | 140 | > $timeUnit: 查询维度,目前只有 DAY 141 | 142 | > $start: 开始时间 timeUnit 为 DAY 的时候格式为 yyyy-MM-dd 143 | 144 | > $duration: 请求时的持续时长,DAY 最大为 60 天 145 | 146 | 147 | **示例:** 148 | 149 | ```php 150 | $response = $report->groups('DAY', 2017-12-01, 6); 151 | ``` 152 | -------------------------------------------------------------------------------- /src/JMessage/IM/Report.php: -------------------------------------------------------------------------------- 1 | reportV1($uri, $start, $count, $beginTime, $endTime); 13 | } 14 | 15 | public function getUserMessagesV1($username, $start, $count, $beginTime = null, $endTime = null) { 16 | $uri = self::BASE_URL_V1 . '/users/' . $username . '/messages'; 17 | return $this->reportV1($uri, $start, $count, $beginTime, $endTime); 18 | } 19 | 20 | private function reportV1($uri, $start, $count, $beginTime = null, $endTime = null) { 21 | $query = [ 22 | 'start' => $start, 23 | 'count' => $count 24 | ]; 25 | 26 | if (!is_null($beginTime)) { 27 | $query['begin_time'] = $beginTime; 28 | } 29 | if (!is_null($endTime)) { 30 | $query['end_time'] = $endTime; 31 | } 32 | return $this->get($uri, $query); 33 | } 34 | 35 | public function getMessages($count, $beginTime, $endTime) { 36 | $uri = self::BASE_URL . 'messages'; 37 | return $this->report($uri, $count, $beginTime, $endTime); 38 | } 39 | 40 | public function getNextMessages($cursor) { 41 | $uri = self::BASE_URL . 'messages'; 42 | return $this->get($uri, [ 'cursor' => $cursor ]); 43 | } 44 | 45 | public function getUserMessages($username, $count, $beginTime, $endTime) { 46 | $uri = self::BASE_URL . 'users/' . $username . '/messages'; 47 | return $this->report($uri, $count, $beginTime, $endTime); 48 | } 49 | 50 | public function getNextUserMessages($username, $cursor) { 51 | $uri = self::BASE_URL . 'users/' . $username . '/messages'; 52 | return $this->get($uri, [ 'cursor' => $cursor ]); 53 | } 54 | 55 | public function getGroupMessages($gid, $count, $beginTime, $endTime) { 56 | $uri = self::BASE_URL . 'groups/' . $gid . '/messages'; 57 | return $this->report($uri, $count, $beginTime, $endTime); 58 | } 59 | 60 | public function getNextGroupMessages($gid, $cursor) { 61 | $uri = self::BASE_URL . 'groups/' . $gid . '/messages'; 62 | return $this->get($uri, [ 'cursor' => $cursor ]); 63 | } 64 | 65 | public function messages($timeUnit, $start, $duration) { 66 | $uri = self::BASE_URL . '/statistic/messages'; 67 | return $this->statistic($uri, $timeUnit, $start, $duration); 68 | } 69 | 70 | public function users($timeUnit, $start, $duration) { 71 | $uri = self::BASE_URL . '/statistic/users'; 72 | return $this->statistic($uri, $timeUnit, $start, $duration); 73 | } 74 | 75 | public function groups($timeUnit, $start, $duration) { 76 | $uri = self::BASE_URL . '/statistic/groups'; 77 | return $this->statistic($uri, $timeUnit, $start, $duration); 78 | } 79 | 80 | 81 | private function report($uri, $count, $beginTime, $endTime) { 82 | $query = [ 83 | 'count' => $count, 84 | 'begin_time' => $beginTime, 85 | 'end_time' => $endTime 86 | ]; 87 | return $this->get($uri, $query); 88 | } 89 | 90 | private function statistic($uri, $timeUnit, $start, $duration) { 91 | $query = [ 92 | 'time_unit' => $timeUnit, 93 | 'start' => $start, 94 | 'duration' => $duration 95 | ]; 96 | return $this->get($uri, $query); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/JMessage/Http.php: -------------------------------------------------------------------------------- 1 | client, 'GET', $uri); 14 | } 15 | public function post($uri, array $body = []) { 16 | return self::request($this->client, 'POST', $uri, $body); 17 | } 18 | 19 | public function put($uri, array $body = []) { 20 | return self::request($this->client, 'PUT', $uri, $body); 21 | } 22 | 23 | public function delete($uri, array $body = []) { 24 | return self::request($this->client, 'DELETE', $uri, $body); 25 | } 26 | 27 | public function upload($uri, array $body = []) { 28 | $headers = [ 29 | 'Content-Type: multipart/form-data', 30 | 'Connection: Keep-Alive' 31 | ]; 32 | return self::request($this->client, 'UPLOAD', $uri, $body, $headers); 33 | } 34 | 35 | public static function getInstance($client) { 36 | if (is_null(self::$_instance) || !(self::$_instance instanceof self)) { 37 | self::$_instance = new self($client); 38 | } 39 | return self::$_instance; 40 | } 41 | 42 | private function __construct($client) { 43 | $this->client = $client; 44 | } 45 | private function __clone() {} 46 | 47 | private static function request($client, $method, $uri, array $body = [], array $headers = []) { 48 | $default_headers = [ 49 | 'Content-Type: application/json', 50 | 'Connection: Keep-Alive' 51 | ]; 52 | 53 | $method = strtoupper($method); 54 | $ch = curl_init(); 55 | $options = array( 56 | CURLOPT_RETURNTRANSFER => true, 57 | CURLOPT_HEADER => true, 58 | CURLOPT_HTTPHEADER => (empty($headers) ? $default_headers : $headers), 59 | CURLOPT_USERAGENT => 'JMessage-Api-PHP-Client', 60 | CURLOPT_CONNECTTIMEOUT => 20, 61 | CURLOPT_TIMEOUT => 120, 62 | 63 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 64 | CURLOPT_USERPWD => $client->getAuth(), 65 | 66 | CURLOPT_URL => $uri, 67 | CURLOPT_CUSTOMREQUEST => ('UPLOAD' == $method) ? 'POST' : $method 68 | ); 69 | if ($client->disableSsl()) { 70 | $options[CURLOPT_SSL_VERIFYPEER] = false; 71 | $options[CURLOPT_SSL_VERIFYHOST] = 0; 72 | } 73 | 74 | if (!empty($body)) { 75 | if ('UPLOAD' == $method) { 76 | if (class_exists('\CURLFile')) { 77 | $options[CURLOPT_SAFE_UPLOAD] = true; 78 | $options[CURLOPT_POSTFIELDS] = ['filename' => new \CURLFile($body['path'])]; 79 | } else { 80 | if (defined('CURLOPT_SAFE_UPLOAD')) { 81 | $options[CURLOPT_SAFE_UPLOAD] = false; 82 | } 83 | $options[CURLOPT_POSTFIELDS] = ['filename' => '@' . $body['path']]; 84 | } 85 | } else { 86 | $options[CURLOPT_POSTFIELDS] = json_encode($body); 87 | } 88 | } 89 | 90 | curl_setopt_array($ch, $options); 91 | $output = curl_exec($ch); 92 | 93 | if($output === false) { 94 | return "Error Code:" . curl_errno($ch) . ", Error Message:".curl_error($ch); 95 | } else { 96 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 97 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 98 | $header_text = substr($output, 0, $header_size); 99 | $body = substr($output, $header_size); 100 | $headers = array(); 101 | foreach (explode("\r\n", $header_text) as $i => $line) { 102 | if (!empty($line)) { 103 | if ($i === 0) { 104 | $headers[0] = $line; 105 | } else if (strpos($line, ": ")) { 106 | list ($key, $value) = explode(': ', $line); 107 | $headers[$key] = $value; 108 | } 109 | } 110 | } 111 | $response['headers'] = $headers; 112 | $response['body'] = json_decode($body, true); 113 | $response['http_code'] = $httpCode; 114 | } 115 | curl_close($ch); 116 | return $response; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/JMessage/IM/Message.php: -------------------------------------------------------------------------------- 1 | 'text', 12 | 'msg_body' => [ 13 | 'text' => $msg['text'] 14 | ] 15 | ], $this->buildMessage($version, $from, $target, $notification, $options)); 16 | 17 | if (isset($msg['extras']) && is_array($msg['extras'])) { 18 | $opts['msg_body']['extras'] = $msg['extras']; 19 | } 20 | return $this->send($opts); 21 | } 22 | 23 | public function sendImage($version, array $from, array $target, array $msg, array $notification = [], array $options = []) { 24 | $opts = array_merge([ 25 | 'msg_type' => 'image', 26 | 'msg_body' => [ 27 | 'media_id' => $msg['media_id'], 28 | 'media_crc32' => $msg['media_crc32'], 29 | 'width' => $msg['width'], 30 | 'height' => $msg['height'], 31 | 'format' => $msg['format'], 32 | 'fsize' => $msg['fsize'] 33 | ] 34 | ], $this->buildMessage($version, $from, $target, $notification, $options)); 35 | 36 | if (isset($msg['hash'])) { 37 | $opts['msg_body']['hash'] = $msg['hash']; 38 | } 39 | 40 | return $this->send($opts); 41 | } 42 | 43 | public function sendVoice($version, array $from, array $target, array $msg, array $notification = [], array $options = []) { 44 | $opts = array_merge([ 45 | 'msg_type' => 'voice', 46 | 'msg_body' => [ 47 | 'media_id' => $msg['media_id'], 48 | 'media_crc32' => $msg['media_crc32'], 49 | 'duration' => $msg['duration'], 50 | 'hash' => $msg['hash'], 51 | 'fsize' => $msg['fsize'] 52 | ] 53 | ], $this->buildMessage($version, $from, $target, $notification, $options)); 54 | 55 | if (isset($msg['hash'])) { 56 | $opts['msg_body']['hash'] = $msg['hash']; 57 | } 58 | 59 | return $this->send($opts); 60 | } 61 | 62 | public function sendCustom($version, array $from, array $target, array $msg, array $notification = [], array $options = []) { 63 | $opts = array_merge([ 64 | 'msg_type' => 'custom', 65 | 'msg_body' => $msg 66 | ], $this->buildMessage($version, $from, $target, $notification, $options)); 67 | 68 | return $this->send($opts); 69 | } 70 | 71 | private function buildMessage($version, array $from, array $target, array $notification = [], array $options = []) { 72 | $opts = [ 73 | 'version' => $version, 74 | 'target_type' => $target['type'], 75 | 'from_type' => $from['type'], 76 | 'target_id' => $target['id'], 77 | 'from_id' => $from['id'] 78 | ]; 79 | if (isset($from['name'])) { 80 | $opts['from_name'] = $from['name']; 81 | } 82 | if (isset($target['name'])) { 83 | $opts['target_name'] = $target['name']; 84 | } 85 | if (isset($options['offline'])) { 86 | $opts['no_offline'] = !$options['offline']; 87 | } 88 | 89 | if (isset($options['target_appkey'])) { 90 | $opts['target_appkey'] = $options['target_appkey']; 91 | } 92 | 93 | if (isset($notification['notifiable'])){ 94 | $opts['no_notification'] = !$notification['notifiable']; 95 | } 96 | if (isset($notification['title'])){ 97 | $opts['notification']['title'] = $notification['title']; 98 | } 99 | if (isset($notification['alert'])){ 100 | $opts['notification']['alert'] = $notification['alert']; 101 | } 102 | return $opts; 103 | } 104 | 105 | public function send(array $options) { 106 | $uri = self::BASE_URI; 107 | $body = $options; 108 | $response = $this->post($uri, $body); 109 | return $response; 110 | } 111 | 112 | public function retract($msgid, $username) { 113 | $uri = self::BASE_URI . '/' . $username . '/' . $msgid . '/retract'; 114 | $response = $this->post($uri); 115 | return $response; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/JMessage/IM/User.php: -------------------------------------------------------------------------------- 1 | $username, 12 | 'password' => $password 13 | ]]; 14 | return $this->batchRegister($body); 15 | } 16 | public function batchRegister(array $users) { 17 | $uri = self::BASE_URI; 18 | $body = $users; 19 | $response = $this->post($uri, $body); 20 | return $response; 21 | } 22 | 23 | public function show($username) { 24 | $uri = self::BASE_URI . $username; 25 | $response = $this->get($uri); 26 | return $response; 27 | } 28 | 29 | public function update($username, array $options) { 30 | $uri = self::BASE_URI . $username; 31 | $body = $options; 32 | $response = $this->put($uri, $body); 33 | return $response; 34 | } 35 | 36 | public function stat($username) { 37 | $uri = self::BASE_URI . $username . '/userstat'; 38 | $response = $this->get($uri); 39 | return $response; 40 | } 41 | 42 | public function updatePassword($username, $password) { 43 | $uri = self::BASE_URI . $username . '/password'; 44 | $response = $this->put($uri, [ 'new_password' => $password ]); 45 | return $response; 46 | } 47 | 48 | public function delete($username) { 49 | $uri = self::BASE_URI . $username; 50 | $response = $this->del($uri); 51 | return $response; 52 | } 53 | 54 | public function listAll($count, $start = 0) { 55 | $uri = self::BASE_URI; 56 | $query = [ 57 | 'start' => $start, 58 | 'count' => $count 59 | ]; 60 | $response = $this->get($uri, $query); 61 | return $response; 62 | } 63 | 64 | public function groups($username) { 65 | $uri = self::BASE_URI . $username . '/groups'; 66 | $response = $this->get($uri); 67 | return $response; 68 | } 69 | 70 | ############## NoDisturb 71 | 72 | public function addSingleNodisturb($touser, array $usernames) { 73 | $single = [ 'add' => $usernames ]; 74 | return $this->nodisturb($touser, [ 'single' => $single ]); 75 | } 76 | 77 | public function removeSingleNodisturb($touser, array $usernames) { 78 | $single = [ 'remove' => $usernames ]; 79 | return $this->nodisturb($touser, [ 'single' => $single ]); 80 | } 81 | 82 | public function addGroupNodisturb($touser, array $gids) { 83 | $group = [ 'add' => $gids ]; 84 | return $this->nodisturb($touser, [ 'group' => $group ]); 85 | } 86 | 87 | public function removeGroupNodisturb($touser, array $gids) { 88 | $group = [ 'remove' => $gids ]; 89 | return $this->nodisturb($touser, [ 'group' => $group ]); 90 | } 91 | 92 | // public function setGlobalNodisturb($touser, bool $opened) { 93 | // return $this->nodisturb($touser, [ 'global' => (int)$opened ]); 94 | // } 95 | 96 | public function openGlobalNodisturb($touser) { 97 | return $this->nodisturb($touser, [ 'global' => 1 ]); 98 | } 99 | 100 | public function closeGlobalNodisturb($touser) { 101 | return $this->nodisturb($touser, [ 'global' => 0 ]); 102 | } 103 | 104 | public function nodisturb($touser, array $options) { 105 | $uri = self::BASE_URI . $touser . '/nodisturb'; 106 | $body = $options; 107 | $response = $this->post($uri, $body); 108 | return $response; 109 | } 110 | 111 | /** 112 | * Forbidden 113 | * 114 | * @param string $username 115 | * @param bool $enabled 116 | * @return mixed 117 | */ 118 | public function forbidden($username, $enabled) { 119 | if ($enabled) { 120 | $bool = 'true'; 121 | } else { 122 | $bool = 'false'; 123 | } 124 | $uri = self::BASE_URI . $username . '/forbidden?disable=' . $bool; 125 | $response = $this->put($uri); 126 | return $response; 127 | } 128 | 129 | # groupsShield 130 | public function addGroupsShield($username, array $gids) { 131 | return $this->updateGroupsShield($username, [ 'add' => $gids ]); 132 | } 133 | public function removeGroupsShield($username, array $gids) { 134 | return $this->updateGroupsShield($username, [ 'remove' => $gids ]); 135 | } 136 | private function updateGroupsShield($username, array $options) { 137 | $uri = self::BASE_URI . $username . '/groupsShield'; 138 | $body = $options; 139 | $response = $this->post($uri, $body); 140 | return $response; 141 | } 142 | 143 | 144 | public function chatrooms($username) { 145 | $uri = self::BASE_URI . $username . '/chatroom'; 146 | $response = $this->get($uri); 147 | return $response; 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /docs/CROSS.md: -------------------------------------------------------------------------------- 1 | # Cross 跨应用 2 | 3 | * [JMessage Client](#jmessage-client) 4 | * [跨应用管理群组成员](#跨应用管理群组成员) 5 | * [注册用户](#注册用户) 6 | * [跨应用添加成员](#跨应用添加成员) 7 | * [跨应用移除成员](#跨应用移除成员) 8 | * [跨应用更新群组成员](#跨应用更新群组成员) 9 | * [跨应用获取群组成员列表](#跨应用获取群组成员列表) 10 | * [跨应用管理黑名单](#跨应用管理黑名单) 11 | * [跨应用获取黑名单列表](#跨应用获取黑名单列表) 12 | * [跨应用添加黑名单](#跨应用添加黑名单) 13 | * [跨应用移除黑名单](#跨应用移除黑名单) 14 | * [跨应用批量添加黑名单](#跨应用批量添加黑名单) 15 | * [跨应用批量移除黑名单](#跨应用批量移除黑名单) 16 | * [跨应用免打扰设置](#跨应用免打扰设置) 17 | * [跨应用单聊免打扰设置](#跨应用单聊免打扰设置) 18 | * [跨应用群聊免打扰设置](#跨应用群聊免打扰设置) 19 | * [跨应用好友管理](#跨应用好友管理) 20 | * [跨应用添加好友](#跨应用添加好友) 21 | * [跨应用移除好友](#跨应用移除好友) 22 | * [跨应用更新好友备注](#跨应用更新好友备注) 23 | * [跨应用批量更新好友备注](#跨应用批量更新好友备注) 24 | * [跨应用发送消息](#跨应用发送消息) 25 | 26 | ## JMessage Client 27 | 28 | ```php 29 | use JMessage\JMessage; 30 | 31 | $appKey = 'xxxx'; 32 | $masterSecret = 'xxxx'; 33 | 34 | $client = new JMessage($appKey, $masterSecret); 35 | ``` 36 | 37 | ## 跨应用管理群组成员 38 | 39 | ```php 40 | use JMessage\Cross\Member; 41 | 42 | $member = new Member($client); 43 | ``` 44 | 45 | ### 跨应用添加成员 46 | 47 | ```php 48 | $member->add($gid, $appKey, array $usernames) 49 | ``` 50 | 51 | **参数:** 52 | 53 | > $gid:表示要添加成员的群组 gid 54 | 55 | > $appKey:表示用户所属的 appKey 56 | 57 | > $usernames:表示要添加到群组的用户数组 58 | 59 | **示例:** 60 | 61 | ```php 62 | # 跨应用把 appKey 为 'xxxxxx' 的应用下的用户 'username0' 和 'username1' 添加到群组 gid 为 'xxxx' 的群组中 63 | 64 | $gid = 'xxxx'; 65 | $appKey = 'xxxxxx'; 66 | $usernames = ['username0', 'username1']; 67 | 68 | $response = $member->add($gid, $appKey, $usernames); 69 | ``` 70 | 71 | ### 跨应用移除成员 72 | 73 | ```php 74 | $member->remove($gid, $appKey, array $usernames); 75 | ``` 76 | 77 | **参数:** 78 | 79 | > $gid:表示要移除成员的群组 gid 80 | 81 | > $appKey:表示用户所属的 appKey 82 | 83 | > $usernames:表示要从群组移除的用户数组 84 | 85 | **示例:** 86 | 87 | ```php 88 | # 跨应用从群组 gid 为 'xxxx' 的群组中把 appKey 为 'xxxxxx' 的应用下的用户 'username0' 和 'username1' 移除 89 | 90 | $gid = 'xxxx'; 91 | $appKey = 'xxxxxx'; 92 | $usernames = ['username0', 'username1']; 93 | 94 | $response = $member->remove($gid, $appKey, $usernames); 95 | ``` 96 | 97 | ### 跨应用更新群组成员 98 | 99 | > 跨应用管理成员的设置参数比较复杂,建议使用上面所述的 2 个方法 100 | 101 | ```php 102 | $member->update($gid, array $options); 103 | ``` 104 | 105 | **参数:** 106 | 107 | > $gid:表示要添加成员的群组 gid 108 | 109 | > $options:表示批量添加成员的选项数组 110 | 111 | **示例:** 112 | 113 | ```php 114 | # 跨应用管理群组 'xxxx' 中的成员 115 | 116 | $gid = 'xxxx'; 117 | 118 | 119 | $appKey0 => 'appkey_0'; 120 | $add0 = ['username0', 'username1']; 121 | $remove0 = ['username2', 'username3']; 122 | 123 | $appKey1 = 'appkey_1'; 124 | $add1 = ['username4', 'username5']; 125 | $remove1 = ['username6', 'username7']; 126 | 127 | $options0 = [ 128 | [ 129 | 'appKey' => $appKey0, 130 | 'add' => $add0, 131 | 'remove' => $remove0 132 | ],[ 133 | 'appKey' => $appKey1, 134 | 'add' => $add1, 135 | 'remove' => $remove1] 136 | ]; 137 | 138 | # or 139 | 140 | $options1 = [ 141 | [ 142 | 'appKey' => 'appkey_0', 143 | 'add' => ['username0', 'username1'], 144 | 'remove' => ['username2', 'username3'] 145 | ], [ 146 | 'appKey' => 'appkey_1', 147 | 'add' => ['username4', 'username5'], 148 | 'remove' => ['username6', 'username7'] 149 | ] 150 | ]; 151 | 152 | $response = $member->update($gid, $options1); 153 | ``` 154 | 155 | ### 跨应用获取群组成员列表 156 | 157 | ```php 158 | $member->listAll($gid); 159 | ``` 160 | 161 | **参数:** 162 | 163 | > $gid:表示群组 gid 164 | 165 | **示例:** 166 | 167 | ```php 168 | # 跨应用获取群组 gid 为 'xxxx' 的群组的成员列表 169 | 170 | $gid = 'xxxx'; 171 | 172 | $response = $member->listAll($gid); 173 | ``` 174 | 175 | ## 跨应用管理黑名单 176 | 177 | ```php 178 | use JMessage\Cross\Blacklist; 179 | 180 | $blacklist = new Blacklist($client); 181 | ``` 182 | 183 | ### 跨应用获取黑名单列表 184 | 185 | ```php 186 | $blacklist->listAll($user); 187 | ``` 188 | 189 | **参数:** 190 | 191 | > $user:表示要跨应用获取其黑名单列表的用户 192 | 193 | **示例:** 194 | 195 | ```php 196 | # 跨应用获取用户 'jiguang' 的黑名单列表 197 | 198 | $user = 'jiguang'; 199 | 200 | $response = $blacklist->listAll($user); 201 | ``` 202 | 203 | ### 跨应用添加黑名单 204 | 205 | ```php 206 | $blacklist->add($user, $appKey, array $usernames); 207 | ``` 208 | 209 | **参数:** 210 | 211 | > $user:表示要跨应用管理其黑名单的用户 212 | 213 | > $appKey:表示用户所属的 appKey 214 | 215 | > $usernames:表示要添加进黑名单的用户的数组 216 | 217 | **示例:** 218 | 219 | ```php 220 | # 跨应用把用户 'username0' 和 'username1' 添加到用户 'jiguang' 的黑名单列表中 221 | 222 | $user = 'jiguang'; 223 | $appKey = 'xxxxxx'; 224 | $username = ['username0', 'username1']; 225 | 226 | $response = $blacklist->add($user, $appKey, $usernames); 227 | ``` 228 | 229 | ### 跨应用移除黑名单 230 | 231 | ```php 232 | $blacklist->remove($user, $appKey, array $usernames); 233 | ``` 234 | 235 | **参数:** 236 | 237 | > $user:表示要跨应用管理其黑名单的用户 238 | 239 | > $appKey:表示用户所属的 appKey 240 | 241 | > $usernames:表示要从黑名单移除的用户的数组 242 | 243 | **示例:** 244 | 245 | ```php 246 | # 跨应用把用户 'username0' 和 'username1' 从用户 'jiguang' 的黑名单列表中移除 247 | 248 | $user = 'jiguang'; 249 | $appKey = 'xxxxxx'; 250 | $username = ['username0', 'username1']; 251 | 252 | $response = $blacklist->remove($user, $appKey, $usernames); 253 | ``` 254 | 255 | ### 跨应用批量添加黑名单 256 | 257 | ```php 258 | $blacklist->batchAdd($user, array $options); 259 | ``` 260 | 261 | **参数:** 262 | 263 | > $user:表示要跨应用批量管理其黑名单的用户 264 | 265 | > $options:表示跨应用批量添加黑名单的选项数组 266 | 267 | **示例:** 268 | 269 | ```php 270 | # 跨应用把应用 ‘appKey0’ 中的用户 'username0' 和 'username1' 以及应用 ‘appKey1’ 中的用户 'username2' 和 'username3' 添加到用户 'jiguang' 的黑名单列表中 271 | 272 | $user = 'jiguang'; 273 | 274 | $options = [ 275 | 'appKey' => 'appKey0', 276 | 'usernames' => ['username0', 'username1'] 277 | ],[ 278 | 'appKey' => 'appKey1', 279 | 'usernames' => ['username2', 'username3'] 280 | ]; 281 | 282 | $response = $blacklist->add($user, $options); 283 | ``` 284 | 285 | ### 跨应用批量移除黑名单 286 | 287 | ```php 288 | $blacklist->batchRemove($user, array $options); 289 | ``` 290 | 291 | **参数:** 292 | 293 | > $user:表示要跨应用批量管理其黑名单的用户 294 | 295 | > $options:表示跨应用批量移除黑名单的选项数组 296 | 297 | 298 | **示例:** 299 | 300 | ```php 301 | # 跨应用把应用 ‘appKey0’ 中的用户 'username0' 和 'username1' 以及应用 ‘appKey1’ 中的用户 'username2' 和 'username3' 从用户 'jiguang' 的黑名单列表中移除 302 | 303 | $user = 'jiguang'; 304 | 305 | $options = [ 306 | 'appKey' => 'appKey0', 307 | 'usernames' => ['username0', 'username1'] 308 | ],[ 309 | 'appKey' => 'appKey1', 310 | 'usernames' => ['username2', 'username3'] 311 | ]; 312 | 313 | $response = $blacklist->batchRemove($user, $options); 314 | ``` 315 | 316 | ## 跨应用免打扰设置 317 | 318 | ```php 319 | use JMessage\Cross\Nodisturb; 320 | 321 | $nodisturb = new Nodisturb($client); 322 | ``` 323 | 324 | ### 跨应用单聊免打扰设置 325 | 326 | ```php 327 | $nodisturb->single($user, $appKey, array $options); 328 | ``` 329 | 330 | **参数:** 331 | 332 | > $user:表示要设置跨应用单聊免打扰的用户 333 | 334 | > $appKey:表示用户所属的 appKey 335 | 336 | > $options:表示设置跨应用单聊免打扰的选项数组,支持键名 'add' 或 'remove' 表示添加或移除 337 | 338 | **示例:** 339 | 340 | ```php 341 | $user = 'jiguang'; 342 | $appKey = 'xxxxxx'; 343 | $add = ['username0', 'username1']; 344 | $remove = ['username2', 'username3']; 345 | $options = ['add' => $add, 'remove' => $remove ]; 346 | 347 | # 用户 'jiguang' 跨应用对 'xxxxxx' 应用下的用户 'username0', 'username1' 添加单聊免打扰,且同时对 'username2', 'username3' 移除单聊免打扰 348 | $response = $nodisturb->single($user, $appKey, ['add' => $add, 'remove' => $remove ]); 349 | 350 | # 用户 'jiguang' 跨应用仅对 'xxxxxx' 应用下的用户 'username0', 'username1' 添加单聊免打扰 351 | $response = $nodisturb->single($user, $appKey, ['add' => $add]); 352 | 353 | # 用户 'jiguang' 跨应用仅对 'xxxxxx' 应用下的用户 'username2', 'username3' 移除单聊免打扰 354 | $response = $nodisturb->single($user, $appKey, ['remove' => $remove ]); 355 | ``` 356 | 357 | ### 跨应用群聊免打扰设置 358 | 359 | ```php 360 | $nodisturb->group($user, $appKey, array $options); 361 | ``` 362 | 363 | **参数:** 364 | 365 | > $user:表示要设置跨应用群聊免打扰的用户 366 | 367 | > $appKey:表示群组所属的 appKey 368 | 369 | > $options:表示设置跨应用群聊免打扰的选项数组,支持键名 'add' 或 'remove' 表示添加或移除 370 | 371 | **示例:** 372 | 373 | ```php 374 | # 用户 'jiguang' 跨应用对 'xxxxxx' 应用下的群组 'gid0', 'gid1' 添加群聊免打扰,且同时对 'gid2', 'gid3' 移除群聊免打扰 375 | 376 | $user = 'jiguang'; 377 | $appKey = 'xxxxxx'; 378 | $options = [ 379 | 'add' => ['gid0', 'gid1'], 380 | 'remove' =>['gid2', 'gid3'] 381 | ] 382 | $response = $nodisturb->group($user, $appKey, $options); 383 | ``` 384 | 385 | ## 跨应用好友管理 386 | 387 | ```php 388 | use JMessage\Cross\Friend; 389 | 390 | $friend = new Friend($client); 391 | ``` 392 | 393 | ### 跨应用添加好友 394 | 395 | ```php 396 | $friend->add($user, $appKey, array $friendnames); 397 | ``` 398 | 399 | **参数:** 400 | 401 | > $user:表示要跨应用管理好友的用户 402 | 403 | > $appKey:表示好友所属的 appKey 404 | 405 | > $friendnames:表示要添加的好友数组 406 | 407 | **示例:** 408 | 409 | ```php 410 | # 用户 'jiguang' 跨应用把应用 'xxxxxx' 下的用户 'username0', 'username1' 添加为好友 411 | 412 | $user = 'jiguang'; 413 | $appKey = 'xxxxxx'; 414 | $friendnames = ['username0', 'username1']; 415 | 416 | $response = $friend->add($user, $appKey, $friendnames); 417 | ``` 418 | 419 | ### 跨应用移除好友 420 | 421 | ```php 422 | $friend->remove($user, $appKey, array $friendnames); 423 | ``` 424 | 425 | **参数:** 426 | 427 | > $user:表示要跨应用管理好友的用户 428 | 429 | > $appKey:表示好友所属的 appKey 430 | 431 | > $friendnames:表示要移除的好友数组 432 | 433 | **示例:** 434 | 435 | ```php 436 | # 用户 'jiguang' 跨应用把应用 'xxxxxx' 下的用户 'username0', 'username1' 从好友列表移除 437 | 438 | $user = 'jiguang'; 439 | $appKey = 'xxxxxx'; 440 | $friendnames = ['username0', 'username1']; 441 | 442 | $response = $friend->remove($user, $appKey, $friendnames); 443 | ``` 444 | 445 | ### 跨应用更新好友备注 446 | 447 | ```php 448 | $friend->updateNotename($user, $appKey, $friendname, array $options); 449 | ``` 450 | 451 | **参数:** 452 | 453 | > $user:表示要跨应用管理好友的用户 454 | 455 | > $appKey:表示好友所属的 appKey 456 | 457 | > $friendname:表示要更新备注的好友 458 | 459 | > $options:表示跨应用更新好友备注的选项数组,支持键名 'note_name' 或 'others' 表示备注或其他备注信息 460 | 461 | **示例:** 462 | 463 | ```php 464 | # 用户 'jiguang' 跨应用更新应用 'xxxxxx' 下的用户 'username0' 的备注为 'u00' 465 | 466 | $user = 'jiguang'; 467 | $appKey = 'xxxxxx'; 468 | $friendname = 'username0'; 469 | $options = ['note_name' => 'u00']; 470 | 471 | $response = $friend->updateNotename($user, $appKey, $usernames, $options); 472 | ``` 473 | 474 | ### 跨应用批量更新好友备注 475 | 476 | ```php 477 | $friend->batchUpdateNotename($user, array $options); 478 | ``` 479 | 480 | **参数:** 481 | 482 | > $user:表示要跨应用管理好友的用户 483 | 484 | > $options:表示跨应用批量更新好友备注的选项数组 485 | 486 | **示例:** 487 | 488 | ```php 489 | # 用户 'jiguang' 跨应用更新应用 'appKey0' 下的好友 'username0' 的备注为 'uu00', 490 | # 同时更新应用 'appKey1' 下的好友 'username1' 的其他备注信息为 'nothing to go' 491 | 492 | $user = 'jiguang'; 493 | 494 | $options = [ 495 | [ 496 | 'appKey' => 'appKey0', 497 | 'username' => 'username0', 498 | 'note_name' => 'uu00' 499 | ],[ 500 | 'appKey' => 'appKey1', 501 | 'username' => 'username1', 502 | 'others' => 'nothing to go' 503 | ] 504 | ]; 505 | 506 | $response = $friend->batchUpdateNotename($user, $options); 507 | ``` 508 | 509 | ## 跨应用发送消息 510 | 511 | ```php 512 | use JMessage\Cross\Message; 513 | 514 | $message = new Message($client); 515 | ``` 516 | 517 | ```php 518 | # 跨应用发送文本消息 519 | $message->sendText($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []); 520 | 521 | # 跨应用发送图片消息 522 | $message->sendImage($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []); 523 | 524 | # 跨应用发送语音消息 525 | $message->sendVoice($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []); 526 | 527 | # 跨应用发送自定义消息 528 | $message->sendCustom($version, $appKey, array $from, array $target, array $msg, array $notification = [], array $options = []); 529 | ``` 530 | 531 | **参数:** 532 | 533 | > $version: 版本号,目前是 1 534 | 535 | > $appKey: 跨应用目标appkey 536 | 537 | > $from: 发送者信息数组(说明同普通消息) 538 | 539 | > $target: 接受者信息数组(说明同同普通消息) 540 | 541 | > $msg: 消息体数组(说明同同普通消息) 542 | 543 | > $notification: 自定义通知栏展示数组(说明同同普通消息) 544 | 545 | > $options: 其他选项数组(说明同同普通消息) 546 | -------------------------------------------------------------------------------- /docs/GUIDE.md: -------------------------------------------------------------------------------- 1 | # 目录 2 | 3 | * [JMessage Client](#jmessage-client) 4 | * [User 用户](#user-用户) 5 | * [注册用户](#注册用户) 6 | * [批量注册用户](#批量注册用户) 7 | * [获取用户列表](#获取用户列表) 8 | * [获取用户信息](#获取用户信息) 9 | * [更新用户信息](#更新用户信息) 10 | * [查询用户在线状态](#查询用户在线状态) 11 | * [修改密码](#修改密码) 12 | * [删除用户](#删除用户) 13 | * [禁用用户](#禁用用户) 14 | * [获取用户的群组列表](#获取用户的群组列表) 15 | * [获取用户聊天室列表](#获取用户聊天室列表) 16 | * [添加单聊免打扰](#添加单聊免打扰) 17 | * [移除单聊免打扰](#移除单聊免打扰) 18 | * [添加群聊免打扰](#添加群聊免打扰) 19 | * [移除群聊免打扰](#移除群聊免打扰) 20 | * [开启全局免打扰](#开启全局免打扰) 21 | * [关闭全局免打扰](#关闭全局免打扰) 22 | * [群消息屏蔽](#群消息屏蔽) 23 | * [Admin 管理员](#admin-管理员) 24 | * [管理员注册](#管理员注册) 25 | * [获取应用管理员列表](#获取应用管理员列表) 26 | * [Blacklist 黑名单](#blacklist-黑名单) 27 | * [黑名单列表](#黑名单列表) 28 | * [添加黑名单](#添加黑名单) 29 | * [移除黑名单](#移除黑名单) 30 | * [Group 群组](#group-群组) 31 | * [创建群组](#创建群组) 32 | * [获取群组详情](#获取群组详情) 33 | * [更新群组信息(群名 or 群描述)](#更新群组信息群名-or-群描述) 34 | * [删除群组](#删除群组) 35 | * [更新群组成员](#更新群组成员) 36 | * [添加群组成员](#添加群组成员) 37 | * [移除群组成员](#移除群组成员) 38 | * [获取群组成员列表](#获取群组成员列表) 39 | * [获取当前应用的群组列表](#获取当前应用的群组列表) 40 | * [群消息屏蔽](#群消息屏蔽) 41 | * [移交群主](#移交群主) 42 | * [Friend 好友](#friend-好友) 43 | * [获取好友列表](#获取好友列表) 44 | * [添加好友](#添加好友) 45 | * [删除好友](#删除好友) 46 | * [更新好友备注](#更新好友备注) 47 | * [Resource 媒体资源](#resource-媒体资源) 48 | * [资源上传](#资源上传) 49 | * [资源下载](#资源下载) 50 | * [消息相关](#消息相关) 51 | * [发送文本消息](#发送文本消息) 52 | * [发送图片消息](#发送图片消息) 53 | * [发送语音消息](#发送语音消息) 54 | * [发送自定义消息](#发送自定义消息) 55 | * [消息撤回](#消息撤回) 56 | * [SensitiveWord 敏感词](#sensitiveword-敏感词) 57 | * [获取敏感词列表](#获取敏感词列表) 58 | * [添加敏感词](#添加敏感词) 59 | * [删除敏感词](#删除敏感词) 60 | * [修改敏感词](#修改敏感词) 61 | * [获取敏感词功能状态](#获取敏感词功能状态) 62 | * [更新敏感词功能状态](#更新敏感词功能状态) 63 | * [ChatRoom 聊天室](#chatroom-聊天室) 64 | * [获取当前应用的聊天室列表](#获取当前应用的聊天室列表) 65 | * [创建聊天室](#创建聊天室) 66 | * [获取聊天室详情](#获取聊天室详情) 67 | * [更新聊天室信息](#更新聊天室信息) 68 | * [删除聊天室](#删除聊天室) 69 | * [修改用户禁言状态](#修改用户禁言状态) 70 | * [获取聊天室成员列表](#获取聊天室成员列表) 71 | * [添加聊天室成员](#添加聊天室成员) 72 | * [移除聊天室成员](#移除聊天室成员) 73 | 74 | * [证书问题](#证书问题) 75 | 76 | ## JMessage Client 77 | 78 | ```php 79 | use JMessage\JMessage; 80 | 81 | $appKey = 'xxxx'; 82 | $masterSecret = 'xxxx'; 83 | 84 | $client = new JMessage($appKey, $masterSecret); 85 | ``` 86 | 87 | ## User 用户 88 | 89 | ```php 90 | use JMessage\IM\User; 91 | 92 | $user = new User($client); 93 | ``` 94 | 95 | ### 注册单个用户 96 | 97 | ```php 98 | $user->register($username, $password); 99 | ``` 100 | 101 | **参数:** 102 | 103 | > $username: 表示用户名 104 | 105 | > $password: 表示密码 106 | 107 | **示例:** 108 | 109 | ```php 110 | $username = 'jiguang'; 111 | $password = 'password'; 112 | $response = $user->register($username, $password); 113 | ``` 114 | 115 | ### 批量注册用户 116 | 117 | 批量注册用户到极光 IM 服务器,一次批量注册最多支持 500 个用户。 118 | 119 | ```php 120 | $user->batchRegister(array $users); 121 | ``` 122 | 123 | **参数:** 124 | 125 | > $users: 表示将要注册的用户的信息的数组 126 | 127 | **示例:** 128 | 129 | ```php 130 | # 批量注册三个用户 131 | 132 | $users = [ 133 | ['username' => 'username0', 'password' => 'password0'], 134 | ['username' => 'username1', 'password' => 'password1'], 135 | ['username' => 'jiguang', 'password' => 'password'] 136 | ]; 137 | $response = $user->batchRegister($users); 138 | ``` 139 | 140 | ### 获取用户列表 141 | 142 | ```php 143 | $user->listAll($count, $start = 0); 144 | ``` 145 | 146 | **参数:** 147 | 148 | > $start: 开始的记录数 149 | 150 | > $count: 要获取的记录个数 151 | 152 | **示例:** 153 | 154 | ```php 155 | # 获取从编号 2 开始的 100 个记录的用户列表 156 | $response = $user->listAll(100, 2); 157 | ``` 158 | 159 | ### 获取用户信息 160 | 161 | ```php 162 | $user->show($username); 163 | ``` 164 | 165 | **参数:** 166 | 167 | > $username: 表示想要获取用户信息的用户名。 168 | 169 | **示例:** 170 | 171 | ```php 172 | $username = 'jiguang'; 173 | $response = $user->show($username); 174 | ``` 175 | 176 | ### 更新用户信息 177 | 178 | ```php 179 | $user->update($username, array $options); 180 | ``` 181 | 182 | **参数:** 183 | 184 | > $username: 表示想要更新其信息的用户的用户名 185 | 186 | > $options: 更新选项数组,表示需要更新的用户信息和值。支持 **nickname**、**avatar**、**birthday**、**signature**、**gender**、**region**、**address** 中的一个或多个 187 | 188 | 参数 | 意义 | 说明 189 | --- | --- | --- 190 | nickname | (选填)用户昵称 | 不支持的字符:英文字符: \n \r\n 191 | avatar | (选填)头像 | 需要填上从文件上传接口获得的 media_id 192 | birthday | (选填)生日 | example: 1990-01-24 yyyy-MM-dd 193 | signature |(选填)签名 | 支持的字符:全部,包括 Emoji 194 | gender | (选填) 性别 | 0 - 未知, 1 - 男 ,2 - 女 195 | region | (选填)地区 | 支持的字符:全部,包括 Emoji 196 | address | (选填)地址 | 支持的字符:全部,包括 Emoji 197 | 198 | **示例:** 199 | 200 | ```php 201 | # 更新用户名为 'jiguang' 的用户的 niackname 和 gender 202 | 203 | $username = 'jiguang'; 204 | $nickname = 'jpush'; 205 | 206 | $response = $user->update($username, ['nickname' => $nickname, 'gender' => 2]); 207 | ``` 208 | 209 | ### 查询用户在线状态 210 | 211 | ```php 212 | $user->stat($username) 213 | ``` 214 | 215 | **参数:** 216 | 217 | > $username: 表示想要查询在线状态的用户的用户名 218 | 219 | **示例:** 220 | 221 | ```php 222 | $username = 'jiguang'; 223 | $response = $user->stat($username); 224 | ``` 225 | 226 | ### 修改密码 227 | 228 | ```php 229 | $user->updatePassword($username, $password); 230 | ``` 231 | 232 | **参数:** 233 | 234 | > $username: 表示想要修改密码的用户的用户名 235 | 236 | > $password: 新密码 237 | 238 | **示例:** 239 | 240 | ```php 241 | 242 | $username = 'jiguang'; 243 | $new_password = 'newpassword'; 244 | $response = $user->updatePassword($username, $new_password); 245 | ``` 246 | 247 | ### 删除用户 248 | 249 | ```php 250 | $user->delete($username); 251 | ``` 252 | 253 | **参数:** 254 | 255 | > $username: 表示想要删除的用户的用户名 256 | 257 | **示例:** 258 | 259 | ```php 260 | # 删除用户名为 'jiguang' 的用户 261 | 262 | $username = 'jiguang'; 263 | $response = $user->delete($username); 264 | ``` 265 | 266 | ### 禁用用户 267 | 268 | ```php 269 | $user->forbidden($username,bool $enabled); 270 | ``` 271 | 272 | **参数:** 273 | 274 | > $username: 表示想要禁用的用户的用户名 275 | 276 | > $enabled: true 表示禁用用户,false 表示取消禁用用户,即激活用户 277 | 278 | **示例:** 279 | 280 | ```php 281 | # 禁用用户名为 'jiguang' 的用户 282 | 283 | $username = 'jiguang'; 284 | $response = $user->forbidden($username, true); 285 | ``` 286 | 287 | ### 获取用户的群组列表 288 | 289 | ```php 290 | $user->groups($username); 291 | ``` 292 | 293 | **参数:** 294 | 295 | > $username: 表示想要获取其群组列表的用户 296 | 297 | **示例:** 298 | 299 | ```php 300 | # 获取用户 'jiguang' 的群组列表 301 | 302 | $username = 'jiguang'; 303 | $response = $user->groups($username); 304 | ``` 305 | 306 | ### 获取用户聊天室列表 307 | 308 | ```php 309 | $user->chatrooms($username); 310 | ``` 311 | 312 | **参数:** 313 | 314 | > $username: 表示想要获取其聊天室列表的用户 315 | 316 | **示例:** 317 | 318 | ```php 319 | # 获取用户 'jiguang' 的聊天室列表 320 | 321 | $username = 'jiguang'; 322 | $response = $user->chatrooms($username); 323 | ``` 324 | 325 | ### 添加单聊免打扰 326 | 327 | ```php 328 | $user->addSingleNodisturb($touser, array $usernames); 329 | ``` 330 | 331 | **参数:** 332 | 333 | > $touser: 表示要设置免打扰的当前用户 334 | 335 | > $usernames: 表示要被添加单聊免打扰的用户名数组 336 | 337 | **示例:** 338 | 339 | ```php 340 | # 用户 'jiguang' 添加对用户 'username0' 和 'username1' 的单聊免打扰 341 | 342 | $touser = 'jiguang'; 343 | $usernames = ['username0', 'username1']; 344 | 345 | $response = $user->addSingleNodisturb($touser, $usernames); 346 | ``` 347 | 348 | ### 移除单聊免打扰 349 | 350 | ```php 351 | $user->removeSingleNodisturb($touser, array $usernames); 352 | ``` 353 | 354 | **参数:** 355 | 356 | > $touser: 表示要设置免打扰的当前用户 357 | 358 | > $usernames: 表示要被移除单聊免打扰的用户名数组 359 | 360 | **示例:** 361 | 362 | ```php 363 | # 用户 'jiguang' 移除对用户 'username0' 和 'username1' 的单聊免打扰 364 | 365 | $touser = 'jiguang'; 366 | $usernames = ['username0', 'username1']; 367 | 368 | $response = $user->removeSingleNodisturb($touser, $usernames); 369 | ``` 370 | 371 | ### 添加群聊免打扰 372 | 373 | ```php 374 | $user->addGroupNodisturb($touser, array $gids); 375 | ``` 376 | 377 | **参数:** 378 | 379 | > $touser: 表示要被设置免打扰的当前用户 380 | 381 | > $gids: 表示要被添加群聊免打扰的群组的 gid 数组 382 | 383 | **示例:** 384 | 385 | ```php 386 | # 用户 'jiguang' 添加对群组 'gid0' 和 'gid1' 的群聊免打扰 387 | $touser = 'jiguang'; 388 | $gid = ['gid0', 'gid1']; 389 | $response = $user->addGroupNodisturb($touser, $gids); 390 | ``` 391 | 392 | ### 移除群聊免打扰 393 | 394 | ```php 395 | $user->removeGroupNodisturb($touser, array $gids); 396 | ``` 397 | 398 | **参数:** 399 | 400 | > $touser: 表示要设置免打扰的当前用户 401 | 402 | > $gids: 表示要被移除群聊免打扰的群组的 gid 数组 403 | 404 | **示例:** 405 | 406 | ```php 407 | # 用户 'jiguang' 移除对群组 'gid0' 和 'gid1' 的群聊免打扰 408 | $touser = 'jiguang'; 409 | $gid = ['gid0', 'gid1']; 410 | $response = $user->removeGroupNodisturb($touser, $gids); 411 | ``` 412 | 413 | ### 开启全局免打扰 414 | 415 | ```php 416 | $user->openGlobalNodisturb($touser); 417 | ``` 418 | 419 | **参数:** 420 | 421 | > $touser: 表示要开启全局免打扰的用户 422 | 423 | **示例:** 424 | 425 | ```php 426 | # 用户 'jiguang' 开启全局免打扰 427 | $touser = 'jiguang'; 428 | $response = $user->openGlobalNodisturb($touser); 429 | ``` 430 | 431 | ### 关闭全局免打扰 432 | 433 | ```php 434 | $user->closeGlobalNodisturb($touser); 435 | ``` 436 | 437 | **参数:** 438 | 439 | > $touser: 表示要关闭全局免打扰的用户 440 | 441 | **示例:** 442 | 443 | ```php 444 | # 用户 'jiguang' 关闭全局免打扰 445 | $touser = 'jiguang'; 446 | $response = $user->closeGlobalNodisturb($touser); 447 | ``` 448 | 449 | ### 自定义免打扰 450 | 451 | > 自定义免打扰的设置参数比较复杂,建议使用上面所述的 6 个方法设置免打扰 452 | 453 | ```php 454 | $user->nodisturb($touser, array $options); 455 | ``` 456 | 457 | **参数:** 458 | 459 | > $touser: 表示要设置自定义免打扰的用户 460 | 461 | > $options: 自定义免打扰设置项数组 462 | 463 | **示例:** 464 | 465 | ```php 466 | # 自定义用户 'jiguang' 的免打扰设置 467 | # 添加用户 $user0 和 $user1 的单聊免打扰,移除用户 $user2 和 $user3 的单聊免打扰 468 | # 添加群组 $gid0 和 $gid1 的群聊免打扰,移除群组 $gid2 和 $gid3 的群聊免打扰 469 | # 关闭全局免打扰 470 | 471 | $username = 'jiguang'; 472 | $options = [ 473 | "single" => [ 474 | "add" => [$user0, $user1], 475 | "remove" => [$user2, $user3] 476 | ], 477 | "group" => [ 478 | "add" => [$gid0, $gid1], 479 | "remove" => [$gid2, $gid3] 480 | ], 481 | "global" => 0 482 | ]; 483 | 484 | $response = $user->nodisturb($touser, $options); 485 | ``` 486 | 487 | ### 群消息屏蔽 488 | 489 | #### 添加 490 | 491 | ```php 492 | $user->addGroupsShield($username, array $gids); 493 | ``` 494 | **参数:** 495 | 496 | > $username: 表示要设置屏蔽群消息的用户 497 | 498 | > $gids: 表示要添加群消息屏蔽的 gid 数组 499 | 500 | **示例:** 501 | 502 | ```php 503 | # 用户 'jiguang' 添加 gid 为 $gid0 和 $gid1 的群组的消息屏蔽 504 | $username = 'jiguang'; 505 | $options = [$gid0, $gid1]; 506 | $response = $user->addGroupsShield($username, $gids); 507 | ``` 508 | 509 | #### 移除 510 | 511 | ```php 512 | $user->removeGroupsShield($username, array $gids); 513 | ``` 514 | 515 | **参数:** 516 | 517 | > $username: 表示要设置屏蔽群消息的用户 518 | 519 | > $gids: 表示要移除群消息屏蔽的 gid 数组 520 | 521 | **示例:** 522 | 523 | ```php 524 | # 用户 'jiguang' 移除 gid 为 $gid2 和 $gid3 的群组的消息屏蔽 525 | $username = 'jiguang'; 526 | $options = [$gid2, $gid3]; 527 | $response = $user->removeGroupsShield($username, $gids); 528 | ``` 529 | 530 | ## Admin 管理员 531 | 532 | ```php 533 | use JMessage\IM\Admin; 534 | 535 | $admin = new Admin($client); 536 | ``` 537 | 538 | ### 管理员注册 539 | 540 | ```php 541 | $admin->register(array $info); 542 | ``` 543 | 544 | **参数:** 545 | 546 | > $info: 表示想要注册的管理员信息 547 | 548 | **示例:** 549 | 550 | ```php 551 | $info = [ 552 | 'username' => 'admin', 553 | 'password' => 'password' 554 | ]; 555 | $response = $admin->register($info); 556 | ``` 557 | 558 | ### 获取应用管理员列表 559 | 560 | ```php 561 | $admin->listAll($count, $start = 0); 562 | ``` 563 | 564 | **参数:** 565 | 566 | > $start: 起始记录位置 从 0 开始 567 | 568 | > $count: 查询条数 最多支持 500 条 569 | 570 | **示例:** 571 | 572 | ```php 573 | # 获取从编号 2 开始的 10 个记录的管理员 admin 列表 574 | $response = $admin->listAll(10, 2); 575 | ``` 576 | 577 | ## Blacklist 黑名单 578 | 579 | ```php 580 | use JMessage\IM\Blacklist; 581 | 582 | $blacklist = new Blacklist($client); 583 | ``` 584 | 585 | ### 黑名单列表 586 | 587 | ```php 588 | $blacklist->listAll($user); 589 | ``` 590 | 591 | **参数:** 592 | 593 | > $user:表示当前用户 594 | 595 | **示例:** 596 | 597 | ```php 598 | # 获取当前用户 'jiguang' 的黑名单列表 599 | $user = 'jiguang'; 600 | $response = $blacklist->listAll($user); 601 | ``` 602 | 603 | ### 添加黑名单 604 | 605 | ```php 606 | $blacklist->add($user, array $usernames); 607 | ``` 608 | 609 | **参数:** 610 | 611 | > $user:表示当前用户 612 | 613 | > $usernames:表示要加入到当前用户黑名单中的用户名数组 614 | 615 | **示例:** 616 | 617 | ```php 618 | # 把用户 'username0' 和 'username1' 添加到 'jiguang' 的黑名单中 619 | $user = 'jiguang'; 620 | $username = ['username0', 'username1']; 621 | 622 | $response = $blacklist->add($user. $username); 623 | ``` 624 | 625 | ### 移除黑名单 626 | 627 | ```php 628 | $blacklist->remove($user, array $usernames); 629 | ``` 630 | **参数:** 631 | 632 | > $user:表示当前用户 633 | 634 | > $usernames:表示要从当前用户的黑名单中移除的用户名数组 635 | 636 | **示例:** 637 | 638 | ```php 639 | # 把用户 'username0' 和 'username1' 从 'jiguang' 的黑名单中移除 640 | $user = 'jiguang'; 641 | $username = ['username0', 'username1']; 642 | 643 | $response = $blacklist->remove($user, $username); 644 | ``` 645 | 646 | ## Group 群组 647 | 648 | ```php 649 | use JMessage\IM\Group; 650 | 651 | $group = new Group($client); 652 | ``` 653 | 654 | ### 创建群组 655 | 656 | ```php 657 | $group->create($owner, $name, $desc, array $members, $avatar, $flag) 658 | ``` 659 | 660 | **参数:** 661 | 662 | > $owner: 表示群主的用名 663 | 664 | > $name: 表示群组的名字 665 | 666 | > $desc: 表示群组描述 667 | 668 | > $members: 表示群组成员的用户名数组 669 | 670 | > $avatar: (选填)群组头像,上传接口所获得的 media_id 671 | 672 | > $flag: (选填) 类型 673 | > * 1 - 私有群(默认) 674 | > * 2 - 公开群 675 | 676 | **示例:** 677 | 678 | ```php 679 | # 创建一个群名为 'jiguang group' 群主为 'jiguang' 的群 680 | 681 | $owner = 'jiguang'; 682 | $members = ['username0', 'username1']; 683 | $name = 'jiguang'; 684 | $desc 'jiguang group for developer'; 685 | 686 | $response = $group->create($owner, $name, $desc, $members); 687 | ``` 688 | 689 | ### 获取群组详情 690 | 691 | ```php 692 | $group->show($gid); 693 | ``` 694 | 695 | **参数:** 696 | 697 | > $gid: 群组 ID, 由创建群组时分配 698 | 699 | **示例:** 700 | 701 | ```php 702 | # 获取群组 ID 为 12345 的群组的详情 703 | 704 | $gid = 12345; 705 | $response = $group->show($gid); 706 | ``` 707 | 708 | ### 更新群组信息(群名 or 群描述) 709 | 710 | ```php 711 | $group->update($gid, $name, $desc, $avatar) 712 | ``` 713 | 714 | **参数:** 715 | 716 | > $gid: 群组 ID, 由创建群组时分配 717 | 718 | > $name: 新的群名 719 | 720 | > $desc: 新的群描述 721 | 722 | > avatar: 群组头像 media_id 723 | 724 | **示例:** 725 | 726 | ```php 727 | $gid = 12345; 728 | $name = 'new name'; 729 | $desc = 'new desc'; 730 | 731 | # 只更新群组 ID 为 12345 的群组的群名 732 | $response = $group->update($gid, $name); 733 | 734 | # 只更新群组 ID 为 12345 的群组的群描述 735 | $response = $group->update($gid, null, $desc); 736 | 737 | # 更新群组 ID 为 12345 的群组的群名和群描述 738 | $response = $group->update($gid, $name, $desc); 739 | ``` 740 | 741 | ### 删除群组 742 | 743 | ```php 744 | $group->delete($gid); 745 | ``` 746 | 747 | **参数:** 748 | 749 | > $gid: 群组 ID, 由创建群组时分配 750 | 751 | **示例:** 752 | 753 | ```php 754 | # 删除群组 ID 为 12345 的群组 755 | 756 | $gid = 12345; 757 | $response = $group->delete($gid); 758 | ``` 759 | 760 | ### 更新群组成员 761 | 762 | #### 添加群组成员 763 | 764 | ```php 765 | $response = $group->addMembers($gid, array $usernames); 766 | ``` 767 | 768 | **参数:** 769 | 770 | > $gid: 群组 ID, 由创建群组时分配 771 | 772 | > $usernames: 表示要添加到群组的用户数组 773 | 774 | **示例:** 775 | 776 | ```php 777 | # 添加用户 'username0', 'username1' 到群组 ID 为 12345 的群组 778 | 779 | $gid = 12345; 780 | $usernames = ['username0', 'username1']; 781 | $response = $group->addMembers($gid, $usernames); 782 | ``` 783 | 784 | #### 移除群组成员 785 | 786 | ```php 787 | $group->removeMembers($gid, array $usernames); 788 | ``` 789 | 790 | **参数:** 791 | 792 | > $gid: 群组 ID, 由创建群组时分配 793 | 794 | > $usernames: 表示要从群组中移除的用户数组 795 | 796 | **示例:** 797 | 798 | ```php 799 | # 把用户 'username0', 'username1' 从群组 ID 为 12345 的群组中移除 800 | 801 | $gid = 12345; 802 | $usernames = ['username0', 'username1']; 803 | 804 | $response = $group->removeMembers($gid, $usernames); 805 | ``` 806 | 807 | #### 更新群组成员 808 | 809 | > 建议使用上面所述的 2 个方法分别添加和移除群组成员。 810 | 811 | ```php 812 | $group->updateMembers($gid, array $options) 813 | ``` 814 | 815 | **参数:** 816 | 817 | > $gid: 群组 ID, 由创建群组时分配 818 | 819 | > $options: 表示更新群组选项 820 | 821 | **示例:** 822 | 823 | ```php 824 | # 添加用户 'username0', 'username1' 到群组 ID 为 12345 的群组, 同时把用户 'username2', 'username3' 从群组 ID 为 12345 的群组中移除 825 | 826 | $gid = 12345; 827 | $add = ['username0', 'username1']; 828 | $remove = ['username2', 'username3']; 829 | 830 | $response = $group->updateMembers($gid, [ 'add' => $add, 'remove' => $remove ]); 831 | ``` 832 | 833 | ### 获取群组成员列表 834 | 835 | ```php 836 | $group->members($gid); 837 | ``` 838 | 839 | **参数:** 840 | 841 | > $gid: 群组 ID, 由创建群组时分配 842 | 843 | **示例:** 844 | 845 | ```php 846 | # 获取群组 ID 为 12345 的群组的成员列表 847 | 848 | $gid = 12345; 849 | 850 | $response = $group->members($gid); 851 | ``` 852 | 853 | ### 获取当前应用的群组列表 854 | 855 | ```php 856 | $group->listAll($count, $start = 0); 857 | ``` 858 | 859 | **参数:** 860 | 861 | > $start: 开始的记录数 862 | 863 | > $count: 本次读取的记录数量,最大值为500 864 | 865 | **示例:** 866 | 867 | ```php 868 | # 获取从编号 2 开始的 100 个记录的群组列表 869 | $response = $group->listAll(100, 2); 870 | ``` 871 | 872 | ### 群成员禁言 873 | 874 | ```php 875 | $group->addSilence($gid, $usernames); 876 | ``` 877 | 878 | **参数:** 879 | 880 | > $gid: 群组 ID, 由创建群组时分配 881 | 882 | > $usernames: 表示要禁言的用户数组 883 | 884 | **示例:** 885 | 886 | ```php 887 | # 表示在群组 12345 中将用户名为 'username0', 'username1' 的用户禁言 888 | $response = $group->addSilence(12345, ['username0', 'username1']); 889 | ``` 890 | 891 | ```php 892 | $group->removeSilence($gid, $usernames); 893 | ``` 894 | 895 | **参数:** 896 | 897 | > $gid: 群组 ID, 由创建群组时分配 898 | 899 | > $usernames: 表示要取消禁言的用户数组 900 | 901 | **示例:** 902 | 903 | ```php 904 | # 表示在群组 12345 中将用户名为 'username0', 'username1' 的用户取消禁言 905 | $response = $group->removeSilence(12345, ['username0', 'username1']); 906 | ``` 907 | 908 | ### 移交群主 909 | 910 | ```php 911 | $group->updateOwner($gid, $username) 912 | ``` 913 | 914 | **参数:** 915 | 916 | > $gid: 群组 ID, 由创建群组时分配 917 | 918 | > $usernames: 表示要设置为群主的用户名 919 | 920 | **示例:** 921 | 922 | ```php 923 | # 表示将群组 12345 的群主设置为用户名为 'username0' 的用户 924 | $response = $group->updateOwner(12345, 'username0'); 925 | ``` 926 | 927 | ## Friend 好友 928 | 929 | ```php 930 | use JMessage\IM\Friend; 931 | 932 | $friend = new Friend($client); 933 | ``` 934 | 935 | ### 获取好友列表 936 | 937 | ```php 938 | $friend->listAll($user); 939 | ``` 940 | 941 | **参数:** 942 | 943 | > $user: 表示要获取其好友列表的用户 944 | 945 | **示例:** 946 | 947 | ```php 948 | # 获取用户 'jiguang' 的好友列表 949 | 950 | $user = 'jiguang'; 951 | 952 | $response = $friend->listAll($user); 953 | ``` 954 | 955 | ### 添加好友 956 | 957 | ```php 958 | $friend->add($user, array $friends); 959 | ``` 960 | 961 | **参数:** 962 | 963 | > $user: 表示要添加好友的用户 964 | 965 | > $friends: 表示要被添加到好友列表的用户数组 966 | 967 | **示例:** 968 | 969 | ```php 970 | # 用户 'jiguang' 把用户 'username0', 'username1' 添加为好友 971 | 972 | $user = 'jiguang'; 973 | $friends = ['username0', 'username1']; 974 | 975 | $response = $friend->add($user, $friends); 976 | ``` 977 | 978 | ### 删除好友 979 | 980 | ```php 981 | $friend->remove($user, array $friends); 982 | ``` 983 | 984 | **参数:** 985 | 986 | > $user: 表示要移除好友的用户 987 | 988 | > $friends: 表示要被从好友列表移除的用户数组 989 | 990 | 991 | **示例:** 992 | 993 | ```php 994 | # 用户 'jiguang' 删除好友 'username0', 'username1' 995 | 996 | $user = 'jiguang'; 997 | $friends = ['username0', 'username1']; 998 | 999 | $response = $friend->remove($user, $friends); 1000 | ``` 1001 | 1002 | ### 更新好友备注 1003 | 1004 | ```php 1005 | $group->updateNotename($user, array $options); 1006 | ``` 1007 | 1008 | **参数:** 1009 | 1010 | > $user: 表示要更新好友备注的用户 1011 | 1012 | > $options: 表示更新好友备注的选项数组 1013 | 1014 | **示例:** 1015 | 1016 | ```php 1017 | # 用户 'jiguang' 更新好友 'username0', 'username1' 的好友备注 1018 | 1019 | $user = 'jiguang'; 1020 | $options = [ 1021 | [ 1022 | 'username' => 'username0', 1023 | 'note_name' => 'username0_alias', 1024 | 'others' => 'good friend' 1025 | ], [ 1026 | 'username' => 'username1', 1027 | 'note_name' => 'username1_alias', 1028 | 'others' => 'normal friend' 1029 | ] 1030 | ]; 1031 | 1032 | $response = $friend->updateNotename($user, $options); 1033 | ``` 1034 | 1035 | ## Resource 媒体资源 1036 | 1037 | ```php 1038 | use JMessage\IM\Resource; 1039 | 1040 | $resource = new Resource($client); 1041 | ``` 1042 | 1043 | ### 资源上传 1044 | 1045 | ```php 1046 | $resource->upload($type, $path); 1047 | ``` 1048 | 1049 | **参数:** 1050 | 1051 | > $type: 表示要上传的资源类型,支持 'image' 、'voice' 和 'file' 三种资源类型 1052 | 1053 | > $path: 表示要上传的资源的全路径 1054 | 1055 | **示例:** 1056 | 1057 | ```php 1058 | $path = '/home/user/www/jiguang.png'; 1059 | 1060 | # 把图片 'jiguang.png' 作为图片上传 1061 | $response = $resource->upload('image', $path); 1062 | 1063 | # 把图片 'jiguang.png' 作为文件上传 1064 | $response = $resource->upload('file', $path); 1065 | ``` 1066 | 1067 | > 注:文件大小限制 8m,暂时只支持图片格式 jpg、bmp、gif、png 等 1068 | 1069 | ### 资源下载 1070 | 1071 | ```php 1072 | $resource->download($mediaId); 1073 | ``` 1074 | 1075 | **参数:** 1076 | 1077 | > $mediaId: 表示资源的 mediaId,包括用户的 avatar 字段,资源上传之后返回 1078 | 1079 | **示例:** 1080 | 1081 | ```php 1082 | $mediaId = 'xxxx'; 1083 | 1084 | $response = $resource->download($mediaId); 1085 | ``` 1086 | 1087 | ## 消息相关 1088 | 1089 | ```php 1090 | use JMessage\IM\Message; 1091 | 1092 | $message = new Message($client); 1093 | ``` 1094 | 1095 | ### 发送文本消息 1096 | 1097 | ```php 1098 | $message->sendText($version, array $from, array $target, array $msg, array $notification = [], array $options = []); 1099 | ``` 1100 | 1101 | **参数:** 1102 | 1103 | > $version: 版本号 1104 | 1105 | > $from: 发送者信息数组 1106 | 1107 | > $target: 接受者信息数组 1108 | 1109 | > $msg: 消息体数组 1110 | 1111 | > $notification: 自定义通知数组 1112 | 1113 | > $options: 其他选项数组 1114 | 1115 | **发送者信息数组 $from 说明:** 1116 | 1117 | 键 | 是否必须 | 含义 1118 | --- | --- | --- 1119 | type | 是 | 发送消息者身份 当前只限 admin 用户,必须先注册 admin 用户 1120 | id | 是 | 发送者的用户名 1121 | name | 否 | 发送者展示名 1122 | 1123 | **接受者信息数组 $target 说明:** 1124 | 1125 | 键 | 是否必须 | 含义 1126 | --- | --- | --- 1127 | type | 是 | 发送目标类型, 'single' - 个人,'group' - 群组, 'chatroom' - 聊天室 1128 | id | 是 | 目标 id, 'single' 填 username 'group' 填 Group Id 1129 | name | 否 | 接受者展示名 1130 | 1131 | **消息体数组 $msg 说明:** 1132 | 1133 | 键 | 是否必须 | 含义 1134 | --- | --- | --- 1135 | text | 是 | 消息内容 1136 | extras | 否 | 接受一个自定义键值对数组 1137 | 1138 | **自定义通知栏展示数组 $notification 说明** 1139 | 1140 | 键 | 是否必须 | 含义 1141 | --- | --- | --- 1142 | notifiable | 否 | 消息是否在通知栏展示 true 或者 false,默认为 true,表示在通知栏展示 1143 | title | 否 | 通知的标题 1144 | alert | 否 | 通知的内容 1145 | 1146 | **其他选项数组 $options 说明** 1147 | 1148 | 键 | 是否必须 | 含义 1149 | --- | --- | --- 1150 | offline | 否 | 消息是否离线存储 true 或者 false,默认为 true,表示需要离线存储 1151 | 1152 | ### 发送图片消息 1153 | 1154 | ```php 1155 | $message->sendImage($version, array $from, array $target, array $msg, array $notification = [], array $options = []); 1156 | ``` 1157 | 1158 | **参数:** 1159 | 1160 | > $version: 版本号,目前是 1 1161 | 1162 | > $from: 发送者信息数组(说明同上) 1163 | 1164 | > $target: 接受者信息数组(说明同上) 1165 | 1166 | > $msg: 消息体数组,格式是上传图片资源的返回值 1167 | 1168 | > $notification: 自定义通知栏展示数组(说明同上) 1169 | 1170 | > $options: 其他选项数组(说明同上) 1171 | 1172 | **消息体数组 $msg 说明:** 1173 | 1174 | > 为File Upload api返回的json 1175 | 1176 | 键 | 是否必须 | 含义 1177 | --- | --- | --- 1178 | media_id | 是 | 文件上传之后服务器端所返回的 key,用于之后生成下载的 url 1179 | media_crc32 | 是 | 文件的 crc32 校验码,用于下载大图的校验 1180 | width | 是 | 图片原始宽度 1181 | height | 是 | 图片原始高度 1182 | format | 是 | 图片格式 1183 | hash | 否 | 图片 hash 值 1184 | fsize | 是 | 文件大小(字节数) 1185 | 1186 | ### 发送语音消息 1187 | 1188 | ```php 1189 | $message->sendVoice($version, array $from, array $target, array $msg, array $notification = [], array $options = []); 1190 | ``` 1191 | 1192 | **参数:** 1193 | 1194 | > $version: 版本号,目前是1 1195 | 1196 | > $from: 发送者信息数组(说明同上) 1197 | 1198 | > $target: 接受者信息数组(说明同上) 1199 | 1200 | > $msg: 消息体数组,格式是上传语音资源的返回值 1201 | 1202 | > $notification: 自定义通知栏展示数组(说明同上) 1203 | 1204 | > $options: 其他选项数组(说明同上) 1205 | 1206 | **消息体数组 $msg 说明:** 1207 | 1208 | 键 | 是否必须 | 含义 1209 | --- | --- | --- 1210 | media_id | 是 | 文件上传之后服务器端所返回的 key,用于之后生成下载的 url 1211 | media_crc32 | 是 | 文件的 crc32 校验码 1212 | duration | 是 | 音频时长 1213 | hash | 否 | 音频 hash 值 1214 | fsize | 是 | 文件大小(字节数) 1215 | 1216 | ### 发送自定义消息 1217 | 1218 | ```php 1219 | $message->sendCustom($version, array $from, array $target, array $msg, array $notification = [], array $options = []); 1220 | ``` 1221 | 1222 | **参数:** 1223 | 1224 | > $version: 版本号 1225 | 1226 | > $from: 发送者信息数组(说明同上) 1227 | 1228 | > $target: 接受者信息数组(说明同上) 1229 | 1230 | > $msg: 消息体数组,接受一个自定义键值对数组 1231 | 1232 | > $notification: 自定义通知栏展示数组(说明同上) 1233 | 1234 | > $options: 其他选项数组(说明同上) 1235 | 1236 | ### 消息撤回 1237 | 1238 | ```php 1239 | $message->retract($msgid, $username); 1240 | ``` 1241 | 1242 | **参数:** 1243 | 1244 | > $msgid: 消息的 msgid 1245 | 1246 | > $username: 发送此消息的用户名 1247 | 1248 | ## SensitiveWord 敏感词 1249 | 1250 | ```php 1251 | use JMessage\IM\SensitiveWord; 1252 | 1253 | $sensitiveWord = new SensitiveWord($client); 1254 | ``` 1255 | 1256 | ### 获取敏感词列表 1257 | 1258 | ```php 1259 | $sensitiveWord->listAll($count, $start = 0); 1260 | ``` 1261 | 1262 | **参数:** 1263 | 1264 | > $start:起始记录位置 从 0 开始 1265 | 1266 | > $count:查询条数,最多 2000 1267 | 1268 | **示例:** 1269 | 1270 | ```php 1271 | # 获取从编号 2 开始的 10 个记录的敏感词列表 1272 | $response = $sensitiveword->listAll(10, 2); 1273 | ``` 1274 | 1275 | ### 添加敏感词 1276 | 1277 | ```php 1278 | $sensitiveWord->add(array $words); 1279 | ``` 1280 | 1281 | **参数:** 1282 | 1283 | > $words:表示敏感词数组 一个词长度最多为 10,默认最多支持 100 个敏感词 1284 | 1285 | **示例:** 1286 | 1287 | ```php 1288 | # 将敏感词 'f0'、'f1' 添加到敏感词列表 1289 | $words = ['f0', 'f1']; 1290 | $response = $sensitiveword->add($words); 1291 | ``` 1292 | 1293 | ### 删除敏感词 1294 | 1295 | ```php 1296 | $sensitiveWord->delete($word); 1297 | ``` 1298 | 1299 | **参数:** 1300 | 1301 | > $word: 表示要被删除的敏感词 1302 | 1303 | **示例:** 1304 | 1305 | ```php 1306 | # 将敏感词 'f0' 从敏感词列表删除 1307 | $words = 'f0'; 1308 | $response = $sensitiveword->delete($words); 1309 | ``` 1310 | 1311 | ### 修改敏感词 1312 | 1313 | ```php 1314 | $sensitiveWord->update($old, $new); 1315 | ``` 1316 | 1317 | **参数:** 1318 | 1319 | > $old: 表示旧敏感词 1320 | 1321 | > $new: 表示新敏感词 1322 | 1323 | **示例:** 1324 | 1325 | ```php 1326 | # 将敏感词 'f0' 修改为 'f1' 1327 | $response = $sensitiveword->update('f0', 'f1'); 1328 | ``` 1329 | 1330 | ### 获取敏感词功能状态 1331 | 1332 | ```php 1333 | $sensitiveWord->getStatus(); 1334 | ``` 1335 | 1336 | ### 更新敏感词功能状态 1337 | 1338 | ```php 1339 | $sensitiveWord->updateStatus(bool $opened); 1340 | ``` 1341 | 1342 | **参数:** 1343 | 1344 | > $opened: 表示敏感词开关状态, true 表示开启过滤, false 表示关闭过滤 1345 | 1346 | **示例:** 1347 | 1348 | ```php 1349 | # 关闭敏感词过滤 1350 | $opened = false; 1351 | $response = $sensitiveword->updateStatus($opened); 1352 | ``` 1353 | 1354 | ## ChatRoom 聊天室 1355 | 1356 | ```php 1357 | use JMessage\IM\ChatRoom; 1358 | 1359 | $room = new ChatRoom($client); 1360 | ``` 1361 | 1362 | ### 获取当前应用的聊天室列表 1363 | 1364 | ```php 1365 | $room->listAll($count, $start = 0); 1366 | ``` 1367 | 1368 | **参数:** 1369 | 1370 | > $start: 开始的记录数 1371 | 1372 | > $count: 本次读取的记录数量 1373 | 1374 | ### 创建聊天室 1375 | 1376 | ```php 1377 | $room->create($name, $owner, array $members = [], $description = null) 1378 | ``` 1379 | 1380 | **参数:** 1381 | 1382 | > $name: 表示聊天室的名字 1383 | 1384 | > $owner: 聊天室拥有者 1385 | 1386 | > $members: 表示聊天室成员的用户名数组 1387 | 1388 | > $description: 表示聊天室描述 1389 | 1390 | ### 获取聊天室详情 1391 | 1392 | ```php 1393 | $room->show($roomId); 1394 | # OR 1395 | $room->showBatch(array $roomIds); 1396 | ``` 1397 | 1398 | **参数:** 1399 | 1400 | > $roomId: 聊天室 ID 1401 | 1402 | > $roomIds: 聊天室 ID 数组 1403 | 1404 | ### 更新聊天室信息 1405 | 1406 | ```php 1407 | $room->update($roomId, array $options) 1408 | ``` 1409 | 1410 | **参数:** 1411 | 1412 | > $roomId: 聊天室 ID 1413 | 1414 | > $options: 表示更新聊天室的选项数组,支持 `name`(新的名称)、`description`(新的描述)和`owner_username`(聊天室拥有者)三个参数 1415 | 1416 | ### 删除聊天室 1417 | 1418 | ```php 1419 | $room->delete($roomId); 1420 | ``` 1421 | 1422 | **参数:** 1423 | 1424 | > $roomId: 聊天室 ID 1425 | 1426 | ### 修改用户禁言状态 1427 | 1428 | ```php 1429 | $room->forbiddenUser($roomId, $user, bool $enabled) 1430 | ``` 1431 | 1432 | **参数:** 1433 | 1434 | > $roomId: 聊天室 ID 1435 | 1436 | > $user: 表示想要禁言的用户的用户名 1437 | 1438 | > $enabled: true 表示禁言,false 表示取消禁言 1439 | 1440 | ### 获取聊天室成员列表 1441 | 1442 | ```php 1443 | $room->members($roomId, $count, $start = 0); 1444 | ``` 1445 | 1446 | ### 添加聊天室成员 1447 | 1448 | ```php 1449 | $room->addMembers($roomId, array $usernames); 1450 | ``` 1451 | 1452 | **参数:** 1453 | 1454 | > $roomId: 聊天室 ID 1455 | 1456 | > $usernames: 表示要添加到聊天室的用户数组 1457 | 1458 | ### 移除聊天室成员 1459 | 1460 | ```php 1461 | $room->removeMembers($roomId, array $usernames); 1462 | ``` 1463 | 1464 | **参数:** 1465 | 1466 | > $roomId: 聊天室 ID 1467 | 1468 | > $usernames: 表示要从聊天室中移除的用户数组 1469 | 1470 | ## 证书问题 1471 | 1472 | > 即常见的 cURL 60 错误,`SSL certificate problem: unable to get local issuer certificate` 一般建议自行安装安全证书配置机器进行解决,下面的方式**不是**推荐方式。 1473 | 1474 | ```php 1475 | use JMessage\JMessage; 1476 | 1477 | $appKey = 'xxxx'; 1478 | $masterSecret = 'xxxx'; 1479 | 1480 | // 禁用 SSL 证书的验证 1481 | $client = new JMessage($appKey, $masterSecret, [ 'disable_ssl' => true ]); 1482 | ``` 1483 | 1484 | > **希望开发者在了解相关风险的前提下如此处理 SSL 证书问题。** 1485 | --------------------------------------------------------------------------------