PHP微信服务器端代码_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > PHP微信服务器端代码

PHP微信服务器端代码

 2017/8/15 23:31:24  happysoul  程序员俱乐部  我要评论(0)
  • 摘要:<?php//引入微信提供的包省的找依赖库include_once"wxBizMsgCrypt.php";//你自己的token、分配给你的appid和密码define("TOKEN","yourtoken");define("APPID","wxb21b4b3b11111111");define("APPSECRET","se8c89e213c8efab31d53fc98s6d2222");$wechatObj=newwechatCallbackapiTest();//验证服务器if
  • 标签:PHP 代码 服务器 服务
class="php">
<?php
//引入微信提供的包省的找依赖库
include_once "wxBizMsgCrypt.php";
  
//你自己的token、分配给你的appid和密码
define("TOKEN", "yourtoken");
define("APPID", "wxb21b4b3b11111111");
define("APPSECRET", "se8c89e213c8efab31d53fc98s6d2222");

$wechatObj = new wechatCallbackapiTest();

//验证服务器
if(isset($_GET["openid"])){
	
	//检查token
	$access_token = $wechatObj->getToken();
    $wechatObj->responseMsg();

}else if (isset($_GET["echostr"])){
	//服务器token验证
    $wechatObj->valid();
}else{
	echo "empty";
}


class wechatCallbackapiTest  
{  
    public function valid()  
    {  
        $echoStr = $_GET["echostr"];
  
        //valid signature , option  
        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 = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
		//$fp = fopen("./post_data.txt", "w+");
		//fwrite($fp, $postStr);
		//fclose($fp);

		$postObj = simplexml_load_string($postStr,'SimpleXMLElement', LIBXML_NOCDATA);
        $RX_TYPE = trim($postObj->MsgType);

           switch($RX_TYPE)
           {
           case "text":
               $resultStr = $this->handleText($postObj);
               break;
           case "event":
               $resultStr = $this->handleEvent($postObj);
               break;
           default:
               $resultStr = "Unknow msg type: ".$RX_TYPE;
               break;
         }
         echo $resultStr;
    }

	//文本消息
    public function handleText($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>0</FuncFlag>
                    </xml>";             
        if(!empty( $keyword ))
        {
            $msgType = "text";
            $contentStr = "你好啊^_^";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        }else{
            echo "Input something...";
        }
    }

    public function handleEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
			//关注事件
            case "subscribe":
                $contentStr = "感谢您关注【我爱财】"."\n"."微信号:happysoul0";
                break;
            default :
                $contentStr = "Unknow Event: ".$object->Event;
                break;
        }
        $resultStr = $this->responseText($object, $contentStr);
        return $resultStr;
    }
    
	//输出返回信息
    public function responseText($object, $content, $flag=0)
    {
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }

	
	//获取token并写入本地
	function getToken(){
		//写入本地的文件名,虽然这样不安全,暂时可以这么调试
		$fileName = "./happysoul_access_token.json";

		$appid=APPID;
		$appsecret=APPSECRET;
		if(!is_file($fileName)){
			$f = fopen($fileName, "w");
			fclose($f);
		}
		$file = file_get_contents($fileName,true);
		$result = json_decode($file,true);
		if (time() > $result['expires']){
			$data = array();
			$data['access_token'] = $this->getNewToken($appid,$appsecret);
			$data['expires']=time()+7000;
			$jsonStr =  json_encode($data);
			$fp = fopen($fileName, "w");
			fwrite($fp, $jsonStr);
			fclose($fp);
			return $data['access_token'];
		}else{
			return $result['access_token'];
		}
	}
	function getNewToken($appid,$appsecret){
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
		$access_token_Arr =  $this->https_request($url);
		return $access_token_Arr['access_token'];
	}
	function https_request ($url){
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			$out = curl_exec($ch);
			curl_close($ch);
			return  json_decode($out,true);
	}

}  
  
?>


附件是微信提供的包,包含 java php c++ c# python


公众平台 - 开发 - 基本配置 (服务器配置)记得验证好了服务器之后要点启用
关注了你的人给你公众号发消息才能发送到你的服务器上
  • cryptoDemo.zip (4.4 MB)
  • 下载次数: 0
发表评论
用户名: 匿名