最近寫了個微信公眾號,遇到不少坑,第一個坑就是 提供的 消息回復的 php sdk 有幾個方法過時了,當然,這個也不能怪微信,主要是我用的php版是7.1的,不過好像5.4還是幾都廢了
pkcs7Encoder.php 首頁是這個文件里面的
/** * 對明文進行加密 * @param string $text 需要加密的明文 * @return string 加密后的密文 */ public function encrypt($text, $appid) { try { //獲得16位隨機字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $appid; // 網絡字節序 $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); //使用自定義的填充方式對明文進行補位填充 $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); mcrypt_generic_init($module, $this->key, $iv); //加密 $encrypted = mcrypt_generic($module, $text); mcrypt_generic_deinit($module); mcrypt_module_close($module); //print(base64_encode($encrypted)); //使用BASE64對加密后的字符串進行編碼 return array(ErrorCode::$OK, base64_encode($encrypted)); } catch (Exception $e) { //print $e; return array(ErrorCode::$EncryptAESError, null); } } /** * 對密文進行解密 * @param string $encrypted 需要解密的密文 * @return string 解密得到的明文 */ public function decrypt($encrypted, $appid) { try { //使用BASE64對需要解密的字符串進行解碼 $ciphertext_dec = base64_decode($encrypted); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); mcrypt_generic_init($module, $this->key, $iv); //解密 $decrypted = mdecrypt_generic($module, $ciphertext_dec); mcrypt_generic_deinit($module); mcrypt_module_close($module); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); } try { //去除補位字符 $pkc_encoder = new PKCS7Encoder; $result = $pkc_encoder->decode($decrypted); //去除16位隨機字符串,網絡字節序和AppId if (strlen($result) < 16) return ""; $content = substr($result, 16, strlen($result)); $len_list = unpack("N", substr($content, 0, 4)); $xml_len = $len_list[1]; $xml_content = substr($content, 4, $xml_len); $from_appid = substr($content, $xml_len + 4); } catch (Exception $e) { //print $e; return array(ErrorCode::$IllegalBuffer, null); } if ($from_appid != $appid) return array(ErrorCode::$ValidateAppidError, null); return array(0, $xml_content); }
這個里面的mcrypt_get_block_size這個方法過期了,重新在網上找了個,把下面的替換掉能用
/** * 對明文進行加密 * @param string $text 需要加密的明文 * @return string 加密后的密文 */ public function encrypt($text, $appid) { try { //獲得16位隨機字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $appid; $iv = substr($this->key, 0, 16); $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv); return array(ErrorCode::$OK, $encrypted); } catch (Exception $e) { //print $e; return array(ErrorCode::$EncryptAESError, null); } } /** * 對密文進行解密 * @param string $encrypted 需要解密的密文 * @return string 解密得到的明文 */ public function decrypt($encrypted, $appid) { try { $iv = substr($this->key, 0, 16); $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); } try { //去除補位字符 $pkc_encoder = new PKCS7Encoder; $result = $pkc_encoder->decode($decrypted); //去除16位隨機字符串,網絡字節序和AppId if (strlen($result) < 16) return ""; $content = substr($result, 16, strlen($result)); $len_list = unpack("N", substr($content, 0, 4)); $xml_len = $len_list[1]; $xml_content = substr($content, 4, $xml_len); $from_appid = substr($content, $xml_len + 4); if (!$appid) $appid = $from_appid; //如果傳入的appid是空的,則認為是訂閱號,使用數據中提取出來的appid } catch (Exception $e) { //print $e; return array(ErrorCode::$IllegalBuffer, null); } if ($from_appid != $appid) return array(ErrorCode::$ValidateAppidError, null); //不注釋上邊兩行,避免傳入appid是錯誤的情況 return array(0, $xml_content, $from_appid); //增加appid,為了解決后面加密回復消息的時候沒有appid的訂閱號會無法回復 }
就可以了,接着就開始用 微信提供的網頁工具開始測試 使用網頁調試工具調試該接口 https://mp.weixin.qq.com/debug 這個是地址
請求的結果都是sccuess了。
以為沒有問題了,結果是這樣。
然后就不知道所以然了,測試接口都測過了沒有問題,還報這個,然后給微信客服 提反饋,果然是沒有luan用的.居然這樣回復
真的就跪了,后面在google 里搜,發現也有哥們和我的情況一樣,然后 在 運維中心 把數據監控 開通,然后居然就可以了,然后就沒有然后了