php生成支付宝小程序码

本文介绍如何使用支付宝SDK生成小程序分享二维码,并提供了完整的PHP代码示例。包括设置应用ID、密钥及接口调用等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先下载官方SDK

支付宝官方SDK

代码实现
<?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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值