Rumah > php教程 > php手册 > teks badan

PHP 微信接口封装类(PHP100)

WBOY
Lepaskan: 2016-06-21 08:49:57
asal
953 orang telah melayarinya

 

/**

  * 微信公众平台接口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="

fromUsername}]]>

toUsername}]]>

{$this->times}

";

 

      //=================type content============

 switch($type){

 

   case "text" : 

 $con.="

{$o_arr}";  

break;

 

case "news" : 

 $con.="{$o_arr[0]}

";

foreach($value_arr as $id=>$v){

if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数

         $con.="

 

";

}

$con.="

{$o_arr[1]}";  

break;

 

 } //end switch

 

//=================end return============

 echo $con."";

}

 

 

 

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;

}

}

}

 

?>



Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!