微信公众号主动给用户发送信息的方法:首先在公众号后台功能模块中添加“客服功能”模块;然后获取“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中文网!

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

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

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

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

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器