0

0

分享微信连续发送多条消息的实例代码

零下一度

零下一度

发布时间:2017-06-29 11:18:47

|

14820人浏览过

|

来源于php中文网

原创

  1 appid = $appid;  
  9         $this->appSecret = $appSecret;  
 10     }  
 11        12     function returnKefu($openid,$content){  
 13         $access_token = $this->getAccessToken();  
 14         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token";  
 15         $responseText = array("touser"=>$openid,"msgtype"=>"text","text"=>array("content"=>$content));  
 16         $responjson = json_encode($responseText);  
 17         $res = $this->httpPost($url,$responjson);  
 18         echo $res;  
 19     }  
 20     function returnPic($openid,$content){  
 21         $access_token = $this->getAccessToken();  
 22         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token";  
 23         $responseText = array("touser"=>$openid,"msgtype"=>"image","image"=>array("media_id"=>"Xfh-2sT-4HHz0z1GrAgb1tlJowuAJuyI-O6AzFX_TT3UsZHwqdsjyUCY6GJGviMi"));  
 24         $responjson = json_encode($responseText);  
 25         $res = $this->httpPost($url,$responjson);  
 26         echo $res;  
 27     }  
 28        29    30        31     function getAccessToken(){  
 32         $file_ticket = "access_token.json";  
 33         if(!file_exists($file_ticket)){  
 34             $file = fopen($file_ticket,'w');  
 35         }  
 36         $data = json_decode(file_get_contents($file_ticket));  
 37         if($data){  
 38             //如果文件存在且内容过期   39             if($data->expires_inhttpGet($url));  
 42                 $access_token = $res->access_token;  
 43                 if($access_token){  
 44                     $data->expires_in = time() + 7000;  
 45                     $data->access_token = $access_token;  
 46                     $fp = fopen($file_ticket, "w");  
 47                     fwrite($fp, json_encode($data));  
 48                     fclose($fp);  
 49                 }  
 50             }else{  
 51                 //没有过期   52                 return $data->access_token;  
 53                    54             }  
 55         }else{  
 56             //文件存在,但是里面没有内容   57             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appSecret";  
 58             $res = json_decode($this->httpGet($url));  
 59                 $access_token = $res->access_token;  
 60                 if($access_token){  
 61                     @$data->expires_in = time() + 7000;  
 62                     $data->access_token = $access_token;  
 63                     $fp = fopen($file_ticket, "w");  
 64                     fwrite($fp, json_encode($data));  
 65                     fclose($fp);  
 66                 }  
 67         }  
 68         return $access_token;  
 69         //return $data;   70     }  
 71     function httpGet($url){  
 72         $ch = curl_init();  
 73         curl_setopt($ch, CURLOPT_URL,$url);  
 74         // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");   75         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
 76         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
 77         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');  
 78         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
 79         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
 80         // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);   81         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
 82    83         $temp = curl_exec($ch);  
 84         curl_close($ch);  
 85         return $temp;  
 86     }  
 87        88     function httpPost($url,$data){  
 89         $ch = curl_init();  
 90         curl_setopt( $ch, CURLOPT_URL, $url);    
 91         curl_setopt( $ch, CURLOPT_POST, 1 );    
 92         curl_setopt( $ch, CURLOPT_HEADER, 0 );    
 93         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );    
 94         //服务器没有使用https的话就不用下面两行     95         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
 96         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    
 97         curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );    
 98              99         $output = curl_exec($ch);    
100         curl_close($ch);    
101         echo $output;    
102     }  
103 }  
104 $k = new Kefu("appid", "appsecret");  
105 echo $k->returnPic("openid","how are you?");  
106 echo $k->returnKefu("openid","how are you?");  
107 ?>

 

Quinvio AI
Quinvio AI

AI辅助下快速创建视频,虚拟代言人

下载

相关文章

微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

0

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

56

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

51

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

397

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

118

2026.01.21

java版本选择建议
java版本选择建议

本专题整合了java版本相关合集,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

11

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号