表情符号从 Angularjs $http 到 PHP json_decode 并存储在 mySQL

Emoji from Angularjs $http to PHP json_decode and store in mySQL

所以我试图将表情符号存储在我的数据库中,并 return 它们在我的应用程序中。我用 ionic v1 构建了一个小信使,一切正常。我只想正确存储表情符号并在应用程序中显示它们。我的整个数据库、表和列都设置为 utf8mb4,所以我知道这很好。当我将它与 $http 一起发送到我的 php 时,它看起来也不错:{"from_account_id":"xxxx","for_account_id":"xxxx","message":""}: 但是当我在 php 中打印它时,它看起来像这样:{"from_account_id":"106191","for_account_id":"989014","message":"đ˝"} 并且在我的 mysql 到处都放了一个表情符号,它显示 ?。我还能做什么?我还将我的 php 中的 header 设置为 UTF_8。我知道对此有很多疑问,但我已经关注了其中的大部分问题,但它仍然没有解决我的问题。

我的 php 我存储消息的地方 :

<?php
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
include_once 'config.php';

$data = file_get_contents("php://input");
$data = json_decode($data, true);

try {
    $stmt = $dbh->prepare("INSERT INTO `messages` (`message_id`, `from_account_id`, `for_account_id`, `date_read`, `message`)
      VALUES (NULL, :from_account_id, :for_account_id, NULL, :message)");
    $stmt->bindValue(':from_account_id', $data['from_account_id']);
    $stmt->bindValue(':for_account_id', $data['for_account_id']);
    $stmt->bindValue(':message', $data['message']);
    $stmt->execute();
  } catch (PDOException $e) {
          throw $e;
  }
  echo 'Success';

?>

我的 AngularJS 发送消息的函数 :

  $scope.sendMessage = function(for_account_id, message){
    if(for_account_id != null && message != null){
    $http({
      method: 'post',
      url: 'myurl',
      data: {
      from_account_id: $rootScope.unique_id,
      for_account_id: for_account_id,
      message: message,
      },
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(data){
      $scope.thisMessage = null;
      $scope.getMessages();
      $rootScope.webSocket.send($stateParams.chatId + "|" + $rootScope.unique_id  + "|" + 'newMessage');
      $scope.markAsRead();
    })
  }
  }

这是我与数据库的连接:

<?php
$hostname='myhost';
$username='myuser';
$password='mypassword';

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8'); 

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=mydbname;charset=utf8mb4",$username,$password, $options);
    $dbh->query ('SET NAMES utf8');
    $dbh->query ('SET CHARACTER_SET utf8_unicode_ci');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
      echo $e->getMessage();
    }
?>

我想念什么提示或想法?

好的,我已经解决了我的问题。我这样做的方式可能不是最好的,但它确实有效。所以基本上在数据库中存储 unicode 就这么简单:

  1. 我将要插入数据的文件中的 header 更改为:

    header('Content-Type: application/json; charset=utf-8');

  2. 我再次将消息编码为 json,同时像这样插入:

    $stmt->bindValue(':message', json_encode($data['message']));

  3. 在再次响应我的应用程序之前将 header 设置为:

    header('Content-Type: application/json; charset=utf-8');

我的代码现在看起来像这样:

<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
include_once 'config.php';

$data = file_get_contents("php://input");
$data = json_decode($data, true);

try {
    $stmt = $dbh->prepare("INSERT INTO `messages` (`message_id`, `from_account_id`, `for_account_id`, `date_read`, `message`)
      VALUES (NULL, :from_account_id, :for_account_id, NULL, :message)");
    $stmt->bindValue(':from_account_id', $data['from_account_id']);
    $stmt->bindValue(':for_account_id', $data['for_account_id']);
    $stmt->bindValue(':message', json_encode($data['message']));
    $stmt->execute();
  } catch (PDOException $e) {
    header('Content-Type: text/html; charset=utf-8');
          throw $e;
  }
  header('Content-Type: text/html; charset=utf-8');
  echo 'Success';

?>

所以这个问题已经解决,并且 unicode 存储在我的数据库中。

然后我又陷入了尴尬的境地,因为在从 mysql 获取数据并将其编码为 json 并发送到我的应用程序时,我的应用程序无法 return unicodes对于特殊字符,即使我用 ng-bind-html 绑定它并使用 $sce.trustAsHtml()。通常我只是将我的查询结果作为 encoded json 发送,但它没有像我说的那样工作。所以我所做的就是这样的:

<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: POST, GET');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
include_once 'config.php';

$data = file_get_contents("php://input");
$data = json_decode($data, true);

try {
  $stmt = $dbh->prepare("SELECT * from messages WHERE (from_account_id = :unique_id AND for_account_id = :user_2) OR (from_account_id = :user_2 AND for_account_id = :unique_id)");
  $stmt->bindValue(':unique_id', $data['user_1']);
  $stmt->bindValue(':user_2', $data['user_2']);
  $stmt->execute();
  } catch (PDOException $e) {
          throw $e;
      }

  $rows = $stmt->rowCount();
  $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

  if ($rows > 0){
    $i = 0;
    echo "[";
    foreach ($result as $key => $value){
      $i++;
      echo "{";
      echo '"message_id"' . ":" . '"' .$value['message_id']. '"' .", ";
      echo '"from_account_id"' . ":" . '"' .$value['from_account_id']. '"' .", ";
      echo '"for_account_id"' . ":". '"' .$value['for_account_id']. '"' .", ";
      echo '"date_sent"' . ":" . '"' .$value['date_sent']. '"' .", ";
      echo '"date_read"' . ":" . '"' .$value['date_read']. '"' .", ";
      echo '"message"' . ":" . '"' .str_replace("\n", "</br>", json_decode($value['message'])). '"' ;
      echo "}";
      if($i != $rows){
        echo ",";
      }
      if($i == $rows){
        echo "]";
      }
      echo "\n";
    }
    exit;
  }
  else {
    echo 'Something went wrong';
    exit;
  }

?>

在我的 angularjs 得到的字符串中发送解码的 json 是我自己制作的 json 的真实解决方案。

我已经添加了这个问题的答案,因为也许有人会遇到同样的麻烦,这会有所帮助。