1 <?php
2 3 class kefu{
4 private $appid;
5 private $appsecret;
6 7 function __construct($appid,$appsecret){
8 $this->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_in<time()){
40 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret;
41 $res = json_decode($this->httpget($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 ?>
以上就是分享微信连续发送多条消息的实例代码的详细内容。