搜尋
首頁後端開發php教程PHP微信開發之自動回复

PHP微信開發之自動回复

Apr 19, 2018 am 09:48 AM
php回覆自動

這篇文章介紹的內容是關於PHP微信開發之自動回复,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

1.關鍵字回复文字內容

首先我們需要在LaneWeChat/core/aes/wechatrequest.lib.php下面的text()方法中需要進行一些修改,程式碼如下:

public static function text(&$request){
        // $content = '收到文本消息';
        // return ResponsePassive::text($request['fromusername'], $request['tousername'], $content);
        $mpid = $_GET['id'];
        $content = $request['content'];
        $where['mp_id'] = $mpid;
        $where['keyword'] = $content;
        $data = M('mp_reply_rule')->where($where)->find();
        if ($data) {
            $reply_id = $data['reply_id'];
            $type = $data['type'];
            if ($type == "text") {
                $reply = M('mp_reply_text')->find($reply_id);
                $reply_text = $reply['content'];
                return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
            }else if($type == "image"){
                $reply = M('mp_reply_image')->find($reply_id);
                $media_id=$reply['media_id'];
                return ResponsePassive::image($request['fromusername'],$request['tousername'],$media_id);
            }else if($type == "news"){
                $reply = M('mp_reply_news')->find($reply_id);
                $item[] = ResponsePassive::newsItem($reply['title'],$reply['descrpition'],$reply['picurl'],$reply['url']);
                return ResponsePassive::news($request['fromusername'],$request['tousername'],$item);
            }
        }else{
            return 'success';
        }
    }

#其次我們開始寫後台PHP代碼

public function replytext(){	
  	if(IS_GET){
  		$this->display();
  	}else{	
  		$content=I('post.content');
  		$keyword=I('post.keyword');
  		$data['content']=$content;
  		$reply_id=M('mp_reply_text')->add($data);
      if(isset($reply_id)){
        $mp=getCurrentMp();
        $data['mp_id']=$mp['id'];   
        $data['keyword']=$keyword; 
        $data['type']='text';                   
        $data['reply_id']=$reply_id;
                     // print_r($data);
                     // exit;
        M('mp_reply_rule')->add($data);
        $this->ajaxReturn(array('msg'=>'上传成功'));                        
      }else{
        $this->ajaxReturn(array('msg'=>'上传失败'));
      }
    }
  }

根據前台頁面輸入關鍵字以及回复內容就可以實現自動回复文本內容

2.根據關鍵字自動回复圖片

根據LaneWeChat/core/aes/wechatrequest.lib.php中text()中的方法可以自動判斷要回复的是那種類型 只需在php後台中獲取其type,自動回復圖片我們在PHP中寫入以下程式碼:

public function replyimage(){
  if(IS_GET){
      $this->display(); 
   }else{
    $url=I('post.url');//图片在本地服务器上的路径  
    $file=realpath('.' .$url);// 相对路径换位结对路径      
    $accessToken=getAccess_token();
    include APP_PATH .'LaneWeChat/lanewechat.php';
    $url="https://api.weixin.qq.com/cgi-bin/material/add_material?accessaccessToken&type=image";
    $data['media']='@' .$file;
    $ret=Curl::callWebServer($url,$data,'post',true,false);
    if(isset($ret['media_id'])){
         $mp=getCurrentMp();
         $data['url']=$url;
         $data['media_id']=$ret['media_id'];       
         $reply_id=M('mp_reply_image')->add($data);
         $keyword=I('post.keyword');
         if(isset($reply_id)){
           $mp=getCurrentMp();
           $data['mp_id']=$mp['id'];   
           $data['keyword']=$keyword; 
           $data['type']='image';                   
           $data['reply_id']=$reply_id;
           M('mp_reply_rule')->add($data);  
         $this->ajaxReturn(array('msg'=>'上传成功'));
      }else{
        $this->ajaxReturn(array('msg'=>'上传失败'));
      }
    }else{
        $this->ajaxReturn(array('msg'=>'上传失败')); 
      }
  }         
}

但要注意我們在回覆圖片時應提前在資料表中存入一張要回覆的圖片,所以我們還需在PHP中寫入upload()方法。

3.根據關鍵字回覆圖文訊息

相當於將回覆文字和圖片的道理相同,只需稍作修改即可,取得有效的字段。

相關推薦:

PHP微信開發之文字自動回覆

PHP微信開發之翻譯功能

PHP微信開發之獲取微信精選文章


以上是PHP微信開發之自動回复的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

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

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

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

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

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

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

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

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

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

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!