function
jssdk(){
$appid
=
'这里替换成你的appid'
;
$secret
=
'这里替换成你的key'
;
$_title
=
'微信'
;
$code
=
$_GET
[
'code'
];
//获取code
$_SESSION
[
'code'
] =
$code
;
//设置code缓存给微信付账使用
$auth
=
file_get_contents
(
"https://api.weixin.qq.com/sns/oauth2/access_token?appid="
.
$appid
.
"&secret="
.
$secret
.
"&code="
.
$code
.
"&grant_type=authorization_code"
);//通过code换取网页授权access_token
$jsonauth
= json_decode(
$auth
);
//对JSON格式的字符串进行编码
$arrayauth
= get_object_vars(
$jsonauth
);
//转换成数组
$openid
=
$arrayauth
[
'openid'
];
//输出openid
$access_token
=
$arrayauth
[
'access_token'
];
$_SESSION
[
'openid'
] =
$openid
;
$accesstoken
=
file_get_contents
(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
.
$appid
.
"&secret="
.
$secret
.
""
);//获取access_token
$token
= json_decode(
$accesstoken
);
//对JSON格式的字符串进行编码
$t
= get_object_vars(
$token
);
//转换成数组
$access_token
=
$t
[
'access_token'
];
//输出access_token
$jsapi
=
file_get_contents
(
"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="
.
$access_token
.
"&type=jsapi"
);
$jsapi
= json_decode(
$jsapi
);
$j
= get_object_vars(
$jsapi
);
$jsapi
=
$j
[
'ticket'
];
//get JSAPI
$time
= 14999923234;
$noncestr
=
$time
;
$jsapi_ticket
=
$jsapi
;
$timestamp
=
$time
;
$url
=
'http://'
.
$_SERVER
[
'HTTP_HOST'
].
$_SERVER
[
'REQUEST_URI'
];
$and
=
"jsapi_ticket="
.
$jsapi_ticket
.
"&noncestr="
.
$noncestr
.
"×tamp="
.
$timestamp
.
"&url="
.
$url
.
""
;
$signature
= sha1(
$and
);
return
$signature
;
}
|
最后的一句代码,就是说,如果你在输出页面使用=jssdk();?>,那么就会输出$signature的内容。其中上面的$time是我手动指定的,这个一定要跟输出页面的js代码的值一样。
输出页面代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
或者直接输出到php页面
require_once "jssdk.php";
$jssdk = new JSSDK("wx78437366c2672eb9", "408e952767c07ab8a116751842e57e56"); $signPackage = $jssdk->GetSignPackage(); ?> |