search
HomeBackend DevelopmentPHP Tutorial微信公众号怎么主动给用户发送信息

微信公众号怎么主动给用户发送信息

Jun 06, 2016 pm 08:33 PM
phpMicro-channel public platform

微信公众号主动给用户发送信息的方法:首先在公众号后台功能模块中添加“客服功能”模块;然后获取“access_token”并调用发送方法;最后在使用时直接调用“sendmsg()”方法传值即可。

微信公众号怎么主动给用户发送信息

微信公众号怎么主动给用户发送信息?

首先,先在公众号后台功能模块中-》添加功能模块  添加   客服功能   模块

如图所示:

企业微信截图_15921877152275.png

 

然后就进入代码环节了。

先添加客服。也可以公众号后台添加

//先添加客服
function addkf()
{
    $token = getToken();
    $url = ‘https://api.weixin.qq.com/customservice/kfaccount/add?access_token=‘.$token;
    $data = ‘{
         "kf_account" : "system@system",
         "nickname" : "客服1",
         "password" : "admin",
    }‘;
    echo https_request($url,$data);
}

然后就是主动发送消息的方法

//获取access_token的方法。
function getToken()
{
    $appid = ‘appid‘;
    $appsecret = ‘appsecret‘;
    $token_file = dirname(dirname(__FILE__)).‘/data/token.txt‘;
    if(!file_exists($token_file) || ((time() - filemtime($token_file)) > 7000)){
        $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        $json=file_get_contents($TOKEN_URL);
        $result=json_decode($json);
        $ACC_TOKEN=$result->access_token;
        file_put_contents($token_file,$ACC_TOKEN);
    }else{
        $ACC_TOKEN = file_get_contents($token_file);
    }
    return $ACC_TOKEN;
}
//调用发送方法
function sendmsg($content,$openid)
{
    $token = getToken();
    $url = ‘https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=‘.$token;
    $content = ‘感谢你的关注\n回复你厉害 \n例如<a href=\"http://www.baidu.com\">回复123456</a>‘;
    $data = ‘{
        "touser":"‘.$openid.‘",
        "msgtype":"text",
        "text":
        {
             "content":"‘.$content.‘"
        }
    }‘;
    https_request($url,$data);
    return true;
}
/**
 * request 请求
 */
function https_request($url, $data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

使用的时候直接调用 sendmsg()方法传值就可以了。

最后上完成的图

企业微信截图_15921877245039.png

如图所示,公众号可以给用户发送消息,而且发送的内容你可以使用html 标签哦

虽然这样感觉很方便,但是同样,微信公众平台对此也有限制。

1.用户必须关注公众号,方可收到信息。

2.用户只能连续收到   20   条客服消息。超过后用户就收不到消息,

超过后,用户必须   主动给公众号发送消息  。或者  点击菜单栏,这样20条消息的限制就会重置。

更多相关知识,请访问PHP中文网

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version