收藏
回答

这个同城配送的门店查询看文档是需要加密的,但没有给一个demo,加密文档也挺模糊的

https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/intracity_service.html

能不能发布一个 php的demo,提升大家开发效率,搜索了两天,也没有找到相关文档,尤其是 “GCM使用的认证数据

”这块,完全没理解

回答关注问题邀请回答
收藏

2 个回答

  • 李坤
    李坤
    2025-12-30

    官方能不能给一个PHP的签名demo

    developers.weixin.qq.com/miniprogram/dev/server/getting_started/api_signature.html

    找了很多资料,都签名失败,下面是千问生成的代码 private_key 是文件路径,可以打印出私钥

    /**
         * 生成 Wechatmp-Signature 签名
         *
         * @param string $urlpath   完整请求URL(不含query参数)
         * @param string $appid     小程序AppID
         * @param int    $timestamp Unix时间戳
         * @param string $postdata  原始POST数据(JSON字符串)
         * @param string $privateKeyPem 私钥内容(PEM格式,包含 -----BEGIN...)
         * @return string Base64编码的签名
         */
        public function generateWechatmpSignature($urlpath, $appid, $timestamp, $postdata)
        {
            $privateKeyPem = file_get_contents($this->config['private_key']);
    
    
            // 加载私钥
            $privateKey = openssl_pkey_get_private($privateKeyPem);
            // 2. 设置PSS选项
            // $options = [
            //     'salt_length' => 32,
            //     'mgf1_hash' => $mgfHash,
            // ];
            // $options = [
            //     'rsa_padding_mode' => 'pss',
            //     'rsa_pss_saltlen'  => 32,
            //     'digest_algo'      => 'sha256'
            // ];
            // 配置 PSS 签名参数:SHA256 + PSS + salt=32
            $algoConfig = [
                'rsa_padding_mode' => 'pss',
                'rsa_pss_saltlen'  => 32,
                'digest_algo'      => 'sha256'
            ];
            // 1. 构造待签名字符串
            $signStr = implode("\n", [$urlpath, $appid, $timestamp, $postdata]);
    
    
            if (!$privateKey) {
                throw new Exception('Invalid private key');
            }
            // 3. 生成签名
            $signature = '';
            $result = openssl_sign($signStr, $signature, $privateKey, $algoConfig);
            //'sha256WithRSAEncryption' OPENSSL_ALGO_SHA256 | OPENSSL_ALGO_PSS
    
    
            if (!$result) {
                throw new Exception('Signature failed: ' . openssl_error_string());
            }
            return base64_encode($signature);
        }
    
    2025-12-30
    有用
    回复 1
    • 李坤
      李坤
      01-11
      已解决
      01-11
      1
      回复
  • 智能回答 智能回答 本次回答由AI生成
    2025-12-30
    有用
登录 后发表内容