【问题标题】:php web service encoding doesn't work with accentphp web 服务编码不适用于口音
【发布时间】:2015-06-17 05:16:35
【问题描述】:

我在 php 中创建了一个带有苗条框架的小型 Web 服务,除非返回的 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;
        }   
    }
?>

【问题讨论】:

    标签: php json slim


    【解决方案1】:

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

    您对浏览器有正确的编码,但对 DB 有错误。

    您可以通过SET Names UTF-8 使用纯 SQL 来完成此操作

    SET NAMES 表示客户端将使用什么字符集向服务器发送 SQL 语句。

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

    【讨论】:

    • 我已经在 db_handler 连接函数上添加了$this-&gt;mysqli-&gt;query("SET Names 'UTF-8'");,但是如果我有并且重音 Web 服务不返回任何内容,它也是一样的
    • 已解决在 db_handler.php 上添加 $this-&gt;mysqli-&gt;query("set names utf8");
    • 如果这个答案让你满意,那么就接受它,这样问题就可以结束了。
    猜你喜欢
    • 2012-07-07
    • 2013-09-30
    • 2014-03-07
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多