当前位置:主页 > 查看内容

php开发微信公众号代码封装(含post-get-getToken)

发布时间:2021-07-14 00:00| 位朋友查看

简介:php开发微信公众号代码封装 注意php版本不要大于7.x,不然* $postStr $GLOBALS[“HTTP_RAW_POST_DATA”] ;*这句代码会受影响; ?php define ( token , aaabbbccc ) ; define ( appid , 假假我爱过你 ) ; define ( appsecret , 希望你过的更好祝你幸福~~~ ) ; /……

php开发微信公众号代码封装
注意,php版本不要大于7.x,不然*$postStr = $GLOBALS[“HTTP_RAW_POST_DATA”] ;*这句代码会受影响;

<?php 
define("token", "aaabbbccc");
define("appid", "假假,我爱过你!");
define("appsecret", "希望你过的更好!祝你幸福~~~");
// 注意php版本不要7.x以上
// 调用下面的方法
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$wx = new wechatCallbackapiTest();
if (isset($_GET['echostr'])){
   $wx->valid();
}else if(!empty($postStr)){
	$wx->responseMsg();
}



//类
class wechatCallbackapiTest
{
	//判断是微信服务器发来的还是用户发来的并作出反应
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
			ob_clean();
            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"];
	  global $postStr;
	   
	 
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            //用户发送的消息类型判断
            switch ($RX_TYPE)
            {
                case "text":    //文本消息
                    $result = $this->receiveText($postObj);
                    break;
                case "image":   //图片消息
                    $result = $this->receiveImage($postObj);
                    break;

                case "voice":   //语音消息
                    $result = $this->receiveVoice($postObj);
                    break;
                case "video":   //视频消息
                    $result = $this->receiveVideo($postObj);
                    break;
                case "location"://位置消息
                    $result = $this->receiveLocation($postObj);
                    break;
                case "link":    //链接消息
                    $result = $this->receiveLink($postObj);
                    break;
                default:
                    $result = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    /*
     * 接收文本消息
     */
    private function receiveText($object)
    {
        $content = "你发送的是文本,内容为:".$object->Content;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收图片消息
     */
    private function receiveImage($object)
    {
        $content = "你发送的是图片,地址为:".$object->PicUrl;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收语音消息
     */
    private function receiveVoice($object)
    {
        $content = "你发送的是语音,媒体ID为:".$object->MediaId;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收视频消息
     */
    private function receiveVideo($object)
    {
        $content = "你发送的是视频,媒体ID为:".$object->MediaId;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收位置消息
     */
    private function receiveLocation($object)
    {
        $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收链接消息
     */
    private function receiveLink($object)
    {
        $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 回复文本消息
     */
    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }
	
	
	public function post($Url,$postDataArr){
			$postJosnData = json_encode($postDataArr);
			$ch = curl_init($Url);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
			$data = curl_exec($ch);
			return $data;
	}
	
	public function get($url){
	    $oCurl = curl_init();
	    if(stripos($url,"https://")!==FALSE){
	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
	        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
	    }
	    curl_setopt($oCurl, CURLOPT_URL, $url);//目标URL
	    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );//设定是否显示头信息,1为显示
	    curl_setopt($oCurl, CURLOPT_BINARYTRANSFER, true) ;//在启用CURLOPT_RETURNTRANSFER时候将获取数据返回
	    $sContent = curl_exec($oCurl);
	    $aStatus = curl_getinfo($oCurl);//获取页面各种信息
	    curl_close($oCurl);
	    if(intval($aStatus["http_code"])==200){
			$sContent = json_decode($sContent, true);
			// 转成json数组形式
			return $sContent;
	    }else{
	        return false;
	    }
	}
	public function getToken($url){
		$url=str_replace("APPID",appid,$url);
		$url=str_replace("APPSECRET",appsecret,$url);
		$data=$this->get($url);
		return $data;
	}
}




?>

;原文链接:https://blog.csdn.net/weixin_48462578/article/details/115712697
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐