微信公眾平台開發入門--PHP,實現自動回複文本,圖文,點擊事件

來源:互聯網
上載者:User

一頁代碼實現微信基本回複和點擊事件功能,部署上去sae或者bae,妥妥的基本免費的伺服器

不懂代碼都基本每個人都可以做自己的微信公眾號了

valid();}else{                     //回複訊息    $wechatObj->responseMsg();}class wechatCallbackapiTest{    public function valid()    {        $echoStr = $_GET["echostr"];        if($this->checkSignature()){            echo $echoStr;            exit;        }    }    private function checkSignature()    {        $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;        $tmpArr = array($token, $timestamp, $nonce);        sort($tmpArr);        $tmpStr = implode( $tmpArr );        $tmpStr = sha1( $tmpStr );        if( $tmpStr == $signature ){            return true;        }else{            return false;        }    }    //回複訊息    public function responseMsg(){    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];    if (!empty($postStr)){        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);        $RX_TYPE = trim($postObj->MsgType);        switch ($RX_TYPE)        {            case "text":                $resultStr = $this->receiveText($postObj);                break;            case "image":                $resultStr = $this->receiveImage($postObj);                break;            case "location":                $resultStr = $this->receiveLocation($postObj);                break;            case "voice":                $resultStr = $this->receiveVoice($postObj);                break;            case "video":                $resultStr = $this->receiveVideo($postObj);                break;            case "link":                $resultStr = $this->receiveLink($postObj);                break;            case "event":                $resultStr = $this->receiveEvent($postObj);                break;            default:                $resultStr = "unknow msg type: ".$RX_TYPE;                break;        }        echo $resultStr;    }else {        echo "";        exit;    }}        //接收簡訊    private function receiveText($object)    {        $keyword = trim($object->Content);        $url = "http://api100.duapp.com/movie/?appkey=DIY_miaomiao&name=".$keyword;        $output = file_get_contents($url,$keyword);        $contentStr = json_decode($output, true);        if (is_array($contentStr)){            $resultStr = $this->transmitNews($object, $contentStr);        }else{            $resultStr = $this->transmitText($object, $contentStr);        }        return $resultStr;    }        //接收事件,關注等    private function receiveEvent($object)    {        $contentStr = "";        switch ($object->Event)        {            case "subscribe":                $contentStr = "你關注了我";    //關注後回複內容                break;            case "unsubscribe":                $contentStr = "";                break;            case "CLICK":                $contentStr =  $this->receiveClick($object);    //點擊事件                break;            default:                $contentStr = "receive a new event: ".$object->Event;                break;        }                return $contentStr;    }        //接收圖片    private function receiveImage($object)    {        $contentStr = "你發送的是圖片,地址為:".$object->PicUrl;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }            //接收語音    private function receiveVoice($object)    {        $contentStr = "你發送的是語音,媒體ID為:".$object->MediaId;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }        //接收視頻    private function receiveVideo($object)    {        $contentStr = "你發送的是視頻,媒體ID為:".$object->MediaId;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }        //位置訊息    private function receiveLocation($object)    {        $contentStr = "你發送的是位置,緯度為:".$object->Location_X.";經度為:".$object->Location_Y.";縮放層級為:".$object->Scale.";位置為:".$object->Label;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }        //連結訊息    private function receiveLink($object)    {        $contentStr = "你發送的是連結,標題為:".$object->Title.";內容為:".$object->Description.";連結地址為:".$object->Url;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }          

//點擊菜單訊息 private function receiveClick($object) { switch ($object->EventKey) { case "1": $contentStr = "貓咪醬個性DIY服裝, 我們專業定製個性【班服,情侶裝,親子裝等,有長短T恤,衛衣,長短褲】 來圖印製即可,給你溫馨可愛的TA, 有事可直接留言微信"; break; case "2": $contentStr = "你點擊了菜單: ".$object->EventKey; break; case "3": $contentStr = "是傻逼"; break; default: $contentStr = "你點擊了菜單: ".$object->EventKey; break; } //兩種回複 if (is_array($contentStr)){ $resultStr = $this->transmitNews($object, $contentStr); }else{ $resultStr = $this->transmitText($object, $contentStr); } return $resultStr; } //回複簡訊 private function transmitText($object, $content) { $textTpl = " %s "; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $resultStr; }

//回複圖文 private function transmitNews($object, $arr_item) { if(!is_array($arr_item)) return;

$itemTpl = " "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

$newsTpl = " %s %s $item_str ";

$resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item)); return $resultStr; } //音樂訊息 private function transmitMusic($object, $musicArray, $flag = 0) { $itemTpl = " ";

$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);

$textTpl = " %s $item_str %d ";

$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag); return $resultStr; } } ?>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.