(转载)微信公众平台 - 实例(未验证)
生活随笔
收集整理的這篇文章主要介紹了
(转载)微信公众平台 - 实例(未验证)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如何部署自己的公眾平臺:
????1.打開http://mp.weixin.qq.com 注冊/登錄
????2.進入高級功能菜單,選擇編輯模式或開發者模式(本例子屬于開發者模式-需要自己擁有服務器)
????3.進入開發者模式,注冊成為開發者,配置接口信息。如:
????????URL:http://192.168.1.1/wx_sample.php
????????Token:weixin (自己填寫一個)
????4.打開wx_sample.php??? $wechatObj->valid(); 這個方法進行驗證
????5.驗證成功,關閉wx_sample.php??? $wechatObj->valid();
????6.馬上使用手機,訪問屬于自己的公眾平臺吧
本例代碼測試方法:
????1.打開手機微信
????2.關注公眾賬號:**** 3.發送一條信息:q北京遇上西雅圖
????4.返回一條磁力鏈接
????5.系統返回的是迅雷磁力鏈接,請您使用電腦版迅雷或手機版迅雷,進行下載觀看
原理:
????1.接收用戶數據,如:q北京遇上西雅圖
????2.到bt搜索引擎進行查詢數據
????3.返回一條磁力鏈接
????ps:由于沒有申請到內測資格,有很多功能都受到限制,如:5秒超時解決不了,
????無法主動推送,批量推送,模擬登錄又不方便等等。。。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
????1.打開http://mp.weixin.qq.com 注冊/登錄
????2.進入高級功能菜單,選擇編輯模式或開發者模式(本例子屬于開發者模式-需要自己擁有服務器)
????3.進入開發者模式,注冊成為開發者,配置接口信息。如:
????????URL:http://192.168.1.1/wx_sample.php
????????Token:weixin (自己填寫一個)
????4.打開wx_sample.php??? $wechatObj->valid(); 這個方法進行驗證
????5.驗證成功,關閉wx_sample.php??? $wechatObj->valid();
????6.馬上使用手機,訪問屬于自己的公眾平臺吧
本例代碼測試方法:
????1.打開手機微信
????2.關注公眾賬號:**** 3.發送一條信息:q北京遇上西雅圖
????4.返回一條磁力鏈接
????5.系統返回的是迅雷磁力鏈接,請您使用電腦版迅雷或手機版迅雷,進行下載觀看
原理:
????1.接收用戶數據,如:q北京遇上西雅圖
????2.到bt搜索引擎進行查詢數據
????3.返回一條磁力鏈接
????ps:由于沒有申請到內測資格,有很多功能都受到限制,如:5秒超時解決不了,
????無法主動推送,批量推送,模擬登錄又不方便等等。。。
[PHP]代碼
| 001 | <?php |
| 002 | /** |
| 003 | ??* wechat php test |
| 004 | ??*/ |
| 005 | header('Content-Type:text/html;charset=utf8'); |
| 006 | date_default_timezone_set('RPC'); |
| 007 | ?? |
| 008 | //define your token |
| 009 | define("TOKEN", "2snH21PBqF7UK");//自定義 |
| 010 | $wechatObj = new wechatCallbackapiTest(); |
| 011 | //$wechatObj->valid();//第一次驗證token時使用 |
| 012 | $wechatObj->responseMsg(); |
| 013 | ?? |
| 014 | class wechatCallbackapiTest |
| 015 | { |
| 016 | ????private $keyword; |
| 017 | ?????? |
| 018 | ????public function valid() |
| 019 | ????{ |
| 020 | ????????$echoStr = $_GET["echostr"]; |
| 021 | ?? |
| 022 | ????????//valid signature , option |
| 023 | ????????if($this->checkSignature()){ |
| 024 | ????????????echo $echoStr; |
| 025 | ????????????exit; |
| 026 | ????????} |
| 027 | ????} |
| 028 | ?????? |
| 029 | ????public function responseMsg() |
| 030 | ????{ |
| 031 | ????????//get post data, May be due to the different environments |
| 032 | ????????$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; |
| 033 | ?? |
| 034 | ????????//extract post data |
| 035 | ????????if (!empty($postStr)){ |
| 036 | ?????????????????? |
| 037 | ????????????????$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); |
| 038 | ????????????????$fromUsername = $postObj->FromUserName; |
| 039 | ????????????????$toUsername = $postObj->ToUserName; |
| 040 | ????????????????$this->keyword = trim($postObj->Content); |
| 041 | ????????????????$time = time(); |
| 042 | ????????????????$textTpl = "<xml> |
| 043 | ????????????????????????????<ToUserName><![CDATA[%s]]></ToUserName> |
| 044 | ????????????????????????????<FromUserName><![CDATA[%s]]></FromUserName> |
| 045 | ????????????????????????????<CreateTime>%s</CreateTime> |
| 046 | ????????????????????????????<MsgType><![CDATA[%s]]></MsgType> |
| 047 | ????????????????????????????<Content><![CDATA[%s]]></Content> |
| 048 | ????????????????????????????<FuncFlag>0</FuncFlag> |
| 049 | ????????????????????????????</xml>";????????????? |
| 050 | ????????????????if(!empty( $this->keyword )) |
| 051 | ????????????????{ |
| 052 | ????????????????????if($this->keyword == 999){ |
| 053 | ????????????????????????$msgType = "text"; |
| 054 | ????????????????????????$contentStr = '當您搜索:q北京遇上西雅圖,系統返回的是迅雷磁力鏈接,請您使用電腦版迅雷或手機版迅雷,進行下載觀看。';//'參數:1, 按下載數查詢;參數:2, 按時間查詢;參數:5, 按質量查詢; 精確查詢請添加雙引號; 例如: q"北京遇上西雅圖"? 1'; |
| 055 | ????????????????????????echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); |
| 056 | ????????????????????????exit; |
| 057 | ????????????????????} |
| 058 | ????????????????????preg_match('#^q(.*)#', $this->keyword, $str); |
| 059 | ????????????????????if($str[1]){ |
| 060 | ????????????????????????$data = $this->getQueryParam($str[1]); |
| 061 | ????????????????????????$contents = $this->getQueryList($data); |
| 062 | ????????????????????????$resutl = $this->getQueryResult($contents); |
| 063 | ????????????????????????$link = str_replace('&','&',urldecode($resutl[1]));//組裝磁力鏈接 |
| 064 | ????????????????????????if($link){ |
| 065 | ????????????????????????????$msgType = "text"; |
| 066 | ????????????????????????????$contentStr = $link; |
| 067 | ????????????????????????????echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); |
| 068 | ????????????????????????} |
| 069 | ????????????????????}else{ |
| 070 | ????????????????????????$msgType = "text"; |
| 071 | ????????????????????????$contentStr = '請您輸入q進行查詢,例如: q北京遇上西雅圖???? 更多幫助請輸入999'; |
| 072 | ????????????????????????echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); |
| 073 | ????????????????????} |
| 074 | ????????????????}else{ |
| 075 | ????????????????????echo "Input something..."; |
| 076 | ????????????????} |
| 077 | ?? |
| 078 | ????????}else { |
| 079 | ????????????echo ""; |
| 080 | ????????????exit; |
| 081 | ????????} |
| 082 | ?????????? |
| 083 | ????} |
| 084 | ?????????? |
| 085 | ????private function checkSignature() |
| 086 | ????{ |
| 087 | ????????$signature = $_GET["signature"]; |
| 088 | ????????$timestamp = $_GET["timestamp"]; |
| 089 | ????????$nonce = $_GET["nonce"];???? |
| 090 | ?????????????????? |
| 091 | ????????$token = TOKEN; |
| 092 | ????????$tmpArr = array($token, $timestamp, $nonce); |
| 093 | ????????sort($tmpArr); |
| 094 | ????????$tmpStr = implode( $tmpArr ); |
| 095 | ????????$tmpStr = sha1( $tmpStr ); |
| 096 | ?????????? |
| 097 | ????????if( $tmpStr == $signature ){ |
| 098 | ????????????return true; |
| 099 | ????????}else{ |
| 100 | ????????????return false; |
| 101 | ????????} |
| 102 | ????} |
| 103 | ?????? |
| 104 | ????//返回q=查詢 |
| 105 | ????function getQueryResult($contents){ |
| 106 | ????????$result = array(); |
| 107 | ????????preg_match('#<a onclick="fclck\(this.href\)" href="(.*)" title="Download via magnet-link">\[magnet-link\]</a>#iUs', $contents, $content); |
| 108 | ????????$result = $content; |
| 109 | ????????return $result; |
| 110 | ????} |
| 111 | ?????? |
| 112 | ?????? |
| 113 | ????//獲取btdigg.org 的查詢數據 |
| 114 | ????function getQueryList($data){ |
| 115 | ????????$data['order'] = $data['order'] ? $data['order'] : 0; |
| 116 | ????????$data['p'] = 0; |
| 117 | ????????$ch = curl_init(); |
| 118 | ?? |
| 119 | ????????curl_setopt($ch, CURLOPT_URL, 'https://btdigg.org/search?'.http_build_query($data)); |
| 120 | ????????curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 121 | ????????curl_setopt($ch, CURLOPT_USERAGENT, 'Google Bot'); |
| 122 | ????????curl_setopt($ch, CURLOPT_TIMEOUT, 4); |
| 123 | ????????curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 124 | ????????curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); |
| 125 | ?????????? |
| 126 | ????????$result = curl_exec($ch); |
| 127 | ????????curl_close($ch); |
| 128 | ????????return $result; |
| 129 | ????} |
| 130 | ?? |
| 131 | ????//獲取查詢參數 |
| 132 | ????function getQueryParam($str){ |
| 133 | ????????$data = array(); |
| 134 | ????????$string = explode(' ', $str); |
| 135 | ?????????? |
| 136 | ????????//是數組 and 最后一個數組是數字 |
| 137 | ????????$last = array_pop($string); |
| 138 | ????????if(is_numeric($last)){ |
| 139 | ????????????$data['q'] = implode(' ', $string); |
| 140 | ????????????$data['order'] = $last; |
| 141 | ????????}else{ |
| 142 | ????????????$data['q'] = $str; |
| 143 | ????????} |
| 144 | ????????return $data; |
| 145 | ????} |
| 146 | ?????? |
| 147 | } |
| 148 | ?? |
| 149 | ?> |
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的(转载)微信公众平台 - 实例(未验证)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通用算法-sql相似度模糊匹配
- 下一篇: Libnids库-网络入侵检测的基础框架