早睡早起,方能养生
Sleep early rise early, way to keep healthy

微信发送客服消息,中文被转义/unicode/乱码

super
2020-05-29 09:10
views 5598

 微信API:

 

https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

 

微信客服消息中文被转化为unicode字符

image.png

 

解决方法:使用json_encode的第二个参数,使中文不被转义

 

$ACCESS_TOKEN = 'xxx';
$url    = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$ACCESS_TOKEN}";
$data   = [
    'touser'	=> 'user's openid',
    'msgtype'	=> 'link',
    'link'		=> [
        'title'	=> '测试资料',
        'description' => '描述',
        'url' => 'https://rongsp.com',
        'thumb_url' => 'https://xxx.com/logo.png'
    ]
];

$send_result = curl_post($url, json_encode($data, JSON_UNESCAPED_UNICODE));
// (PHP > 5.4) json_encode 加上第二个参数JSON_UNESCAPED_UNICODE:不要编码Unicode.

 

关于json_encode的详细内容:json_encode中文不转码

 

function curl_post($url, $data)
{
    $headers = ["Accept:application/json"];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $response = curl_exec($ch) ? curl_multi_getcontent($ch) : '';
    curl_close($ch);
    $output = json_decode($response, true);
    return $output;
}

 

image.png


分享
0 条讨论
top