php Web 服务编码不适用于口音

php web service encoding doesn't work with accent

我在 php 中使用 slim 框架创建了一个小网络服务,它可以工作,除非返回的 json 对象的 json 字段之一包含像 á,é 这样的字符,í,ó,ú ,数据库的编码是ut8_spanish_ci 这是网络服务代码

<?php
header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");

require 'Slim/Slim.php';


\Slim\Slim::registerAutoloader();
//creamos el objeto app
$app = new \Slim\Slim();

//importamos los ficheros con las funciones necesarias
require 'db/db_handler.php';
require 'app/app.php';
//ejecutamos app
$app->run();

这是app.php

<?php
    $app->get('/getAll/:table', function ($table) use($app){
            $db = new Db_handler;
            $result = $db->select_all($table);
            while($row = $result->fetch_assoc()){
                $dependency[] = $row ;
            }
            $struct = array("Dependencies"=>$dependency);

            $app->response->headers->set("Content-type","application/json");
            $app->response->status(200);
            $app->response->body(json_encode($struct));
        }
    );

    $app->get(
        '/get/:table/:id', function ($table, $id) use($app){
            $db = new Db_handler;
            $result = $db->select($table, $id);
            $row = $result->fetch_assoc();

            $app->response->headers->set("Content-type","application/json");
            $app->response->status(200);
            $app->response->body(json_encode($row));

        }
    );
?>

和db_handler.php

<?php
    class Db_handler{
        private $driver;
        private $host;
        private $port;
        private $schema;
        private $username;
        private $password;
        private $mysqli;

        function Db_handler( $config_file = 'connection.ini' ){
            if(!$connection_data = parse_ini_file($config_file, true)) throw new exception("No se puedo abrir el fichero de configuracion ".$config_file." .");
            $this->driver = $connection_data["database"]["driver"];
            $this->host = $connection_data["database"]["host"];
            $this->port = $connection_data["database"]["port"];
            $this->schema = $connection_data["database"]["schema"];
            $this->username = $connection_data["database"]["username"];
            $this->password = $connection_data["database"]["password"];

        }

        function connect(){
            $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->schema, $this->port);
            if ($this->mysqli->connect_errno) {
                echo "Fallo al conectar a MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error;
            }
            $this->mysqli->query("set names utf8");
        }

        function close(){
            $this->mysqli->close();
        }

        function select_all($table){
            $this->connect();
            if(!$result = $this->mysqli->query("SELECT * FROM $table"))
                die('Ocurrió un error al conectar  [' . $db->error . ']');
            $this->close();

            return $result;
        }

        function select($table,$id){
            $this->connect();
            if(!$result = $this->mysqli->query("SELECT * FROM $table WHERE nombre = '$id'"))
                die('Ocurrió un error al conectar  [' . $db->error . ']');
            $this->close();

            return $result;
        }   
    }
?>

我不知道 DbHandler 是什么,但您应该为您的 Db 连接设置编码,因为据我所知,这可能会导致问题。

您对浏览器进行了正确编码,但对数据库进行了错误编码。

你可以用纯 SQL 和 SET Names UTF-8

SET NAMES indicates what character set the client will use to send SQL statements to the server.

您还应该考虑比 "require" 更好的自动加载,因为它看起来真的很糟糕。 Slim 可以使用 composer 自动加载 类 和 PSR-4