TOP

微信公众平台开发(一)
2017-10-10 08:34:26 】 浏览:10088
Tags:公众 平台 开发

1.配置微信公众平台通信

首先已经拥有服务号或企业号并已经成为开发者

这时可以拿到微信公众平台登录用户名和密码以及appid和appsecret(共计4个参数)

下载wx_sample.php

该代码完成了验证web网站与微信公众平台之间的通信

代码如下:

<?php
/**
* wechat php test
*/

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

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;
$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 = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
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);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}

?>

在微信开发设置中配置url,token和随机字符串等三个参数并启用:

url即为在web网站生产环境能访问到该wx_sample.php的url

token即为该wx_sanple.php中定义的token的值

noncestr由系统生成

 

2. 在web网页获取code和access_token及带openid的用户信息

先在接口权限中设置授权域名为该web网站的域名

通过appid获取code:

public function actionLogin(){

$params = Yii::$app->params;
$hostname = $params["hostname"];
$appid = $params["appid"];
$appsecret = $params["appsecret"];
$redirect_uri = "http://manhua.51haowenzhang.com/customer/register";
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=manhua#wechat_redirect';
Header("Location: $url");
}

再通过code获取access_token和openid及用户信息,并将用户信息持久化,并使用cookie登录:

public function actionRegister(){
$params = Yii::$app->params;
$hostname = $params["hostname"];
$appid = $params["appid"];
$appsecret = $params["appsecret"];

$code = Yii::$app->request->get('code');

$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$json = $this->curl_get($url);
$token = json_decode($json,true);
$access_token = $token['access_token'];
$openid = $token['openid'];

$customer = Customer::find()->
微信公众平台开发(一) https://www.cppentry.com/bencandy.php?fid=85&id=126481

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php解决约瑟夫环 下一篇前端到后台ThinkPHP开发整站(完)