PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > php结合SDK生成支付宝小程序码

php结合SDK生成支付宝小程序码

Top丶Rachel的博客
Top丶Rachel的博客 原创
2020年11月13日 08:45:15 4342浏览
首先下载SDK

支付宝官方SDK

php实现代码
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Created by PHPstorm: [ JRK丶Admin ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | SiteUrl: http://www.luckyhhy.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: LuckyHhy <jackhhy520@qq.com>
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020/11/9 0009
  12. // +----------------------------------------------------------------------
  13. // | Description:
  14. // +----------------------------------------------------------------------
  15. namespace app\common\service;
  16. use think\Db;
  17. use think\Exception;
  18. class CreateAlipayQrcodeService {
  19. const APPID = "20210021"; //appid
  20. const PRIVATEKEY = 'MII='; //私钥
  21. const PUBLICKEY = 'AQAB'; //公钥
  22. /**
  23. * @param $queryParam 参数
  24. * @param string $page 启动页
  25. * @param string $describe 描述
  26. * @param int $site_id
  27. * @param bool $domain
  28. * @return \SimpleXMLElement|string
  29. * @throws Exception
  30. * @author: Hhy <jackhhy520@qq.com>
  31. * @describe:支付宝小程序生成分享二维码
  32. */
  33. public static function createQrcode($queryParam, $page = "pages/tabs/home", $describe = "默认站点",$site_id=0,$domain=false) {
  34. //根据你sdk存放的地址引入文件
  35. include EXTEND_PATH . "alipay/AopClient.php";
  36. include EXTEND_PATH . "alipay/request/AlipayOpenAppQrcodeCreateRequest.php";
  37. try {
  38. $aop = new \AopClient ();
  39. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  40. $aop->appId = self::APPID;
  41. $rsaPrivateKey = self::PRIVATEKEY;
  42. $aop->rsaPrivateKey = trim($rsaPrivateKey);
  43. $alipayrsaPublicKey = self::PUBLICKEY;
  44. $aop->alipayrsaPublicKey = trim($alipayrsaPublicKey);
  45. $aop->apiVersion = '1.0';
  46. $aop->signType = 'RSA2';
  47. $aop->postCharset = 'UTF-8';
  48. $aop->format = 'json';
  49. $request = new \AlipayOpenAppQrcodeCreateRequest ();
  50. $request->setBizContent("{" . "\"url_param\":\"" . $page . "\"," . "\"query_param\":\"" . $queryParam . "\"," . "\"describe\":\"" . $describe . "\"" . " }");
  51. // 执行操作 请求对应的接口
  52. $result = $aop->execute($request);
  53. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  54. $resultCode = $result->$responseNode->code;
  55. if(!empty($resultCode)&&$resultCode == 10000){
  56. if ($domain){
  57. //服务端使用,图片下载到本地
  58. return self::create($result->$responseNode->qr_code_url,$site_id);
  59. }else{
  60. //支付宝小程序端使用(直接返回生成的地址就行)
  61. return $result->$responseNode->qr_code_url;
  62. }
  63. }else{
  64. throw new Exception("错误代码:".$resultCode);
  65. }
  66. } catch (Exception $exception) {
  67. throw new Exception($exception->getMessage());
  68. }
  69. }
  70. /**
  71. * @param $qr_code_url
  72. * @param $site_id
  73. * @return string
  74. * @author: Hhy <jackhhy520@qq.com>
  75. * @describe:远程图片保存到本地
  76. */
  77. protected static function create($qr_code_url,$site_id){
  78. $d=date("Ymd",time());
  79. $save_dir=ROOT_PATH."public";
  80. $dk="/alipay/".$d."/";
  81. $save_dir.=$dk;
  82. $filename=$site_id.".jpg";
  83. if (!is_dir($save_dir)){
  84. @mkdir($save_dir,0777,true);
  85. }
  86. if (file_exists($save_dir.$filename)){
  87. return $dk.$filename;
  88. }
  89. $url=$qr_code_url.".jpg";
  90. $ch = curl_init ();
  91. curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
  92. curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
  93. curl_setopt ( $ch, CURLOPT_URL, $url );
  94. ob_start ();
  95. curl_exec ( $ch );
  96. $return_content = ob_get_contents();
  97. ob_end_clean ();
  98. $fp=@fopen($save_dir.$filename,"a"); //将文件绑定到流
  99. fwrite($fp,$return_content); //写入文件
  100. fclose($fp);
  101. return $dk.$filename;
  102. }
  103. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议