php调用微信用户信息接口昵称里面的emoji表情怎么存储到mysql中?
天蓬老师
天蓬老师 2017-05-16 13:06:54
0
3
803

如题:目前确定是将获取到的字符串转为unicode编码存储到表中,表的编码是utf-8;现在遇到的问题是:如果使用json_encode()函数多次转义后存储到数据库中的字符串多了好几层的双引号,如果只转义一次存储到数据库中虽然没有了引号,但是 符号不见了,如下图,后面附关键代码,新手咨询存储的正确方式;

看到回答都是需要将数据库字符集改为:utf8mb4,想知道不用这个字符集用utf-8不可以吗?新手只知道在my.ini修改全局的字符集,不知道怎么针对这个字段来修改啊。

最新更新:经过测试虽然utf8mb4能够将数据存储到数据库中,但是还是有问题的:如果微信昵称前后面都有表情,中间有文字的时候,数据库中只能将前面的表情保存,后面的表情变成了空格,经过几番周折还是使用了utf8来保存下图蓝色条选中的那样字符串来保存,在前端对字符串进行去"处理,保证用户的昵称不被破坏,如果各位有更好的办法欢迎留言。

关键代码:

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 "失败:".$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;
  }
}
天蓬老师
天蓬老师

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!