1. 程式人生 > >網易通訊與視頻

網易通訊與視頻

self 問題 date ati join 參數 做的 result serve

http://www.upwqy.com/details/104.html

最近做的一個項目.其中涉及到音視頻, 開始使用的是環信,但時客戶需要在,視頻通話中,,附帶美顏效果.環信這邊聯系客戶好像沒有這一塊.所以又重新找了一個平臺:網易雲通訊與視頻

文檔地址:

http://dev.netease.im/docs/product/IM%E5%8D%B3%E6%97%B6%E9%80%9A%E8%AE%AF/%E6%9C%8D%E5%8A%A1%E7%AB%AFAPI%E6%96%87%E6%A1%A3/%E6%8E%A5%E5%8F%A3%E6%A6%82%E8%BF%B0

接口使用的過程 ,最主要的是一個checksum的生成,和請求參數的構造.

checksum的生成

/**
 * 生成 checksum
 */
private function getCheckSum(){
    $hex_digits = self::HEX_DIGITS;
    $this->nonce;
    for($i=0;$i<128;$i++){
        //隨機字符串最大128個字符,也可以小於該數
        $this->nonce.= $hex_digits[rand(0,15)];
    }
    $this->curTime = (string)(time());  //當前時間戳,以秒為單位

    $join_string
= $this->appSecret.$this->nonce.$this->curTime; $this->checkSum = sha1($join_string); }

請求參數的構造.

$postDataArray = array();
foreach ($data as $key=>$value){
    array_push($postDataArray, $key.‘=‘.urlencode($value));
}
$postData = join(‘&‘, $postDataArray);
 

以下演示了幾個實例,其他的api基本上都是一樣的,只要上面的配置弄好,基本上沒啥問題

<?php
/**
 * Created by PhpStorm.
 * User: [一秋]
 * Date: 2018/2/2
 * Time: 16:22
 * Desc: 成功源於點滴
 */

namespace cmf\service;

//網易音視頻

class WangYiService{
    private $appKey;
    private $appSecret;
    private $nonce;
    private $curTime;
    private $checkSum;
    const   HEX_DIGITS = "0123456789abcdef";

    public function __construct()
    {
        $this->appKey = ‘‘;
        $this->appSecret = ‘‘;
    }

    /**
     * 新建通訊用戶id
     * @return array
     */
    public function createAccid($accid,$token){
        $url = ‘https://api.netease.im/nimserver/user/create.action‘;
        $data= array(
            ‘accid‘ => $accid,
            ‘token‘=>$token
        );
        $result = $this->postDataCurl($url,$data);
        return $result;

    }

    /**
     * 更新用戶基本信息
     * @return array
     */
    public function updateAccid($accid){

        $url = ‘https://api.netease.im/nimserver/user/update.action‘;
        $data= array(
            ‘accid‘ => $accid
        );
        $result = $this->postDataCurl($url,$data);
        return $result;
    }

    /**
     * 更新token值
     * @param $accid
     * @return array
     *
     */
    public function refreshToken($accid){
        $url = ‘https://api.netease.im/nimserver/user/refreshToken.action‘;
        $data= array(
            ‘accid‘ => $accid
        );
        $result = $this->postDataCurl($url,$data);
        return $result;
    }

    /**
     * 封禁網易雲通信ID
     */
    public function blockAccid($accid){
        $url = ‘https://api.netease.im/nimserver/user/block.action‘;
        $data= array(
            ‘accid‘ => $accid,
            ‘needkick‘=>true  //是否踢掉被禁用戶,true或false,默認false
        );
        $result = $this->postDataCurl($url,$data);
        return $result;
    }

    /**
     * 解禁網易雲通信ID
     */
    public function unblockAccid($accid){
        $url = ‘https://api.netease.im/nimserver/user/unblock.action‘;
        $data= array(
            ‘accid‘ => $accid
        );
        $result = $this->postDataCurl($url,$data);
        return $result;
    }



    public function postDataCurl($url,$data){
        $this->getCheckSum();       //發送請求前需先生成checkSum

        $timeout = 5000;
        $http_header = array(
            ‘AppKey:‘.$this->appKey,
            ‘Nonce:‘.$this->nonce,
            ‘CurTime:‘.$this->curTime,
            ‘CheckSum:‘.$this->checkSum,
            ‘Content-Type:application/x-www-form-urlencoded;charset=utf-8‘
        );

        $postDataArray = array();
        foreach ($data as $key=>$value){
            array_push($postDataArray, $key.‘=‘.urlencode($value));
        }
        $postData = join(‘&‘, $postDataArray);

        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt ($ch, CURLOPT_HEADER, false );
        curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //處理http證書問題
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);
        if (false === $result) {
            $result =  curl_errno($ch);
        }
        curl_close($ch);
        return $this->json_to_array($result);
    }


    /**
     * 生成 checksum
     */
    private function getCheckSum(){
        $hex_digits = self::HEX_DIGITS;
        $this->nonce;
        for($i=0;$i<128;$i++){
            //隨機字符串最大128個字符,也可以小於該數
            $this->nonce.= $hex_digits[rand(0,15)];
        }
        $this->curTime = (string)(time());  //當前時間戳,以秒為單位

        $join_string = $this->appSecret.$this->nonce.$this->curTime;
        $this->checkSum = sha1($join_string);
    }

    /**
     * json轉數組
     */
    private function json_to_array($json_str){

        if(is_array($json_str) || is_object($json_str)){
            $json_str = $json_str;
        }else if(is_null(json_decode($json_str))){
            $json_str = $json_str;
        }else{
            $json_str =  strval($json_str);
            $json_str = json_decode($json_str,true);
        }
        $json_arr=array();
        foreach($json_str as $k=>$w){
            if(is_object($w)){
                $json_arr[$k]= $this->json_to_array($w); //判斷類型是不是object
            }else if(is_array($w)){
                $json_arr[$k]= $this->json_to_array($w);
            }else{
                $json_arr[$k]= $w;
            }
        }
        return $json_arr;
    }

    private function gengerateToken(){
        return  md5(uniqid()) . md5(uniqid());
    }
}

網易通訊與視頻