How to store the emoji expression in the nickname of WeChat user information interface in PHP into mysql?
天蓬老师
天蓬老师 2017-05-16 13:06:54
0
3
953

As shown in the title: It is currently confirmed that the obtained string is converted to unicode encoding and stored in the table. The encoding of the table is utf-8; the problem encountered now is: if the json_encode() function is used to escape multiple times and then stored The string in the database has several layers of double quotes. If it is escaped only once and stored in the database, although there are no quotes, the symbols are missing, as shown below, followed by the key code. Newbies can consult on the correct way to store;

I saw answers that you need to change the database character set to: utf8mb4. I would like to know if it is not possible to use utf-8 instead of this character set? Newbies only know how to modify the global character set in my.ini, but don't know how to modify this field.

Latest update: After testing, although utf8mb4 can store data in the database, there is still a problem: if there are expressions before and after the WeChat nickname, and there is text in the middle, only the first expression can be saved in the database, and the last one can be saved in the database. expression turned into spaces. After several twists and turns, I still used utf8 to save the string selected in the blue bar in the picture below. The string was de-processed on the front end to ensure that the user's nickname is not destroyed. If you have more information, Good ideas are welcome to leave a message

< /p>

Key code:

function weixininfo($code){
    $userinfo = getJson($get_user_info_url);
  $wxif = new wxinfo($userinfo["openid"],$userinfo["nickname"],$userinfo["sex"],$userinfo["headimgurl"]);
  class Emp {};
  $obj = new Emp();
  $obj->result = 0;
  $obj->info = $wxif;
  return $obj;
}

$code = $_GET['code'];
$data = weixininfo($code);
$info = $data->info;
$openid = $info->openid;
$nickname = $info->nickname;
$name = json_encode($nickname);
$sex = $info->sex;
$headurl = $info->headurl;
$sqls = "INSERT INTO mytest(openid,nickname,sex,headurl) VALUES ('$openid',$name,'$sex','$headurl')";
if($conn->query($sqls) === true){
    session_start();
    $_SESSION["openid"]=$openid;
    $conn->close();
    // $uri ="./index.php";
    // header( "Location: $uri" );
}else{
    echo "Failed:".$sqls."<br>".$conn->error;
};
function getJson($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}
class wxinfo {
  public $openid;
  public $nickname;
  public $sex;
  public $headurl;

  public function __construct($openid,$nickname,$sex,$headurl){
    $this->openid = $openid;
    $this->nickname = $nickname;
    $this->sex = $sex;
    $this->headurl = $headurl;
  }
}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
仅有的幸福
  1. mysql field set to utf8mb4-utf8mb4-general_ci

  2. php

<?php
json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
phpcn_u1582

If the table supports utf8mb4, change it to utf8mb4. If it is not useful, use utf32. utf8 only supports a small number of emoji expressions

Before querying, execute $conn->query("set names utf8") to ensure that the data transmission process is not garbled

emoji are also characters, there is no need to convert them to json

我想大声告诉你

utf-8 uses 3 bytes for storage, while emoji characters have 4 bytes, so converting utf-8 to utf8mb4 will solve it

Also note: the mysql version must be v5.5.3 or higher

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template