PHP 微信公众号开发者模式 第三方接入

23 篇文章 0 订阅


<?php

//define your token
define("TOKEN", "weixin2015");

$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest{
    public function valid(){
        $echoStr = $_GET["echostr"];        
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            $this->responseMsg();//验证后处理用户发关的消息-这里是原样返回用户的消息]
            exit;
        }
    }

    public function responseMsg(){
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        //extract post data
    	if (!empty($postStr)){
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
        		
     			$type = $postObj->MsgType;
                $event = $postObj->Event;

        		$c = $postObj->Location_X;
                $d = $postObj->Location_Y;
        		$Label = $postObj->Label;
        
                $keyword = trim($postObj->Content);
        		$PicUrl = trim($postObj->PicUrl);
         		$msgType = trim($postObj->msgType);
                $time = time();
            	
                $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>1</FuncFlag>
                        </xml>";
        
        		$msgid = $postObj->MsgId;
        		$picurl = $postObj->PicUrl;
        
        		$piccontentTpl="<xml>
                             <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                             <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[%s]]></MsgType>
                             <ArticleCount>1</ArticleCount>
                             <Articles>
                             <item>
                                 <Title><![CDATA[%s]]></Title>
                                 <Description><![CDATA[%s]]></Description>
                                 <PicUrl><![CDATA[%s]]></PicUrl>
                                 <Url><![CDATA[%s]]></Url>
                             </item>
                             </Articles>
                             <FuncFlag>1</FuncFlag>
                             </xml>";//图片消息

        	//天气信息
       		 $textHaderTpl = "<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[news]]></MsgType>
					<Content><![CDATA[]]></Content>
					<ArticleCount>%d</ArticleCount>
					<Articles>";
			$textContentTpl = "<item>
					<Title><![CDATA[%s]]></Title>
					<Description><![CDATA[%s]]></Description>
					<PicUrl><![CDATA[%s]]></PicUrl>
					<Url><![CDATA[%s]]></Url>
					</item>";
			$textFooterTpl = "</Articles>
					<FuncFlag>1</FuncFlag>
					</xml>";
        
        			$imageTpl="<xml>
                             <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                             <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[%s]]></MsgType>
                             <Image>
                            	 <MediaId><![CDATA[%s]]></MediaId>
                             </Image>
                             </xml>";//图片image
              
    	
   		 if(!empty($keyword)){
			 if(1==$keyword){
				$msgType = "text";
				$content1="你的openid: $fromUsername \r\n \r\n TOKEN:".TOKEN.'';
				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,  $content1);
			    echo $resultStr;
			 }else{
				//接入第三方URL、token
				$url = '第三方URL';
				$token = '第三方TOKEN';
				$timestamp = $_GET["timestamp"];
                $nonce = $_GET["nonce"];
                $tmpArr = array($token, $timestamp, $nonce);
	            sort($tmpArr, SORT_STRING);
	            $tmpStr = implode( $tmpArr );
	            $tmpStr = sha1( $tmpStr );
                $url .= stripos($url, '?') === false ? '?' : '&';
                $url .= http_build_query(array(
                      'signature' => $tmpStr,
                      'timestamp' => $timestamp,
                      'nonce' => $nonce,
                ));
				//echo $url;
				//如果不是项目相关的事件 $returnStr返回空
				$header = array();
				//$header[] = "Content-type: text/xml";//定义content-type为xml
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
				//curl_setopt($ch, CURLOPT_POSTFIELDS, $pp);
				$response = curl_exec($ch);
				if(curl_errno($ch)){
				    error_log (curl_error($ch));
				}
				curl_close($ch);
				echo $response;
			 
			 }
             
             
             
            }else{            
        		if($event=="subscribe"){//
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, "欢迎关注");
                    echo  $resultStr;
                }else if($type=='location'){//
                     
                }
                echo "Input something...";
            }

    }else {
        echo "";
        exit;
    }
}

		
	private function checkSignature(){
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr,SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值