PHP 微信介面封裝類(PHP100)

來源:互聯網
上載者:User

 

<?php/**  * 微信公眾平台介面PHP100封裝功能類檔案  * author PHP100.com  張恩民  * date 2013-4-9 PRC:E+8 23:03  * linkme QQ925939 chuangen.com  */ define("TOKEN", "chuangenit");$wechatObj = new wechatCallbackapiTest();$wechatObj->weixin_run(); //執行接收器方法 class wechatCallbackapiTest{   private $fromUsername;   private $toUsername;   private $times;   private $keyword;         public function weixin_run(){     $this->responseMsg();$arr[]=array("創恩很靠譜,PHP100也不錯!","aa","http://www.chuangen.com/pic/hj/s_4.png","http://www.chuangen.com");$arr[]=array("創恩很靠譜,PHP100也不錯!","aa","http://www.chuangen.com/pic/hj/s_4.png","http://www.chuangen.com");$arr[]=array("創恩很靠譜,PHP100也不錯!","aa","http://www.chuangen.com/pic/hj/s_4.png","http://www.chuangen.com");$this->fun_xml("news",$arr,array(2,0));    }        function t($n){   include("t_api.php");   $c_name=$t_api[$n];   $json=file_get_contents("http://m.weather.com.cn/data/101110310.html");   return json_decode($json);  }         public function valid()    {        $echoStr = $_GET["echostr"];         //valid signature , option        if($this->checkSignature()){        echo $echoStr;        exit;        }    }     public function responseMsg()    {$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){              $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);                $this->fromUsername = $postObj->FromUserName;                $this->toUsername = $postObj->ToUserName;                $this->keyword = trim($postObj->Content);$this->times = time();        }else {        echo "this a file for weixin API!";        exit;        }    } //微信封裝類,//type: text 文本類型, news 圖文類型//text,array(內容),array(ID)//news,array(array(標題,介紹,圖片,超連結),...小於10條),array(條數,ID) private function fun_xml($type,$value_arr,$o_arr=array(0)){ //=================xml header============ $con="<xml><ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName><FromUserName><![CDATA[{$this->toUsername}]]></FromUserName><CreateTime>{$this->times}</CreateTime><MsgType><![CDATA[{$type}]]></MsgType>";       //=================type content============ switch($type){    case "text" :  $con.="<Content><![CDATA[{$value_arr[0]}]]></Content><FuncFlag>{$o_arr}</FuncFlag>";  break; case "news" :  $con.="<ArticleCount>{$o_arr[0]}</ArticleCount><Articles>";foreach($value_arr as $id=>$v){if($id>=$o_arr[0]) break; else null; //判斷數組數不超過設定數         $con.="<item><Title><![CDATA[{$v[0]}]]></Title> <Description><![CDATA[{$v[1]}]]></Description><PicUrl><![CDATA[{$v[2]}]]></PicUrl><Url><![CDATA[{$v[3]}]]></Url></item>";}$con.="</Articles><FuncFlag>{$o_arr[1]}</FuncFlag>";  break;  } //end switch //=================end return============ echo $con."</xml>";}   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;}}} ?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.