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

Web全栈笔记之微信公众平台开发

2016-11-28 08:56 295 查看

1.微信公众测试平台

用微信扫一扫登陆微信公众测试平台



2.接口配置

接入指南

查看接口文档



下载示例代码



示例代码文件设置



然后上传到服务器

公众平台设置接口信息



如果填写信息无误提示配置成功

3.获取access_token

获取access_token文档

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。



请求地址 2小时期限 每天获取次数有限 需要保存下来,过期再重新获取

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

access_token.txt文件

access_token:保存获取到的access_token值,time:记录获取时间

{"access_token":"","time":0}


<?php
$appid = 'wx3ccefa9090f7d87
4000
2';
$secret = 'b6c7ad6f13d8755953afd709365eab1a';
//获取access_token地址
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
//先查看access_token.txt里是否有access_token
$str = file_get_contents("access_token.txt");
$data = json_decode($str);
//判断是否过期,如果是重新获取access_token值
if ($data->time<time()) {
// 获取access_token地址的内容
$str = file_get_contents($url);
// 把获取到的JSON字符串转为json对象
$json = json_decode($str);
//记录获取到的access_token和时间
$data->access_token = $json->access_token;
$data->time = time()+7000;
$str = json_encode($data);
//把获取到access_token值保存到access_token.txt
file_put_contents("access_token.txt", $str);
}


4.获取用户信息

获取用户信息文档





<?php
//用户的openid
$openid = "o-6mGw3buyMYKQz15gQmKeY4KFEg";
// //获取用户信息地址
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$data->access_token.'&openid='.$openid.'&lang=zh_CN';
//获取接口信息
$str = file_get_contents($url);
//把获取到的内容转为json对象
$json = json_decode($str);
$sex = $json->sex==1?"男":"女";
echo "<table>";
echo "<tr>
<td>头像<td>
<td>姓名<td>
<td>性别<td>
<td>城市<td>
</tr>";

echo "<tr>
<td><img style='width:100px' src='{$json->headimgurl}'><td>
<td>{$json->nickname}<td>
<td>{$sex}<td>
<td>{$json->city}<td>
</tr>";

echo "</table>";
?>


5.获取用户列表

获取用户列表文档



// 获取用户列表
$url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$data->access_token;

$str = file_get_contents($url);
$json2 = json_decode($str);

// echo "$json->count";

echo "<table>";
echo "<tr>
<td>头像<td>
<td>姓名<td>
<td>性别<td>
<td>城市<td>
</tr>";

for ($i=0; $i < $json2->count; $i++) {
// echo $json->data->openid[$i]."<br>";

$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$data->access_token.'&openid='.$json2->data->openid[$i].'&lang=zh_CN';
$str = file_get_contents($url);
$json = json_decode($str);
$sex = $json->sex==1?"男":"女";

echo "<tr>
<td><img style='width:100px' src='{$json->headimgurl}'><td>
<td>{$json->nickname}<td>
<td>{$sex}<td>
<td>{$json->city}<td>
</tr>";

}
echo "</table>";


6.用户授权

用户授权文档





<?php
echo "授权页面";
$code = $_GET["code"];//用户确认授权后返回过来的code值
$appid = 'wx3ccefa9090f7d872';
$secret = 'b6c7ad6f13d8755953afd709365eab1a';

//通过code值获取access_token的地址
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';

$str = file_get_contents($url);
$json = json_decode($str);//把获取到的内容转为json对象

//用户接口地址
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$json->access_token.'&openid='.$json->openid.'&lang=zh_CN';

//获取用户接口信息
$str = file_get_contents($url);
//把获取到的内容转为json对象
$json = json_decode($str);
$sex = $json->sex==1?"男":"女";
echo "<table>";
echo "<tr>
<td>头像<td>
<td>姓名<td>
<td>性别<td>
<td>城市<td>
</tr>";

echo "<tr>
<td><img style='width:100px' src='{$json->headimgurl}'><td>
<td>{$json->nickname}<td>
<td>{$sex}<td>
<td>{$json->city}<td>
</tr>";

echo "</table>";

// echo urlencode('http://weixin5.applinzi.com/code.php');
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=http%3A%2F%2Fweixin5.applinzi.com%2Fcode.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect 
?>


7.微信JS-SDK

微信JS-SDK文档









页面代码需要设置APPIP APPSECRET



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