PHP code example of playable-cn / im

1. Go to this page and download the library: Download playable-cn/im library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

playable-cn / im example snippets


php bin/hyperf.php vendor:publish playable-cn/im

SDK_APP_ID=
IDENTIFIER=
SECRET_KEY=
REGION=
// REGION 默认zh,还可以配置:sgp、kr、ger、ind、usa



declare(strict_types=1);

namespace App\Controller;

use Hedeqiang\IM\Exceptions\Exception;
use Hedeqiang\IM\Exceptions\HttpException;
use Hedeqiang\IM\IM;
use Hyperf\Config\Annotation\Value;

class IndexController extends AbstractController
{
    /**
     * @Value(key="im")
     */
    protected $config;

    public function index()
    {
        $config = $this->config;
        $im = new IM($config);
        $params = [
            'To_Account' => ['hedeqiang']
        ];
        
        print_r($im->send('openim','querystate',$params));
    }
}


{
	"ActionStatus": "OK",
	"ErrorInfo": "",
	"ErrorCode": 0,
	"QueryResult": [{
		"To_Account": "hedeqiang",
		"State": "Offline"
	}]
}

$params = [
    'From_Account' => 'hedeqiang',
        'ProfileItem' => [
            ['Tag' => 'Tag_Profile_IM_Nick', 'Value' => 'hedeqiang'],
            ['Tag' => 'Tag_Profile_IM_Gender', 'Value' => 'Gender_Type_Male'],
            ['Tag' => 'Tag_Profile_IM_BirthDay', 'Value' => 19940410],
            ['Tag' => 'Tag_Profile_IM_SelfSignature', 'Value' => '程序人生的寂静欢喜'],
            ['Tag' => 'Tag_Profile_IM_Image', 'Value' => 'https://upyun.laravelcode.cn/upload/avatar/1524205770e4fbfbff-86ae-3bf9-b7b8-e0e70ce14553.png'],
        ],
];

print_r($im->send('profile','portrait_set',$params));

{
	"ActionStatus": "OK",
	"ErrorCode": 0,
	"ErrorInfo": "",
	"ErrorDisplay": ""
}

$params = [
    'SyncOtherMachine' => 1, // 消息不同步至发送方
    'From_Account' => 'hedeqiang',
    'To_Account' => 'zhangsan',
    'MsgRandom' => 1287657,
    'MsgTimeStamp' => 1557387418,
    'MsgBody' => [
        [
            'MsgType' => 'TIMTextElem',
            'MsgContent' => [
                'Text' => '晚上去撸串啊'
            ]
        ]
    ]
];

print_r($im->send('openim','sendmsg',$params));

{
    "ActionStatus":"OK",
    "ErrorInfo":"",
    "ErrorCode":0,
    "MsgTime":1573179125,
    "MsgKey":"748144182_1287657_1573179125"
}

{
    "From_Account":"id",
    "ProfileItem":
    [
        {
            "Tag":"Tag_Profile_IM_Nick",
            "Value":"MyNickName"
        }
    ]
}