php code implements the method of modifying the nickname: 1. Find the image format; 2. Save the file on the server where the project is located; 3. Get the configuration information; 4. Verify the referenced sdk and upload the image to the server, update the avatar Just a nickname.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to modify the nickname in php code?
Easyly implement the modification of the avatar nickname in the source code of the PHP live broadcast platform
In the development of the source code of the php live broadcast platform , Jiguang Push and Jiguang IM are often used to implement message push functions and real-time communication functions. If you need to update user information and other functions, many friends will find that the update will only update the database information and will not be updated to Jiguang simultaneously. server?
Then let’s talk about it next. If Jiguang SDK is connected to the PHP live broadcast platform source code, how to update Jiguang user avatar and nickname.
The basic steps are as follows:
1. Upload image format: png, jpg or jpeg
2. Upload and save the image to be updated in the project In the specified directory of the server where you are located
3. Reference Jiguang sdk and other related information, upload the pictures in the known directory to Jiguang, and update the user avatar
1. First: ensure the uploaded pictures The format needs to meet the requirements of the Aurora avatar. Specifically look for the Aurora picture format. The format used here is png, jpg or jpeg.
2. Step 2: Save the file on the server where the project is located. The code is as follows:
$image =$_FILES['file']; //设置上传路径,我把它放在了PHP直播平台源码中,upload下的jmessage目录下(需要在linux中给interview设置文件夹权限) if($image['name']!=''){ $type = strrchr($image['name'], "."); $path = "./public/jmessage/" . $image['name']; //判断上传的文件是否为图片格式 if (strtolower($type) == '.png' || strtolower($type) == '.jpg' || strtolower($type) == '.jpeg') { //将图片文件移到该目录下 move_uploaded_file($image['tmp_name'], $path); }else{ $this->error('请上传后缀为png,jpg或jpeg的图片'); } }
3. Step 3: Get the Aurora configuration information. Here we save it to the unified configuration
$configpri=getConfigPri(); $app_key = $configpri['jpush_key']; //极光key $master_secret = $configpri['jpush_secret']; //极光secret
in the information table 4. Verify that the Aurora sdk is referenced and upload the image to the Aurora server, update the avatar and nickname, and set the source code of the PHP live broadcast platform as follows
if($app_key && $master_secret){ Vendor("JMessage.autoload"); $jm = new\JMessage\JMessage($app_key, $master_secret); $user = new \JMessage\IM\User($jm); $username = $id; //极光用户名 if($image['name']!=''){ $rescource = new \JMessage\IM\Resource($jm); //把图片上传到极光 $response = $rescource->upload('image', $path); //更改极光平台用户的名称头像 $respon=$user->update($username,['nickname'=> “你想要更新的昵称”,'avatar'=>$response['body']['media_id']]); unlink($path); //删除本地图片 }else{ //更改极光平台用户的名称 $respon=$user->update($username,['nickname'=>“你想要更新的昵称”]); } //查询用户信息,可打印此信息查看是否更新成功 $userinfo = $user->show($username); } $this->success('修改成功'); }else{ $this->error('修改失败'); }
Note: The configuration information in the above code can be The variable data value can be adjusted according to project needs
The function of updating the avatar and nickname of the Aurora user is now completed.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to modify nickname in php code. For more information, please follow other related articles on the PHP Chinese website!