博主信息
Top丶Rachel的博客
博文
1
粉丝
0
评论
0
访问量
1129
积分:1
P豆:18

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

2020年11月13日 08:45:15阅读数:1129博客 / Top丶Rachel的博客/ php学习
首先下载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. }

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • 首先说明一下,微信的主要逻辑集中在后端,前端只需携带所需的数据请求后端接口然后根据返回果做相应功失败处理即可。