微信公众平台开发
2018-06-22 05:26:21来源:未知 阅读 ()
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()->where(array("openid"=>$openid))->one();
if($customer == null){
$customerinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN ';
$customerinfo_json = $this->curl_get($customerinfo_url);
$customerinfo = json_decode($customerinfo_json, true);
$customer = new Customer();
$customer->name = $customerinfo['nickname'];
$customer->headimg = $customerinfo['headimgurl'];
$customer->sex = $customerinfo['sex'];
$customer->openid = $customerinfo['openid'];
$customer->address = $customerinfo['country'].$customerinfo['province'].$customerinfo['city'];
$customer->created_at = time();
$customer->updated_at = time();
$customer->is_del = 0;
$customer->money = 0;
$customer->save();
}
setCookie("is_login", 1, time()+3600*24*30*12, "/");
setCookie("customer_id", $customer->id, time()+3600*24*30*12, "/");
$redirect_url = $hostname;
Header("Location: $redirect_url");
}
public function curl_get($url)
{
$host = parse_url($url, PHP_URL_HOST);
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)');
curl_setopt($ch,CURLOPT_REFERER, $host);
curl_setopt($ch,CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$str = curl_exec($ch);
curl_close($ch);
return $str;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 微信公众平台开发之配置与请求 2019-09-23
- 微信支付:chooseWXPay:fail, the permission value is offl 2019-07-24
- 微信支付和微信支付通知基于sdk的说明 2019-07-24
- 微信支付H5支付开发文档 2019-07-23
- PHP---微信JS-SDK获取access_token/jsapi_ticket/signature 2019-07-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash