V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
GPU
V2EX  ›  PHP

php json encode 中文部分被替换为 unicode 码 ,应该怎么用最简单的方法解决?

  •  
  •   GPU · 2014-09-22 19:49:37 +08:00 · 3234 次点击
    这是一个创建于 3873 天前的主题,其中的信息可能已经有所发展或是发生改变。
    if(!$_GET['username']||!$_GET['password']||!$_GET['domain']){
    exit(urldecode(json_encode(array('msg'=>'username,password,domain 参数必选'))));
    }
    view raw messy code hosted with ❤ by GitHub


    代码如上 ,

    此代码json输出网页的东西是这样子的
    " {"msg":"username,password,domain \u53c2\u6570\u5fc5\u9009"}"


    完整版如下,


    <?php
    error_reporting(0);
    set_time_limit(0);
    header('Content-type:text/html; charset=utf-8');
    if(!$_GET['username']||!$_GET['password']||!$_GET['domain']){
    exit(json_encode(array('msg'=>'username,password,domain 参数必选')));
    }
    $domain = explode("@", $_GET['domain']);
    $config = array(
    'login_email' => $_GET['username'],
    'login_password' => $_GET['password'],
    'sub_domain' => $domain[0],
    'domain' => $domain[1],
    'record_line' => $_GET['line']?$_GET['line']:'默认',
    'ttl' => 600,
    'format' => 'json',
    'lang' => 'cn',
    'error_on_empty' => 'no',
    );
    $dnspod = new dnspod($config);
    $config['ip'] = $_GET['myip']?$_GET['myip']:$_SERVER['REMOTE_ADDR'];
    $dnspod->updateRecordIp($config['ip']);
    class dnspod {
    var $config;
    var $domain;
    var $sub_domain;
    var $record_line;
    function __construct($config)
    {
    $this->config = $config;
    $this->domain = $config['domain'];
    $this->sub_domain = $config['sub_domain'];
    $this->record_line = $config['record_line'];
    }
    public function api_call($api, $data) {
    if ($api == '' || !is_array($data)) {
    exit(json_encode(array('msg'=>'内部错误:参数错误')));
    }
    $api = 'https://dnsapi.cn/' . $api;
    $data = array_merge($data,$this->config);
    $result = $this->post_data($api, $data);
    if (!$result) {
    exit(json_encode(array('msg'=>'内部错误:调用失败')));
    }
    $results = @json_decode($result, 1);
    if (!is_array($results)) {
    exit(json_encode(array('msg'=>'内部错误:返回错误')));
    }
    if ($results['status']['code'] != 1) {
    exit(json_encode(array('msg'=>$results['status']['message'])));
    }
    return $results;
    }
    public function updateRecordDdns(){
    $record = $this->getRecordInfo();
    //判断IP是否改变
    if($record['records'][0]['value'] == $_SERVER['REMOTE_ADDR']){
    exit(json_encode(array('msg'=>'记录不需要更新')));
    }
    $response = $this->api_call('Record.Ddns', array('record_id'=>$record['records'][0]['id'],'record_line'=>$this->record_line));
    if($response){
    exit(json_encode($response));
    }else{
    exit(json_encode(array('msg'=>'更新失败')));
    }
    }
    //获取域名信息
    public function getDomainInfo(){
    $response = $this->api_call('Domain.Info', array('domain' =>$this->domain));
    return $response;
    }
    //获取记录
    function getRecordInfo(){
    $response = $this->api_call('Record.List', array('sub_domain'=>$this->sub_domain,'domain' =>$this->domain));
    return $response;
    }
    //修改A记录IP
    function updateRecordIp($ip){
    $record = $this->getRecordInfo();
    //判断IP是否改变
    if($record['records'][0]['value']==$ip){
    exit(json_encode(array('msg'=>'记录不需要更新')));
    }
    $response = $this->api_call('Record.Modify', array('record_id'=>$record['records'][0]['id'],'record_line'=>$this->record_line,'record_type'=>'A','value'=>$ip));
    return $response;
    }
    private function post_data($url, $data) {
    if ($url == '' || !is_array($data)) {
    return false;
    }
    $ch = @curl_init();
    if (!$ch) {
    exit(json_encode(array('msg'=>'内部错误:服务器不支持CURL')));
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'DNSPod MYDDNS/0.1 (i@biner.me)');
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }
    }
    view raw dnspod.php hosted with ❤ by GitHub
    14 条回复    2014-09-24 09:01:03 +08:00
    anewg
        1
    anewg  
       2014-09-22 19:52:28 +08:00
    5.4 之后 echo json_encode("中文", JSON_UNESCAPED_UNICODE);

    5.4 之前 echo json_encode(urlencode("中文"));接收端 json_decode 后再 urldecode
    GPU
        2
    GPU  
    OP
       2014-09-22 20:02:28 +08:00
    @anewg 5.4 是什么意思?
    anewg
        3
    anewg  
       2014-09-22 20:10:07 +08:00
    @GPU php版本5.4
    GPU
        4
    GPU  
    OP
       2014-09-22 20:25:30 +08:00
    @anewg
    https://gist.github.com/anonymous/6d8f133df10314942724

    改了一下, 成这样子 ,输出正常 .不知道有没有可以写得更简化的呢?
    iyaozhen
        5
    iyaozhen  
       2014-09-22 20:38:10 +08:00
    其实没关系的,接收的时候php_decode会把编码还原。
    js我用jquery接收,不用任何处理data.msg直接获取就行。
    一楼说的php5.4之后的JSON_UNESCAPED_UNICODE参数可以直接输出中文。
    zakokun
        6
    zakokun  
       2014-09-22 21:44:08 +08:00 via iPad
    先把中文urlencode一下,处理完以后再转回来urldecode
    anewg
        7
    anewg  
       2014-09-22 22:26:06 +08:00   ❤️ 1
    @GPU 你用5.4以上的php的话这样是最简了
    GPU
        8
    GPU  
    OP
       2014-09-22 22:51:47 +08:00
    @anewg 嗯. 好的.
    bombless
        9
    bombless  
       2014-09-23 07:27:35 +08:00
    我还以为是指读不出来unicode编码呢…
    这种编码算是一种惯例了,不明白为啥要改
    tmkook
        10
    tmkook  
       2014-09-23 09:08:50 +08:00
    function jsonEncode($arr){
    $json = json_encode($arr);
    return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $json);
    }

    放出我的必杀技!!!
    GPU
        11
    GPU  
    OP
       2014-09-23 11:18:34 +08:00 via iPhone
    @tmkook 看不懂啊
    tmkook
        12
    tmkook  
       2014-09-23 19:52:56 +08:00
    @GPU echo jsonEncode(array("test":"中文")); 生成:{"test":"中文"}
    GPU
        13
    GPU  
    OP
       2014-09-23 23:22:45 +08:00
    @tmkook 这个技能貌似真的很屌
    Actrace
        14
    Actrace  
       2014-09-24 09:01:03 +08:00
    一个比较容易懂的做法是先把数组内的元素都用urlencode处理成不需要转换的字符串,然后生成JSON字符串后再用urldecode来对整个JSON字符串进行解码(中文等将被还原),类似于金属提纯.
    不过看了10楼的做法,感觉10楼效率上会有很大的优势,毕竟是正则替换,而且相对于上面提出的方案少了一个转换的流程.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2304 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 02:47 · PVG 10:47 · LAX 19:47 · JFK 22:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.