您的位置:首页 > 移动开发 > 微信开发

[已解决]微信公众平台开发 客服系统回复文本显示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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: