洛丽糖(luolt.cn),致力于互联网资源的共享, 分享各类技术教程,typecho主题模板,zblog主题模板,网站源码等各种资源。
首页 » 收集 » 正文内容
php结合支付宝小程序SDK生成支付宝小程序码
寻梦xunm| 641| 收集
3年前
超过1266天 温馨提示
本文最后更新于2021年06月06日,已超过1266天没有更新,若内容或图片失效,请留言反馈。

首先下载SDK
支付宝官方SDK

php实现代码

<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/11/9 0009
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
namespace app\common\service;
use think\Db;
use think\Exception;
class CreateAlipayQrcodeService {
    const  APPID = "20210021"; //appid
    const  PRIVATEKEY = 'MII='; //私钥
    const  PUBLICKEY = 'AQAB'; //公钥
    /**
     * @param $queryParam 参数
     * @param string $page 启动页
     * @param string $describe 描述
     * @param int $site_id 
     * @param bool $domain
     * @return \SimpleXMLElement|string
     * @throws Exception
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:支付宝小程序生成分享二维码
     */
    public static function createQrcode($queryParam, $page = "pages/tabs/home", $describe = "默认站点",$site_id=0,$domain=false) {
       //根据你sdk存放的地址引入文件
        include EXTEND_PATH . "alipay/AopClient.php";
        include EXTEND_PATH . "alipay/request/AlipayOpenAppQrcodeCreateRequest.php";
        try {
            $aop = new \AopClient ();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
            $aop->appId = self::APPID;
            $rsaPrivateKey = self::PRIVATEKEY;
            $aop->rsaPrivateKey = trim($rsaPrivateKey);
            $alipayrsaPublicKey = self::PUBLICKEY;
            $aop->alipayrsaPublicKey = trim($alipayrsaPublicKey);
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset = 'UTF-8';
            $aop->format = 'json';
            $request = new \AlipayOpenAppQrcodeCreateRequest ();
            $request->setBizContent("{" . "\"url_param\":\"" . $page . "\"," . "\"query_param\":\"" . $queryParam . "\"," . "\"describe\":\"" . $describe . "\"" . "  }");
            // 执行操作  请求对应的接口
            $result = $aop->execute($request);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if(!empty($resultCode)&&$resultCode == 10000){
                if ($domain){
                    //服务端使用,图片下载到本地
                    return self::create($result->$responseNode->qr_code_url,$site_id);
                }else{
                    //支付宝小程序端使用(直接返回生成的地址就行)
                    return $result->$responseNode->qr_code_url;
                }
            }else{
                throw  new Exception("错误代码:".$resultCode);
            }
        } catch (Exception $exception) {
            throw  new Exception($exception->getMessage());
        }
    }
    /**
     * @param $qr_code_url
     * @param $site_id
     * @return string
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:远程图片保存到本地
     */
    protected static function create($qr_code_url,$site_id){
        $d=date("Ymd",time());
        $save_dir=ROOT_PATH."public";
        $dk="/alipay/".$d."/";
        $save_dir.=$dk;
        $filename=$site_id.".jpg";
        if (!is_dir($save_dir)){
            @mkdir($save_dir,0777,true);
        }
        if (file_exists($save_dir.$filename)){
            return  $dk.$filename;
        }
        $url=$qr_code_url.".jpg";
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        ob_start ();
        curl_exec ( $ch );
        $return_content = ob_get_contents();
        ob_end_clean ();
        $fp=@fopen($save_dir.$filename,"a"); //将文件绑定到流
        fwrite($fp,$return_content); //写入文件
        fclose($fp);
        return $dk.$filename;
    }
}

文章来源于:http://www.lovegyl.cn/show/MzY=

none
0 赞 or 打赏
喜欢就打赏一点
微信 支付宝
20240430140454171445709417079.png
20240430140454171445709417079.png
隐私
Q Q:1340326824
邮箱:vipshiyi@qq.com
QQ群:422720328

我的音乐