您的位置:首页 > 移动开发 > 微信开发

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

2015-11-05 09:59 676 查看
<?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;
}
}
}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: