微信公众号主动给用户发送信息的方法:首先在公众号后台功能模块中添加“客服功能”模块;然后获取“access_token”并调用发送方法;最后在使用时直接调用“sendmsg()”方法传值即可。
微信公众号怎么主动给用户发送信息?
首先,先在公众号后台功能模块中-》添加功能模块 添加 客服功能 模块
如图所示:
然后就进入代码环节了。
先添加客服。也可以公众号后台添加
//先添加客服 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()方法传值就可以了。
最后上完成的图
如图所示,公众号可以给用户发送消息,而且发送的内容你可以使用html 标签哦
虽然这样感觉很方便,但是同样,微信公众平台对此也有限制。
1.用户必须关注公众号,方可收到信息。
2.用户只能连续收到 20 条客服消息。超过后用户就收不到消息,
超过后,用户必须 主动给公众号发送消息 。或者 点击菜单栏,这样20条消息的限制就会重置。
更多相关知识,请访问PHP中文网!

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

WebStorm Mac版
好用的JavaScript开发工具