您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

调用微信企业号接口发送通报信息的PHP代码

2024/2/19 15:14:05发布35次查看
调用微信企业号接口发送通知信息的php代码
我在研究用php调用微信企业号接口发送通知信息时,遇到了一个问题,就是汉字编码的问题。在用post提交的数据如果是用数组型时,要先用json_encode将数组型数据转成josn字串,但数据中如果有汉字就会出现问题:json_encode不能序列化gb2312编码的汉字,若是utf-8编码的汉字在用json_encode转换后也成了无法识别的乱码,微信企业号的接口也无法接收这些乱码。该怎么办呢?经过反复研究、反复调试终于找出了两种方法:
第一种方法(数组型数据):
1、将页面代码转存成utf-8编码;
2、用urlencode将汉字编码;
?
3、用json_encode将数组型数据转成josn字串
4、用urldecode将josn字串型数据解码;
5、再将解码后的josn字串型数据发送给微信企业号接口即可。
?
第二种方法(字串型数据):
1、将页面代码转存成utf-8编码;
2、将要传递的post数据用字串拼接的型连接起来;
3、再将拼接好的字串型数据发送给微信企业号接口即可。
?
为了代码简单,我用了第二种方法,代码如下:
?
$url, curlopt_header => 0, curlopt_post => 1, curlopt_postfields => $data, curlopt_returntransfer => 1, curlopt_timeout => 20 ); $ssl = substr($url,0,8) == https:// ? true : false; if ($ssl){ $opt[curlopt_ssl_verifyhost] = 1; $opt[curlopt_ssl_verifypeer] = false; } curl_setopt_array($ch,$opt); $data = curl_exec($ch); curl_close($ch); return $data;}$corpid=请修改为你企业号的corpid;$corpsecret=请修改为你企业号的corpsecret;$url=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret;$res = curlpost($url);$access_token=json_decode($res)->access_token;$url=https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token;$msg='要发送的文字信息';$data={\touser\:\要发给的用户id\,\msgtype\:\text\,\agentid\:你的应用id,\text\:{\content\:\$msg\},\safe\:0};$res = curlpost($url,$data);$errmsg=json_decode($res)->errmsg;if($errmsg===ok){ echo 发送成功!;}else{ echo 发送失败,.$errmsg;}?>
?
?
更多介绍:http://www.cnblogs.com/jisheng/archive/2012/02/13/2350040.html
?
?
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product