http://www.upwqy.com/details/104.html
最近做的一个项目.其中涉及到音视频, 开始使用的是环信,但时客户需要在,视频通话中,,附带美颜效果.环信这边联系客户好像没有这一块.所以又重新找了一个平台:网易云通讯与视频
文档地址:
接口使用的过程 ,最主要的是一个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()); } }