网易云信im即时通讯,php网易云信im即时通讯,tp,demo,即时通讯

这篇博客介绍如何使用PHP进行网易云信IM即时通讯的开发,包括配置appKey和appSecret,以及服务端和Web端的联调。提供了公共类Common.php和Nim.php的说明,Nim.php需要继承Common。此外,还提到了HTML测试demo页面,JS文件可在官方下载。
摘要由CSDN通过智能技术生成

 请先配置appKey和appSecret     本demo实现了服务端和web的联调(系统通知)

服务端参考文档:开发者中心

WEB参考文档:开发者中心

目录

 1.公共类 Common.php

2.  Nim.php 需要继承Common

3. HTML 测试demo页面  js文件请在官方网站自行下载


 1.公共类 Common.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/1/25
 * Time: 14:11
 */
namespace app\nim\controller;
use \think\Controller;
class Common extends Controller{

    public $appKey="";//开发者平台分配的 appkey
    public $appSecret="";//开发者平台分配的 appSecret
    public $accid;//IM 账号
    public $nim_name;//redis键名
    public $input;
    public $nonce;//随机数(最大长度128个字符)
    public $checkSum;//SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
    public $time;//当前时间戳
    public function _initialize()
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
        $this->input=input('');
        $this->accid=session_id();//账户 用于测试
        $this->nim_name='nim_token_'.$this->accid;
        $this->nonce=$this->rand_code(16);
        $this->time=time();
        $this->checkSum=$this->nim_hearder_init($this->time,$this->nonce);
    }
    /**
     * Notes:生成 checkSum  请求头初始化
     * User: ZHOU WEI YUN
     * Date: 2022/1/24 0024
     * Time: 下午 2:04
     * @param $curtime int 当前UTC时间戳
     * @param $nonce   string 随机数(最大长度128个字符)
     * @return string
     */
    public function nim_hearder_init($curtime,$nonce){
        $appSecret=$this->appSecret;
        return strtolower(sha1($appSecret.$nonce.$curtime,false));
    }


    /* 随机字符串
	*  $strlen  随机数长度
	*/
    public function rand_code($strlen=4){
        $result='';
        $str='abcdefghijklmnopqrstuvwxyz0123456789';//ABCDEFGHIJKLMNOPQRSTUVWXYZ
        for($i=0;$i<$strlen;$i++){
            $result.=substr($str,mt_rand(0,strlen($str)-$i),1);
            //$result.=$str[random_int(0,strlen($str)-$i)];
        }
        return $result;
    }

    public function post_url($url,$urlencodedStr)
    {
        $arr=[
            'Content-type:application/x-www-form-urlencoded',
            'AppKey:'. $this->appKey,
            'Nonce:'. $this->nonce,
            'CurTime:'. $this->time,
            'CheckSum:'. $this->checkSum,
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($urlencodedStr));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//去掉https验证
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

}

2.  Nim.php 需要继承Common

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/30
 * Time: 14:10
 */
namespace app\nim\controller;

class Nim extends Common
{
    //https://doc.yunxin.163.com/docs/TM5MzM5Njk/DEwMTE3NzQ?platformId=60353 文档

    //产生网易云通信ID
    public function create_accid()
    {
        $return=[
            'code'=>400,
            'msg'=>'请求失败',
            'data'=>[
                'appkey'=>$this->appKey,
                'token'=>'',
                'account'=>$this->accid
            ],
        ];
        $redis=get_redis();
        if(!$redis){
            return  $return['msg']='redis连接失败';
        }

        $token=$redis->get($this->nim_name);
        if(empty($token)){
            $url = "https://api.netease.im/nimserver/user/create.action";//获取
            $urlencodedStr_arr=array();
            $urlencodedStr_arr['accid']=$this->accid;//自定义(传入即可)
            $re=json_decode($this->post_url($url,$urlencodedStr_arr),true);
            if($re['code']===200){
                $return['msg']='请求成功';
                $return['code']=200;
                $return['data']['token']=$re['info']['token'];
                $redis->set($this->nim_name,$re['info']['token'],600);
            }else{
                return $this->refresh_token();//重置token
            }
        }else{
            $return['msg']='请求成功';
            $return['code']=200;
            $return['data']['token']=$token;
        }
        return $return;
    }

    /**
     * Notes:主动更新 token
     * User: ZHOU WEI YUN
     * Date: 2022/1/24 0024
     * Time: 下午 3:43
     * @param array
     * @return array|string
     */
    public function refresh_token()
    {
        $return=[
            'code'=>400,
            'msg'=>'请求失败',
            'dat
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值