微信invalidsignaturee 微信 怎么获取

微信JSSDK获取当前地理位置信息
& 果果文库所有资源均来源于互联网,仅供网友学习交流,若侵犯了您的权益,请联系我们予以删除。
13151&&人浏览
5974&&人浏览
7496&&人浏览
15012&&人浏览
19210&&人浏览
10055&&人浏览
16012&&人浏览
12467&&人浏览
969&&人浏览
7916&&人浏览
16367&&人浏览
2444&&人浏览
8429&&人浏览
13289&&人浏览
13472&&人浏览
本文标题:微信JSSDK获取当前地理位置信息 链接地址:
copyright@ 2013- Inc. All Rights Reserved 果果文库 版权所有 联系站长: ; 经营许可证编号:浙ICP备号问题: PHP开发微信无法获取到signature,timestamp,nonce
描述:&?php namespace HomeCuse ThinkCdefine("TOKEN","weixin");/* *微信的入口文件 */class WechatController extends Controller {
protected $U
//微信用户对象
protected $app_
protected $
/*通用入口 构造方法
*aunthor:caodi
public function _initialize() {
$this-&app_id = C("APPID");
$this-&secret = C("APPSECRET");
/*微信入口
*author:caodi
public function wechat() {
DLOG("微信入口记录的时间","run","caodi");
if ($_GET['echostr'] != NULL ) {
echo $_GET['echostr'];
//微信只会在第一次在URL中带echostr参数,以后就不会带这个参数了
if ($this-&checkSignature()) { //success!
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)) {
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr,"SimpleXMLElement",LIBXML_NOCDATA);
$this-&$User = $postO
//根据消息类型将信息分发
$this-&route($postObj);
//以下为测试用的
$toUsername = $postObj-&ToUserN
$fromUsername = $postObj-&FromUserN
$keyword = trim($postObj-&Content);
$msyType = trim($postObj-&MsgType); //消息类型
$event = trim($postObj-&Event); //事件类型
$time = time();
$result = json_encode($postObj);
DLOG("消息的参数".$result,"run","caodi");
$textTpl = "&xml&
&ToUserName&&![CDATA[%s]]&&/ToUserName&
&FromUserName&&![CDATA[%s]]&&/FromUserName&
&CreateTime&%s&/CreateTime&
&MsgType&&![CDATA[%s]]&&/MsgType&
&Content&&![CDATA[%s]]&&/Content&
&FuncFlag&0&/FuncFlag&
if ($event == "subscribe") {
$msgType = "text";
$contentStr = date("Y-m-d H:i:s",time());
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultS
echo "error";
/*wechat身份验证
*author:caodi
public function checkSignature() {
//you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception("TOKEN is not defined!");
= $_GET["nonce"];
$token = TOKEN;
$timestamp = $_GET["timestamp"];
$signature = $_GET["signature"];
echo $signature."&br/&";
echo $timestamp."&br/&";
echo $nonce."&br/&";
$tmpArr = array($token,$timestamp,$nonce);
sort($tmpArr,SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
echo "true";
echo "false";
/*根据微信的消息类型来进行的分发
*author:caodi
public function route($postObj) {
$msgType = trim($postObj-&MsgType);
DLOG("mygtype=".$msgType,"run","caodi");
switch ($msgType) {
//(1)接受的为消息推送
case "text":
$this-&reponse_text($postObj);
case "image":
$this-&reponse_image($postObj);
case "voice":
$this-&reponse_voice($postObj);
//(2)接受的为事件推送
case "event":
$event = $postObj-&E
DLOG("event=".$event,"run","caodi");
switch ($event) {
case "subscribe":
$this-&subscribe($postObj);
case "unsubscribe":
$this-&unsubscribe($postObj);
//自定义菜单的事件功能
/*微信用户关注微信号事件(获取用户的基本信息存入到用户表中去)
*author:caodi
public function subscribe($postObj) {
$open_id = $postObj-&FromUserN
$create_time = $postObj-&CreateT
$UserDao = M("user");
//(1)根据用户的open_id去 https://api./cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
$access_token = "RQ4fmRD-a2JflW7_9-mmefNkHnK35aoZHHXn9PoB_vqDfxVWdT8XNbtfv5F1v1yK_b81Xar3So4gRLdlX6QxJfa5fGApcOAeLI_Fx3h9hxGjkNhUgADXidNBKIi5EjanHOZjADAVCN";
$url = "https://api./cgi-bin/user/info?access_token=".$access_token."&openid=".$open_id."&lang=zh_CN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求保存的结果到字符串还是输出在屏幕上,非0表示保存到字符串中
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证来源的检查,0表示阻止对证书的合法性检查
$result = curl_exec($ch);
DLOG("result".$result,"run","caodi");
curl_close($ch);
$user_info = json_decode($result,true);
//(2)将得到的用户信息保存到数据库中去
$data = array();
$data['user_nick'] = $user_info['nickname'];
$user_info['sex'] = $user_info['sex'] == 0 ? 1 : $user_info['sex']; //将性别为0的转化为默认的男性
$data['user_sex'] = $user_info['sex'];
$data['user_avatar'] = $user_info['headimgurl'];
$data['user_type'] = 1;//用户类型 1-普通用户 2-助理
$open_id = json_decode($open_id,true);
$data['wx_open_id'] = $user_info['openid'];
$data['user_app_version'] = "wechat9.0";
$data['user_platform'] = "wechat";
//当前使用的设备平台
$data['user_create_time'] = date("Y-m-d H:i:s",time());
$result = $UserDao-&add($data);
DLOG("sql= ".$UserDao-&getlastsql(),"run","caodi");
if($result === false) {
DLOG("数据库插入失败","run","caodi");
/*自定义菜单的生成
*author:caodi
public function create_menu(){
include_once(APP_PATH."Common/Conf/menu_config.php");
$data = $menu_
$access_token = "RQ4fmRD-a2JflW7_9-mmefNkHnK35aoZHHXn9PoB_vqDfxVWdT8XNbtfv5F1v1yK_b81Xar3So4gRLdlX6QxJfa5fGApcOAeLI_Fx3h9hxGjkNhUgADXidNBKIi5EjanHOZjADAVCN";
$url = "https://api./cgi-bin/menu/create?access_token=".$access_
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
/*通过OAuth2.0的网页授权(自定义菜单中,获取用户的openID同时进入我的任务页)
*author:caodi
public function my_task () {
$code = $_GET['code'];
$oprn_id = $this-&code_to_openID($code);
var_dump($code);
echo "caodi"."&br&";
echo "&center&&h1&{$open_id}&/h1&&/center&";
/*由OAuth2.0获取到的code转化成用户的openID
*author:caodi
public function code_to_openID($code) {
if (empty($code) == true) {
DLOG("获取的code为空","run","caodi");
$appid = $this-&app_
$secret = $this-&
$url = "https://api./sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
DLOG("由OAuth2.0获取到的code转化成用户的openID的结果=".$result,"run","caodi");
curl_close($ch);
$user_info = json_decode($result,true);
$open_id = $user_info['openid'];
return $open_
上一篇: 下一篇:11177人阅读
微信开发(5)
微信接口调用验证最终需要用到的三个参数noncestr、timestamp、signature:
接下来将会给出获取这三个参数的详细代码
本文的环境eclipse + maven
本文使用到的技术HttpClient、Json字符串转map、sha1加密
二、需要用到的jar包
maven依赖的包有:
1、HttpClient包依赖
&org.apache.httpcomponents&
&httpcore&
&org.apache.httpcomponents&
&httpclient&
2、json转map相关包依赖
&net.sf.json-lib&
&json-lib&
三、运行结果
四、详细代码
package com.luo.util
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.ArrayList
import java.util.HashMap
import java.util.Iterator
import java.util.List
import java.util.Map
import java.util.Set
import java.util.UUID
import net.sf.json.JSONObject
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.ParseException
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.methods.HttpUriRequest
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.message.BasicNameValuePair
import org.apache.http.protocol.HTTP
import org.apache.http.util.EntityUtils
public class HttpXmlClient {
public static String post(String url, Map&String, String& params) {
DefaultHttpClient httpclient = new DefaultHttpClient()
String body = null
HttpPost post = postForm(url, params)
body = invoke(httpclient, post)
httpclient.getConnectionManager().shutdown()
return body
public static String get(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient()
String body = null
HttpGet get = new HttpGet(url)
body = invoke(httpclient, get)
httpclient.getConnectionManager().shutdown()
return body
private static String invoke(DefaultHttpClient httpclient,
HttpUriRequest httpost) {
HttpResponse response = sendRequest(httpclient, httpost)
String body = paseResponse(response)
return body
private static String paseResponse(HttpResponse response) {
HttpEntity entity = response.getEntity()
String charset = EntityUtils.getContentCharSet(entity)
String body = null
body = EntityUtils.toString(entity)
} catch (ParseException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
return body
private static HttpResponse sendRequest(DefaultHttpClient httpclient,
HttpUriRequest httpost) {
HttpResponse response = null
response = httpclient.execute(httpost)
} catch (ClientProtocolException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
return response
private static HttpPost postForm(String url, Map&String, String& params) {
HttpPost httpost = new HttpPost(url)
List&NameValuePair& nvps = new ArrayList&NameValuePair&()
Set&String& keySet = params.keySet()
for (String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key)))
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8))
} catch (UnsupportedEncodingException e) {
e.printStackTrace()
return httpost
public static void main(String[] args) {
//获取access_token
Map&String, String& params = new HashMap&String, String&()
params.put("corpid","wx5f24fa0db1819ea2")
params.put("corpsecret","uQtWzF0bQtl2KRHX0amekjpq8L0aO96LSpSNfctOBLRbuYPO4DUBhMn0_v2jHS-9")
String xml = HttpXmlClient.post("https://qyapi./cgi-bin/gettoken",params)
JSONObject jsonMap
= JSONObject.fromObject(xml)
Map&String, String& map = new HashMap&String, String&()
Iterator&String& it = jsonMap.keys()
while(it.hasNext()) {
String key = (String) it.next()
String u = jsonMap.get(key).toString()
map.put(key, u)
String access_token = map.get("access_token")
System.out.println("access_token=" + access_token)
//获取ticket
params.put("access_token",access_token)
xml = HttpXmlClient.post("https://qyapi./cgi-bin/get_jsapi_ticket",params)
= JSONObject.fromObject(xml)
map = new HashMap&String, String&()
it = jsonMap.keys()
while(it.hasNext()) {
String key = (String) it.next()
String u = jsonMap.get(key).toString()
map.put(key, u)
String jsapi_ticket = map.get("ticket")
System.out.println("jsapi_ticket=" + jsapi_ticket)
//获取签名signature
String noncestr = UUID.randomUUID().toString()
String timestamp = Long.toString(System.currentTimeMillis() / 1000)
String url="http://mp."
String str = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + noncestr +
"&timestamp=" + timestamp +
"&url=" + url
//sha1加密
String signature = SHA1(str)
System.out.println("noncestr=" + noncestr)
System.out.println("timestamp=" + timestamp)
System.out.println("signature=" + signature)
//最终获得调用微信js接口验证需要的三个参数noncestr、timestamp、signature
public static String SHA1(String str) {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1")
digest.update(str.getBytes())
byte messageDigest[] = digest.digest()
// Create Hex String
StringBuffer hexStr = new StringBuffer()
// 字节数组转换为 十六进制 数
for (int i = 0
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF)
if (shaHex.length() & 2) {
hexStr.append(0)
hexStr.append(shaHex)
return hexStr.toString()
} catch (NoSuchAlgorithmException e) {
e.printStackTrace()
return null
五、工程下载
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:735733次
积分:7538
积分:7538
排名:第2743名
原创:94篇
评论:508条
阅读:43843
文章:18篇
阅读:287937
文章:55篇
阅读:258825
(1)(1)(1)(1)(2)(6)(7)(14)(18)(15)(26)(2)微信开发之使用java获取签名signature
字体:[ ] 类型:转载 时间:
这篇文章主要为大家详细介绍了微信开发之使用java获取签名signature,感兴趣的小伙伴们可以参考一下
微信接口调用验证最终需要用到的三个参数noncestr、timestamp、signature:
接下来将会给出获取这三个参数的详细代码
本文的环境eclipse + maven
本文使用到的技术HttpClient、Json字符串转map、sha1加密
二、需要用到的jar包
maven依赖的包有:
1、HttpClient包依赖
&dependency&
&groupId&org.apache.httpcomponents&/groupId&
&artifactId&httpcore&/artifactId&
&version&4.4.3&/version&
&/dependency&
&dependency&
&groupId&org.apache.httpcomponents&/groupId&
&artifactId&httpclient&/artifactId&
&version&4.5.1&/version&
&/dependency&
2、json转map相关包依赖
&dependency&
&groupId&net.sf.json-lib&/groupId&
&artifactId&json-lib&/artifactId&
&version&2.4&/version&
&classifier&jdk15&/classifier&
&/dependency&
&dependency&
&groupId&xom&/groupId&
&artifactId&xom&/artifactId&
&version&1.2.5&/version&
&/dependency&
三、运行结果
四、详细代码
package com.luo.
import java.io.IOE
import java.io.UnsupportedEncodingE
import java.security.MessageD
import java.security.NoSuchAlgorithmE
import java.util.ArrayL
import java.util.HashM
import java.util.I
import java.util.L
import java.util.M
import java.util.S
import java.util.UUID;
import net.sf.json.JSONO
import org.apache.http.HttpE
import org.apache.http.HttpR
import org.apache.http.NameValueP
import org.apache.http.ParseE
import org.apache.http.client.ClientProtocolE
import org.apache.http.client.entity.UrlEncodedFormE
import org.apache.http.client.methods.HttpG
import org.apache.http.client.methods.HttpP
import org.apache.http.client.methods.HttpUriR
import org.apache.http.impl.client.DefaultHttpC
import org.apache.http.message.BasicNameValueP
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityU
public class HttpXmlClient {
public static String post(String url, Map&String, String& params) {
DefaultHttpClient httpclient = new DefaultHttpClient();
String body =
HttpPost post = postForm(url, params);
body = invoke(httpclient, post);
httpclient.getConnectionManager().shutdown();
public static String get(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient();
String body =
HttpGet get = new HttpGet(url);
body = invoke(httpclient, get);
httpclient.getConnectionManager().shutdown();
private static String invoke(DefaultHttpClient httpclient,
HttpUriRequest httpost) {
HttpResponse response = sendRequest(httpclient, httpost);
String body = paseResponse(response);
private static String paseResponse(HttpResponse response) {
HttpEntity entity = response.getEntity();
String charset = EntityUtils.getContentCharSet(entity);
String body =
body = EntityUtils.toString(entity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
private static HttpResponse sendRequest(DefaultHttpClient httpclient,
HttpUriRequest httpost) {
HttpResponse response =
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
private static HttpPost postForm(String url, Map&String, String& params) {
HttpPost httpost = new HttpPost(url);
List&NameValuePair& nvps = new ArrayList&NameValuePair&();
Set&String& keySet = params.keySet();
for (String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
public static void main(String[] args) {
//获取access_token
Map&String, String& params = new HashMap&String, String&();
params.put("corpid","wx5f24fa0db1819ea2");
params.put("corpsecret","uQtWzF0bQtl2KRHX0amekjpq8L0aO96LSpSNfctOBLRbuYPO4DUBhMn0_v2jHS-9");
String xml = HttpXmlClient.post("https://qyapi./cgi-bin/gettoken",params);
JSONObject jsonMap = JSONObject.fromObject(xml);
Map&String, String& map = new HashMap&String, String&();
Iterator&String& it = jsonMap.keys();
while(it.hasNext()) {
String key = (String) it.next();
String u = jsonMap.get(key).toString();
map.put(key, u);
String access_token = map.get("access_token");
System.out.println("access_token=" + access_token);
//获取ticket
params.put("access_token",access_token);
xml = HttpXmlClient.post("https://qyapi./cgi-bin/get_jsapi_ticket",params);
jsonMap = JSONObject.fromObject(xml);
map = new HashMap&String, String&();
it = jsonMap.keys();
while(it.hasNext()) {
String key = (String) it.next();
String u = jsonMap.get(key).toString();
map.put(key, u);
String jsapi_ticket = map.get("ticket");
System.out.println("jsapi_ticket=" + jsapi_ticket);
//获取签名signature
String noncestr = UUID.randomUUID().toString();
String timestamp = Long.toString(System.currentTimeMillis() / 1000);
String url="http://mp.";
String str = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + noncestr +
"&timestamp=" + timestamp +
//sha1加密
String signature = SHA1(str);
System.out.println("noncestr=" + noncestr);
System.out.println("timestamp=" + timestamp);
System.out.println("signature=" + signature);
//最终获得调用微信js接口验证需要的三个参数noncestr、timestamp、signature
* @author:罗国辉
* @date: 日 上午9:24:43
* @description: SHA、SHA1加密
* @parameter: str:待加密字符串
* @return: 加密串
public static String SHA1(String str) {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
digest.update(str.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexStr = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i & messageDigest. i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() & 2) {
hexStr.append(0);
hexStr.append(shaHex);
return hexStr.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
五、工程下载
更多精彩内容请点击《》,《》欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 微信 js获取signature 的文章

 

随机推荐