[已解决]微信公众平台开发 客服系统回复文本显示json_encode|unicode中文乱码
2018-03-02 09:51
281 查看
新手入门总会遇到头疼的问题,客服系统回复文本显示中文乱码,如下:
导致原因:在 curl post 方式调用客服接口时,传参过程中使用了 json_encode 函数对变量进行 JSON 编码,我们习惯性的不填写第二个参数,或者使用框架里封装好的 json 函数,导致中文乱码的问题;
解决方法:在 json_encode 函数加上第二个参数,完美解决微信客服系统的中文乱码问题;
代码: function sendService($object, $access_token)
{
/* 获得openId值 */
$openid = (string)$object->FromUserName;
$post_data = array(
'touser' => $openid,
'msgtype' => 'text',
'text' => array(
'content' => '这是客服自动回复的消息'
)
);
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
curlPost($url, json_encode($post_data, JSON_UNESCAPED_UNICODE));
}
function curlPost($url, $post_data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
curl_close($ch);
//-------请求为空
if(empty($response)){
exit("50001");
}
return $response;
}
导致原因:在 curl post 方式调用客服接口时,传参过程中使用了 json_encode 函数对变量进行 JSON 编码,我们习惯性的不填写第二个参数,或者使用框架里封装好的 json 函数,导致中文乱码的问题;
解决方法:在 json_encode 函数加上第二个参数,完美解决微信客服系统的中文乱码问题;
代码: function sendService($object, $access_token)
{
/* 获得openId值 */
$openid = (string)$object->FromUserName;
$post_data = array(
'touser' => $openid,
'msgtype' => 'text',
'text' => array(
'content' => '这是客服自动回复的消息'
)
);
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
curlPost($url, json_encode($post_data, JSON_UNESCAPED_UNICODE));
}
function curlPost($url, $post_data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
curl_close($ch);
//-------请求为空
if(empty($response)){
exit("50001");
}
return $response;
}
相关文章推荐
- PHP,postgresQL,json_encode,sphinx 中文显示乱码解决方法总结
- PHP数组传递给JavaScript以及json_encode的gbk中文乱码的解决
- PHP json_encode中文乱码解决方法
- VC6.0在文本编辑区中文显示乱码解决办法
- php json_encode()中文乱码解决
- linux 系统中文显示乱码的解决
- PHP json_encode中文乱码解决方法
- php 解决json_encode中文UNICODE转码问题
- 解决 json_encode 中文乱码
- 解决putty终端中显示linux系统中C编程使用curses中文乱码的问题
- 邮件系统开发中,解决提取邮件时出现中文乱码的方法
- Win 7英文系统显示中文乱码的解决(转)
- ubuntu显示中文文本乱码解决方法
- 解决sencha touch显示.JSON包含中文数据时显示乱码问题
- putty在登陆Linux系统后运行命令中文显示乱码的解决办法
- 使用SecureCRT登陆RHEL系统显示中文乱码的解决方法
- 解决 json_encode 中文乱码
- 解决Ubuntu9.04文本&网页flash中文显示乱码
- 解决putty终端中显示linux系统中C编程使用curses中文乱码的问题
- PHP json_encode中文乱码解决方法