checksignature java_java - PHP开发微信无法获取到signature,timestamp,nonce

namespace HomeController;

use ThinkController;

define("TOKEN","weixin");

/*

*微信的入口文件

*/

class WechatController extends Controller {

protected $User; //微信用户对象

protected $app_id;

protected $secret;

/*通用入口 构造方法

*aunthor:caodi

*date:2015-09-25

*/

public function _initialize() {

$this->app_id = C("APPID");

$this->secret = C("APPSECRET");

}

/*微信入口

*author:caodi

*date:2015-09-22

*/

public function wechat() {

DLOG("微信入口记录的时间","run","caodi");

if ($_GET['echostr'] != NULL ) {

echo $_GET['echostr'];

exit;

}

//微信只会在第一次在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 = $postObj;

//根据消息类型将信息分发

$this->route($postObj);

//exit;

//以下为测试用的

$toUsername = $postObj->ToUserName;

$fromUsername = $postObj->FromUserName;

$keyword = trim($postObj->Content);

$msyType = trim($postObj->MsgType); //消息类型

$event = trim($postObj->Event); //事件类型

$time = time();

$result = json_encode($postObj);

DLOG("消息的参数".$result,"run","caodi");

$textTpl = "

%s

0

";

if ($event == "subscribe") {

$msgType = "text";

$contentStr = date("Y-m-d H:i:s",time());

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}

}

} else {

echo "error";

}

}

/*wechat身份验证

*author:caodi

*date:2015-09-22

*/

public function checkSignature() {

//you must define TOKEN by yourself

if (!defined("TOKEN")) {

throw new Exception("TOKEN is not defined!");

}

$nonce = $_GET["nonce"];

$token = TOKEN;

$timestamp = $_GET["timestamp"];

$signature = $_GET["signature"];

echo $signature."
";

echo $timestamp."
";

echo $nonce."
";

$tmpArr = array($token,$timestamp,$nonce);

sort($tmpArr,SORT_STRING);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

if ($tmpStr == $signature) {

return true;

echo "true";

} else {

return false;

echo "false";

}

}

/*根据微信的消息类型来进行的分发

*author:caodi

*date:2015-09-23

*/

public function route($postObj) {

$msgType = trim($postObj->MsgType);

DLOG("mygtype=".$msgType,"run","caodi");

switch ($msgType) {

//(1)接受的为消息推送

case "text":

$this->reponse_text($postObj);

break;

case "image":

$this->reponse_image($postObj);

break;

case "voice":

$this->reponse_voice($postObj);

break;

//(2)接受的为事件推送

case "event":

$event = $postObj->Event;

DLOG("event=".$event,"run","caodi");

switch ($event) {

case "subscribe":

$this->subscribe($postObj);

break;

case "unsubscribe":

$this->unsubscribe($postObj);

break;

//自定义菜单的事件功能

}

}

}

/*微信用户关注微信号事件(获取用户的基本信息存入到用户表中去)

*author:caodi

*date:2015-09-23

*/

public function subscribe($postObj) {

$open_id = $postObj->FromUserName;

$create_time = $postObj->CreateTime;

$UserDao = M("user");

//(1)根据用户的open_id去 https://api.weixin.qq.com/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.weixin.qq.com/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");

exit;

}

}

/*自定义菜单的生成

*author:caodi

*date:2015-09-24

*/

public function create_menu(){

include_once(APP_PATH."Common/Conf/menu_config.php");

$data = $menu_config;

$access_token = "RQ4fmRD-a2JflW7_9-mmefNkHnK35aoZHHXn9PoB_vqDfxVWdT8XNbtfv5F1v1yK_b81Xar3So4gRLdlX6QxJfa5fGApcOAeLI_Fx3h9hxGjkNhUgADXidNBKIi5EjanHOZjADAVCN";

$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

$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);

exit;

}

/*通过OAuth2.0的网页授权(自定义菜单中,获取用户的openID同时进入我的任务页)

*author:caodi

*date:2015-09-24

*/

public function my_task () {

$code = $_GET['code'];

$oprn_id = $this->code_to_openID($code);

var_dump($code);

echo "caodi"."
";

echo "

{$open_id}

";

}

/*由OAuth2.0获取到的code转化成用户的openID

*author:caodi

*date:2015-09-24

*/

public function code_to_openID($code) {

if (empty($code) == true) {

DLOG("获取的code为空","run","caodi");

exit;

}

$appid = $this->app_id;

$secret = $this->secret;

$url = "https://api.weixin.qq.com/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_id;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
企业微信开发回调验证是指通过验证企业微信接收到的外部事件回调(例如消息、联系人等),确保这些事件确实是由企业微信发送的。下面给出一个用Java代码实现企业微信开发回调验证的示例: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class CallbackVerifier { private static final String TOKEN = "your_token"; // 企业微信后台配置的Token // 验证回调请求的签名是否合法 public static boolean verifySignature(String signature, String timestamp, String nonce) { String[] arr = {TOKEN, timestamp, nonce}; Arrays.sort(arr); // 字典序排序 StringBuilder content = new StringBuilder(); for (String s : arr) { content.append(s); } return signature.equals(sha1(content.toString())); } // 使用SHA1算法计算字符串的哈希值 private static String sha1(String str) { try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.update(str.getBytes()); byte[] bytes = digest.digest(); StringBuilder sb = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(b & 0xff); if (hex.length() == 1) { sb.append("0"); } sb.append(hex); } return sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } // 测试 public static void main(String[] args) { String signature = "signature"; // 从企业微信接收到的请求参数 String timestamp = "timestamp"; String nonce = "nonce"; boolean isValid = verifySignature(signature, timestamp, nonce); System.out.println("是否合法:" + isValid); } } ``` 在上述代码中,首先定义了一个TOKEN常量,即企业微信后台配置的Token。然后定义了一个verifySignature方法,该方法接收从企业微信接收到的signaturetimestampnonce参数,并通过字典序排序和SHA1算法生成待验证的签名。最后,通过判断生成的签名是否与接收到的signature一致来验证回调请求的合法性。 该示例代码可以直接运行,并且可以将TOKEN替换为实际的Token进行验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值