WeChat triggers continuous transmission of multiple messages (prototype), multiple prototype

Source: Internet
Author: User
Tags openid

Trigger continuous transmission of multiple messages (prototype), multiple prototype

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 // if the file exists and the content expires 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 // No expired 52 return $ data-> access_token; 53 54} 55} else {56 // The file exists, but there is no content in it 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; 7 0} 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 cur Rochelle 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 cu Rl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); 94 // if the server does not use https, the following two lines are not used: 95 curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); 96 curl_setopt, 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?>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.