微信公众号开发中,中文的编码问题
微信公众号开发过程中,接口都是比较简单的,就是按微信要求的格式post一段json数据过去。
如果json有中文,json_encode后出现了类似”\u5c0f\u8c61″ 的unicode字符。
{"errcode":40033,"errmsg":"invalid charset. please check your request, if include \\uxxxx will create fail!"}
方法1
如果你的php版本是5.4+, 一个参数JSON_UNESCAPED_UNICODE就能搞定。
<?php
$data = array(
"name"=>"羊羊羊",
"type"=>"view",
"url"=>"http://xuan9806.com/"
);
echo json_encode($data, JSON_UNESCAPED_UNICODE), "\n";
方法2
如果由于种种原因你的php无法升到高版本,那么可以这么做:
把字段中的中文urlencode, 在json_encode后将得到的字串整体urldecode即可
<?php
$data = array(
"name"=>urlencode("羊羊羊"),
"type"=>"view",
"url"=>"http://xuan9806.com/"
);
$result = json_encode($data, JSON_UNESCAPED_UNICODE);
$result = urldecode($result);
