Home Backend Development PHP Tutorial Share the example code for sending multiple messages continuously on WeChat

Share the example code for sending multiple messages continuously on WeChat

Jun 29, 2017 am 11:18 AM
send information

  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 ?>
Copy after login

 

The above is the detailed content of Share the example code for sending multiple messages continuously on WeChat. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to swipe right and reply quickly in iMessage on iOS 17 How to swipe right and reply quickly in iMessage on iOS 17 Sep 20, 2023 am 10:45 AM

How to Use Swipe to Reply in iMessages on iPhone Note: The Swipe to Reply feature only works with iMessage conversations in iOS 17, not regular SMS conversations in the Messages app. Open the Messages app on your iPhone. Then, head to the iMessage conversation and simply swipe right on the iMessage you want to reply to. Once this is done, the selected iMessage will be in focus while all other messages will be blurred in the background. You'll see a text box for typing a reply and a "+" icon for accessing iMessage apps like Check-ins, Places, Stickers, Photos, and more. Just enter your message,

What does it mean when a message has been sent but rejected by the other party? What does it mean when a message has been sent but rejected by the other party? Mar 07, 2024 pm 03:59 PM

The message has been sent but rejected by the other party. This means that the sent information has been successfully sent from the device, but for some reason, the other party did not receive the message. More specifically, this is usually because the other party has set certain permissions or taken certain actions, which prevents your information from being received normally.

How to send files to others on TikTok? How to delete files sent to others? How to send files to others on TikTok? How to delete files sent to others? Mar 22, 2024 am 08:30 AM

On Douyin, users can not only share their life details and talents, but also interact with other users. In this process, sometimes we need to send files to other users, such as pictures, videos, etc. So, how to send files to others on Douyin? 1. How to send files to others on Douyin? 1. Open Douyin and enter the chat interface where you want to send files. 2. Click the &quot;+&quot; sign in the chat interface and select &quot;File&quot;. 3. In the file options, you can choose to send pictures, videos, audio and other files. After selecting the file you want to send, click &quot;Send&quot;. 4. Wait for the other party to accept your file. Once the other party accepts it, the file will be transferred successfully. 2. How to delete files sent to others on Douyin? 1. Open Douyin and enter the text you sent.

iOS 17: How to use emojis as stickers in Messages iOS 17: How to use emojis as stickers in Messages Sep 18, 2023 pm 05:13 PM

In iOS17, Apple has added several new features to its Messages app to make communicating with other Apple users more creative and fun. One of the features is the ability to use emojis as stickers. Stickers have been around in the Messages app for years, but so far, they haven't changed much. This is because in iOS17, Apple treats all standard emojis as stickers, allowing them to be used in the same way as actual stickers. This essentially means you're no longer limited to inserting them into conversations. Now you can also drag them anywhere on the message bubble. You can even stack them on top of each other to create little emoji scenes. The following steps show you how it works in iOS17

WhatsApp Tips: How to Send HD Photos and Videos WhatsApp Tips: How to Send HD Photos and Videos Sep 10, 2023 am 10:13 AM

WhatsApp has launched a new option that allows users to send photos and videos in high resolution through the messaging platform. Read on to find out how it's done. WhatsApp has released an update that allows iPhone and Android users to send photos and videos in high resolution, finally addressing the service's low-quality media sharing limitations. The option is called "HD Quality" and means users can send clearer photos and videos with minimal compression. For example, images captured on the iPhone can now be sent at 3024x4032 resolution instead of the previous maximum of 920x1280, while videos can be sent at 1280×718 resolution instead of 848×476.

The message has been sent but was rejected by the other party. Should I block it or delete it? The message has been sent but was rejected by the other party. Should I block it or delete it? Mar 12, 2024 pm 02:41 PM

1. Being added to the blacklist: The message has been sent but rejected by the other party. Generally, you have been blacklisted. At this time, you will not be able to send messages to the other party, and the other party will not be able to receive your messages. 2. Network problems: If the recipient's network condition is poor or there is a network failure, the message may not be successfully received. At this point, you can try to wait for the network to return to normal before sending the message again. 3. The other party has set up Do Not Disturb: If the recipient has set up Do Not Disturb in WeChat, the sender’s messages will not be reminded or displayed within a certain period of time.

How to set up Xiaomi Mi 14 Pro to light up the screen for messages? How to set up Xiaomi Mi 14 Pro to light up the screen for messages? Mar 18, 2024 pm 12:07 PM

Xiaomi 14Pro is a flagship model with excellent performance and configuration. It has achieved high sales since its official release. Many small functions of Xiaomi 14Pro will be ignored by everyone. For example, it can be set to light up the screen for messages. Although the function is small, , but it is very practical. Everyone will encounter various problems when using the mobile phone. So how to set up the Xiaomi 14Pro to light up the screen for messages? How to set up Xiaomi Mi 14 Pro to light up the screen for messages? Step 1: Open your phone’s Settings app. Step 2: Swipe down until you find the &quot;Lock screen and password&quot; option and click to enter. Step 3: In the &quot;Lock screen &amp; passcode&quot; menu, find and click the &quot;Turn on screen for notifications&quot; option. Step 4: On the &quot;Turn on screen when receiving notifications&quot; page, turn on the switch to enable

How to get HTML form data as text and send to html2pdf? How to get HTML form data as text and send to html2pdf? Sep 06, 2023 pm 12:21 PM

html2pdf is a JavaScript package that allows developers to convert html to canvas, pdf, images, and more. It takes html as parameter and adds it to pdf or desired document. Additionally, it allows users to download the document after adding html content. Here we will access the form and add it to the pdf using the html2pdfnpm package. We will see different examples to add form data to pdf. Syntax User can follow the following syntax to pass html form data as text and send it to html2pdf. varelement=document.getElementById('form');html2

See all articles