首頁 >後端開發 >php教程 >PHP微信開發教程之搜尋已發送圖文及符合關鍵字回覆的方法

PHP微信開發教程之搜尋已發送圖文及符合關鍵字回覆的方法

巴扎黑
巴扎黑原創
2017-09-15 09:32:411799瀏覽

這篇文章主要介紹了PHP使用微信開發模式實現搜尋已發送圖文及匹配關鍵字回复的方法,涉及php針對微信json格式數據的解析與正則匹配相關操作技巧,需要的朋友可以參考下

本文實例講述了PHP使用微信開發模式實現搜尋已發送圖文及匹配關鍵字回應的方法。分享給大家供大家參考,具體如下:

用戶發送關鍵字,搜尋對應的圖文回覆。

模擬登陸就不說了

右鍵看「已傳送」圖片頁的原始碼,圖文部分是json格式的。

複製程式碼 程式碼如下:

{"msg_item":[{"id":80630,"type":9,"fakeid":"2397007301","nick_name":"每天學點心理學","date_time":14034445168,"content ":"","source":"mass","msg_status":2,"title":"(第167期)| 我讀過的具有正能量的一篇文章,為你取暖","desc" :"傳說世間的一切生靈皆可修煉成仙,而貓自然在其中。每修練二十年,貓就會多長出一條尾巴,等到有...","content_url":"http:\/ \/mp.weixin.qq.com\/s?__biz=MjM5NzAwNzMwMQ==&mid=201393381&idx=1&sn=5351cb8d440365c24a6a_bb709e4e0d8c#rd"," 3,"comment_url" :"","has_reply":0,"refuse_reason":"","multi_item":[{"seq":0,"cover":"https:\/\/mmbiz.qlogo.cn\/mmbiz\/ jOtiajqZ1n62uicM0XBZkqUB5BdJRdCMt0Y3CDIApNglh6Lj5zcmfm0mqN0YyGkNvekxP7j0uqs95AQ|wZpTfCg\/0","title":"(第167723篇文章"靈皆可修煉成仙,而貓咪自然在其中。 __biz=MjM5NzAwNzMwMQ==&mid=201393381&idx=1&sn=5351cb8d440365c24a6abb709e4e0d8c#rd","file_id":201393." ":0," vote_id":[]}],"to_uin":0,"send_stat":{"total":1656,"succ":1656,"fail":0}},{"id":80444,"type": 2,"fakeid":"2397007301","nick_name":"每天學點心理學","date_time":1403358199,"source":"mass","msg_status":2,"comment_url":""," has_reply":0,"refuse_reason":"","multi_item":[],"to_uin":0,"send_stat":{"total":1631,"succ":1631,"fail":0}}, {"id":80222,"type":9,"fakeid":"2397007301","nick_name":"每天學點心理學","date_time":1403272191,"content":"","source": "mass","msg_status":2,"title":"(第166期)| 想要的超能力,來玩一個投票娛樂下吧~","desc":"雖然沒啥了不起的,但還是想要的這種超能力…-----------END-----------可以點選右上角『","content_url":"http:\/\/mp .weixin.qq.com\/s?__biz=MjM5NzAwNzMwMQ==&mid=201376620&idx=1&sn=a9ccae0826f5aa74476dc5936cec51f2#rd","show_type"aa74476dc5936cec51f2#rd","show_type"aa74476dc5936cec51f2#rd","show_type" url":"" ,"has_reply":0,"refuse_reason":"","multi_item":[{"seq":0,"cover":"https:\/\/mmbiz.qlogo.cn\/mmbiz\/jOtiajqZ1n62MibMmurxXEVsDbwZgl* I9iaCXicCTJC5Mef8ggBA\/ 0","title":"(第166期)| 想要的超能力,來玩一個投票娛樂下吧~","digest":"雖然沒啥了不起的,但還是想要的這種超能力…-----------END-----------可以點選右上角『","content_url":"http:\/\/mp.weixin.qq.com\ /s?__biz=MjM5NzAwNzMwMQ==&mid=201376620&idx=1&sn=a9ccae0826f5aa74476dc5936cec51f2#rd","file_id":20137086,"",",",",",", 您, 您,0137086,"p; 0,"vote_id":[45050]},{"seq":1,"cover":"https:\/\/mmbiz.qlogo.cn\/mmbiz\/jOtiajqZ1n62MibMmurxXEVsDbwZglIq1w4hNsMagnia4p614h2120P27pE \/0","title":" 【美文】| 揮之不去的無奈","digest":"躺在床上,過往的一幕幕在腦海中掠過。四年的堅持,抑或說是7年的夢想,當它破滅時,僅僅是淡然一笑,談何容易?其實實作","content_url":"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzAwNzMwMQ==&mid=201376620&idx=2&sn=21a7f8715f4b2erd2484ffiled" "content ":"","source_url":"","author":"","show_cover_pic":1,"vote_id":[]},{"seq":2,"cover":"https:\/\ /mmbiz.qlogo.cn\/mmbiz\/jOtiajqZ1n62MibMmurxXEVsDbwZglIq1wNzqasiaEPYAGXQQdLnkaiaW56ueicHelZFJCjWUA3dg6ibWw15N1KQt,eQtle "願每一個相信愛的女生,都能成為愛裡打不倒的戰士。沒有人比你更美好,因為這個世界上不會再有第二個你了。少一點依","content_url":"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzAwNzMwMQ==&mid=201376620&idx=3&sn=ca0a2122e1c5d8f848fd646423f content":"","source_url":"","author":"","show_cover_pic":0,"vote_id":[]}],"to_uin":0,"send_stat":{"total": 1610,"succ":1610,"fail":0}},{"id":79872,"type":1,"fakeid":"2397007301","nick_name":"每天學點心理學"," date_time":1403185708,"content":"朋友們,今天休息,不發咯,你們也早點休息喲〜\/:8d1ad4fab35c804ba411dd69aef9286b","source":"mass","msg_status":2," comment_url":"","has_reply":0,"refuse_reason":"","multi_item":[],"to_uin":0,"send_stat":{"total":1584,"succ":1584," fail":0}},{"id":79544,"type":9,"fakeid":"2397007301","nick_name":"每天學點心理學","date_time":1403098674,"content": "","source":"mass","msg_status":2,"title":"(第165期)| 孩子為什麼會偷東西","desc":"小孩偷東西是一種很嚴重的行為,但好像是很多孩子在成長過程中都有偷東西一個階段,父母該如何教育呢?孩子為什麼會","content_url":"http:\/\/mp.weixin.qq.com\/s? __biz=MjM5NzAwNzMwMQ==&mid=201345041&idx=1&sn=b2ef53dcfd93db2737240f932a0297d5#rd","show_type":1,"file_id":2012401." reply":0,"refuse_reason" :"","multi_item":[{"seq":0,"cover":"https:\/\/mmbiz.qlogo.cn\/mmbiz\/jOtiajqZ1n60ZFQ7comgKCEXlkibVZXmJf1PqAs4cza \/0","title":"(第165期)| 孩子為什麼會偷東西","digest":"小孩偷東西是一種很嚴重的行為,但好像是很多孩子在成長過程中都有偷東西一個階段,父母該如何教育呢?孩子為何會","content_url":"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzAwNzMwMQ==&mid=201345041&idx=1&sn=b2ef53dcfd93db27372409320"dfile" ":"","source_url":"","author":"蟲","show_cover_pic":1,"vote_id":[]},{"seq":1,"cover":"https:\/ \/mmbiz.qlogo.cn\/mmbiz\/jOtiajqZ1n60ZFQ7comgKCEXlkibVZXmJfRPJl3P72Vh8l46B1m6EvZY4IWRnFB0AHkxrTSbghME9TxmFigdXptitlei" 。 -----------END-----------可以點選右上角『分享至朋友圈』。微訊號:m","content_url":"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzAwNzMwMQ==&mid=201345041&idx=2&sn=63ae37b620566f28004 "content":"","source_url":"","author":"蟲","show_cover_pic":0,"vote_id":[]}],"to_uin":0,"send_stat":{"total ":1568,"succ":1568,"fail":0}}]}


#直接解析json即可。


<?php
$wechatObj = new wechatappmsg();
$wechatObj->getappmsg(&#39;这里是json&#39;);
class wechatappmsg{
  function getappmsg($content){
    //require_once &#39;db.php&#39;;
    $jsonstr=$content;
    //   $jsonstr=&#39;{"msg_item":[{"id":79544,"type":9},{}]}&#39;;
    $jsonstr = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $jsonstr);
    $result = json_decode($jsonstr);
    $name=$result->msg_item;
    // var_dump($name);
    //   echo &#39;<br/>json:&#39;.$result;
    $size=count($name);
    echo &#39;<br/>size:&#39;.$size;
    //  exit;
    for($i=0;$i<$size;$i++){
      $multi_item=$name[$i]->multi_item;
      $size2=count($multi_item);
      $app_id=$name[$i]->app_id;
      $create_time=$name[$i]->date_time;//发送时间
      //  echo &#39;<br/>app_id:&#39;.$app_id;
      $create_time=date ("Y-m-d H:i:s", $create_time);//时间转换
      echo &#39;<br/>create_time:&#39;.$create_time;
      for($j=0;$j<$size2;$j++){
        $title=$multi_item[$j]->title;//标题
        $pic_url=$multi_item[$j]->cover;//图片地址
        $digest=$multi_item[$j]->digest;//简要
        $content_url=$multi_item[$j]->content_url;//图文地址
        $t = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $title);
        echo &#39;<br/>title:&#39;.$t;
        echo &#39;<br/>Link:&#39;.$content_url;
        preg_match("/mid=([0-9]+)&/",$content_url,$string);
        $app_mid=$string[1];//一次发送的多个图文,这个ID值可以判断这两个图文是同一次发送的
        echo &#39;<br/>app_mid:&#39;.$app_mid;
        //这里可以直接进行入库
//$x=saveappmsg($pic_url,$title,$digest,$content_url,$create_time,$app_mid);
 sleep(3);
      }
      echo &#39;<br/>-------------------------------------&#39;;
    }
  }
}
?>

資料庫:

使用者回覆關鍵字,然後查詢回覆對應圖文資訊。

select app_id from table where title like &#39;%162期%&#39; limit 1;
select * from table where app_id=XXXXX;

2》根據關鍵字:


select * from table where title like &#39;%故事%&#39; limit 10;

以上是PHP微信開發教程之搜尋已發送圖文及符合關鍵字回覆的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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