小程序Openid 獲取,服務器 encryptedData 解密 遇到的坑
阿新 • • 發佈:2018-05-24
font 服務 參數 通過 enc 錯誤 url編碼 openid 微信
獲取客戶 openId 和 unionId 需要以下步驟(都為必須步驟)
1.從驗證從客戶端傳上來code, 獲取sessionKey (需要配合小程序appid ,secret 發送到微信服務器)
$params = [ ‘appid‘ => $this->appid, ‘secret‘ => $this->secret, ‘js_code‘ => $this->code, ‘grant_type‘ => $this->grant_type ];
2.獲取到微信服務器 返回信息
(通過客戶端傳來的 原始數據 + 獲取的sessionKey 與客戶端傳來的 簽名對比)
$signature = sha1($this->rawData . $this->sessionKey);
if ($signature !== $this->signature){ return $this->ret_message("signNotMatch"."sessionKey 簽名不匹配"); }
3.重要環節:
通過 服務器返回的 session_key 解密 客戶端上傳的加密數據
/* 3.通過 服務器返回的 session_key 解密 客戶端上傳的加密數據 * 需要參數 * [ "appid"=>$this->appid, "sessionKey"=>$sessionKey, "encryptedData"=>$this->encryptedData, "iv"=>$this->iv, ];*/
坑:客戶端上傳的 encryptedData 需要用encodeURIComponent方法進行URL編碼,對應服務端需要URL解碼
坑:客戶端上傳的 encryptedData 需要用encodeURIComponent方法進行URL編碼,對應服務端需要URL解碼
坑:客戶端上傳的 encryptedData 需要用encodeURIComponent方法進行URL編碼,對應服務端需要URL解碼
重要的事情說三遍!
小程序:
encodeURIComponent(data.encryptedData);//一定要把加密串轉成URL編碼
對應服務端需要進行URL解碼 否則出現解密結果 為NULL的錯誤
服務端
<?php //php 端接受數據 時做url解碼 $encryptedData=urldecode($_GET["encryptedData"]);
小程序Openid 獲取,服務器 encryptedData 解密 遇到的坑