>백엔드 개발 >PHP 튜토리얼 >关于微信开发模式的PHP代码

关于微信开发模式的PHP代码

WBOY
WBOY원래의
2016-06-23 13:58:20954검색

<?php/**  * wechat php test  *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();//$wechatObj->valid();$wechatObj->responseMsg();class wechatCallbackapiTest{	public function valid()    {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature()){        	echo $echoStr;        	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;				$customevent = $postObj->Event;				$latitude =$postObj->Location_X;				$longitude =$postObj->Location_Y;                $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>";             			switch($type)			{			case "event";			if($customevent == "subscribe")			{$contentStr = "感谢关注"}			break;			case "image"			$contentStr ="图片很棒";		    break;			case "location";			$contentStr ="{$latitude},{longitude}";			break;			case "text";			switch($keyword)			{case "1"			 $contentStr="1"			 break;			 case "2"			 $contentStr ="2"			 break;			 default;			 $contentStr= "hi";}			 break;			 }			 $msgType = "text";			 $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);			 echo $resultStr;			 }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;		}	}}?>

代码检查了N遍但是功能一个都不能实现 !球大神帮帮~另外怎么也做不出来微信调用百度地图来实现导航如果能一起帮助就更好了


回复讨论(解决方案)

$keyword
输出看看

public function valid()    {        $echoStr = $_GET["echostr"];         //valid signature , option        if($this->checkSignature()){            echo $echoStr;            exit;        }    }
这代码是为了填写url 通过验证, 通过之后 ,可以把这个代码改了。如改成
public function valid()    {        $echoStr = $_GET["echostr"];         //valid signature , option        if($this->checkSignature()){            //echo $echoStr;            //exit;		responseMsg();        }    }
 就行了、

微信公众账号,只能看作一种特殊的能识别用户身份的浏览器,你想要识现的功能,大多都像你做网站一下,只是入口不同而已,如果想要百度的导航,就要你的网站接入百度接口  http://api.map.baidu.com/lbsapi/cloud/webservice-placeapi.htm 只是用户的当前位置,可以让用户发地理位置消息。

如果微信 要做菜单,可以看这里 http://pan.baidu.com/s/1eQd12wy

代码有问题啊,case “1”:应该是冒号而不是读号啊,其他的没什么问题

 switch($type)            {            case "event":            if($customevent == "subscribe")            {$contentStr = "感谢关注";}            break;            case "image":            $contentStr ="图片很棒";            break;            case "location":            $contentStr ="{$latitude},{longitude}";            break;            case "text":            switch($keyword)            {case "1":             $contentStr="1";             break;             case "2":             $contentStr ="2";             break;             default;             $contentStr= "hi";}             break;             }

也要改下

default:
$contentStr= "hi";
             

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.