当前位置:首页 > PHP教程 > php高级应用 > 列表

php微信扫码支付 php公众号支付

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-14 11:24:39 浏览: 评论:0 

这篇文章主要为大家详细介绍了php微信扫码支付,php公众号支付功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

本文实例为大家分享了php微信扫码支付,公众号支付的具体代码,供大家参考,具体内容如下:

  1. <?php 
  2.    
  3. # 微信统一下单接口 
  4. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; 
  5.    
  6.    
  7. $param = [ 
  8.  'appid' => '公众号id', 
  9.  'mch_id' => '商户id', 
  10.  'nonce_str' =>uniqid(), 
  11.  'sign_type' => 'MD5', 
  12.  'body' => 'test', 
  13.  'detail' => 'test detail', 
  14.  'out_trade_no' => date('Ymd').rand(10000,99999), 
  15.  'total_fee' => 1, 
  16.  'notify_url' => 'http://www.test.top/testpay/pay.php', 
  17.  'trade_type' =>'JSAPI', 
  18. ]; 
  19.    
  20.    
  21. ksort( $param ); 
  22.    
  23. $sign_str = urldecode(http_build_query( $param )); 
  24.    
  25.    
  26. $sign_str .= '&key=商户密钥'; 
  27.    
  28.    
  29. //echo $sign_str;exit; 
  30.    
  31. $sign_str = md5( $sign_str ); 
  32.    
  33.    
  34. $param['sign'] = strtoupper( $sign_str ); 
  35.    
  36. function CurlPost($url, $param = [], $is_post = 1, $timeout = 5 ) 
  37. { 
  38.    
  39.  //初始化curl 
  40.  $curl = curl_init(); 
  41.    
  42.  // 设置请求的路径 
  43.  curl_setopt($curl, CURLOPT_URL, $url); 
  44.    
  45.  if ($is_post == 1) { 
  46.   //设置POST提交 
  47.   curl_setopt($curl, CURLOPT_POST, 0); 
  48.  } 
  49.    
  50.  //显示输出结果 1代表 把接口返回的结果当作一个字符串处理 
  51.  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  52.    
  53.  // 设置请求超时时间 
  54.  curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); 
  55.    
  56.  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
  57.  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  58.    
  59.    
  60.  if ($is_post == 1) { 
  61.   //提交数据 -- 设置post提交的数据 
  62.   if (is_array($param)) { 
  63.    
  64.    //http_build_query 
  65.    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param)); 
  66.   } else { 
  67.    curl_setopt($curl, CURLOPT_POSTFIELDS, $param); 
  68.   } 
  69.  } 
  70.    
  71.  //执行请求 
  72.  $data = $data_str = curl_exec($curl); 
  73.  //处理错误 
  74.  if ($error = curl_error($curl)) { 
  75.   $log_data = array( 
  76.    'url' => $url, 
  77.    'param' => $param, 
  78.    'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>', 
  79.   ); 
  80.    
  81.   var_dump($log_data); 
  82.   exit; 
  83.  } 
  84.    
  85.  # 关闭CURL 
  86.  curl_close($curl); 
  87.    
  88.    
  89.  //json数据转换为数组 
  90.  $data = json_decode($data, true); 
  91.    
  92.  if (!is_array($data)) { 
  93.   $data = $data_str; 
  94.  } 
  95.    
  96.  #调用玩接口之后写一个日志 
  97.  $log = [ 
  98.   'url' => $url, 
  99.   'param' => $param, 
  100.   'response' => $data_str 
  101.  ]; 
  102.  file_put_contents(__DIR__ . '/wechat.log', print_r($log, true), 8); 
  103.    
  104.  return $data; 
  105.    
  106. } 
  107. function arr2Xml( $arr ){ 
  108.    
  109.  $xml = '<xml version="1.0" encoding="UTF-8"> '; 
  110.  foreach( $arr as $key => $value ){ 
  111.   if (is_numeric($value)){ 
  112.    $xml.="<".$key.">".$value."</".$key.">"; 
  113.   }else{ 
  114.    $xml.="<".$key."><![CDATA[".$value."]]></".$key.">"; 
  115.   } 
  116.    
  117.  } 
  118.  $xml .= '</xml>'; 
  119.  return $xml; 
  120. } 
  121.    
  122. $xml = arr2Xml( $param ); 
  123.    
  124. $result = CurlPost( $url , arr2Xml($param) ); 
  125.    
  126.    
  127. $api_arr = json_decode( json_encode(simplexml_load_string( $result , 'SimpleXMLElement', LIBXML_NOCDATA) ), true ); 
  128.    
  129. if( $api_arr['return_code'] == 'SUCCESS' ){ 
  130.    
  131.  include __DIR__ . '/phpqrcode.php'; 
  132.  header('content-type:image/png'); 
  133.  echo Qrcode::png( $api_arr['code_url'] , false , 'H' , 6 ,2 ); 
  134. } 
  135.  
  136. <?php 
  137.    
  138. #微信统一下单接口 
  139. $url='https://api.mch.weixin.qq.com/pay/unifiedorder'; 
  140.    
  141. $param=[ 
  142.  #公众账号ID 
  143.  'appid'=>'****', 
  144.  'mch_id'=>'***', 
  145.  'nonce_str'=>uniqid(), 
  146.  'sign_type'=>'MD5', 
  147.  'body'=>'test', 
  148.  'detail'=>'detail', 
  149.  'out_trade_no'=>date('Ymd').rand(10000,99999), 
  150.  'total_fee'=>1, 
  151.  'spbill_create_ip'=>$_SERVER['SERVER_ADDR'], 
  152.  'notify_url'=>'http://****/test.php', 
  153.  'trade_type'=>'NATIVE', 
  154. ]; 
  155. ksort($param); 
  156.    
  157. $sign_str=urldecode(http_build_query($param)); 
  158.    
  159. $sign_str.='&key=8934e7d15453e97507ef794cf7b0519d'; 
  160.    
  161. $sign_str=md5($sign_str); 
  162.    
  163. $param['sign']=strtoupper($sign_str); 
  164.    
  165. //print_r($param);exit; 
  166. function CurlPost($url, $param = [], $is_post = 1, $timeout = 5 ) 
  167. { 
  168.  //初始化curl 
  169.  $curl = curl_init(); 
  170.    
  171.  // 设置请求的路径 
  172.  curl_setopt($curl, CURLOPT_URL, $url); 
  173.    
  174.  if ($is_post == 1) { 
  175.   //设置POST提交 
  176.   curl_setopt($curl, CURLOPT_POST, 0); 
  177.  } 
  178.    
  179.  //显示输出结果 1代表 把接口返回的结果当作一个字符串处理 
  180.  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  181.    
  182.  // 设置请求超时时间 
  183.  curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); 
  184.    
  185.  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
  186.  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  187.    
  188.    
  189.  if ($is_post == 1) { 
  190.   //提交数据 -- 设置post提交的数据 
  191.   if (is_array($param)) { 
  192.    
  193.    //http_build_query 
  194.    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param)); 
  195.   } else { 
  196.    curl_setopt($curl, CURLOPT_POSTFIELDS, $param); 
  197.   } 
  198.  } 
  199.    
  200.  //执行请求 
  201.  $data = $data_str = curl_exec($curl); 
  202.  //处理错误 
  203.  if ($error = curl_error($curl)) { 
  204.   $log_data = array( 
  205.    'url' => $url, 
  206.    'param' => $param, 
  207.    'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>', 
  208.   ); 
  209.    
  210.   var_dump($log_data); 
  211.   exit; 
  212.  } 
  213.    
  214.  # 关闭CURL 
  215.  curl_close($curl); 
  216.    
  217.    
  218.  //json数据转换为数组 
  219.  $data = json_decode($data, true); 
  220.    
  221.  if (!is_array($data)) { 
  222.   $data = $data_str; 
  223.  } 
  224.    
  225.  #调用玩接口之后写一个日志 
  226.  $log = [ 
  227.   'url' => $url, 
  228.   'param' => $param, 
  229.   'response' => $data_str 
  230.  ]; 
  231.  file_put_contents(__DIR__ . '/wechat.log', print_r($log, true), 8); 
  232.    
  233.  return $data; 
  234.    
  235. } 
  236.    
  237. function arrzxml($arr){ 
  238.  $xml='<xml version="1.0" encoding="UTF-8">'; 
  239.  foreach($arr as $key=>$value){ 
  240.   if(is_numeric($value)){ 
  241.    $xml.="<".$key.">".$value."</".$key.">"; 
  242.   }else{ 
  243.    $xml.="<".$key."><![CDATA[".$value."]]></".$key.">"; 
  244.   } 
  245.    
  246.  } 
  247.  $xml.='</xml>'; 
  248. // var_dump($xml);exit; 
  249.  return $xml; 
  250.    
  251. } 
  252.    
  253. $xml=arrzxml($param); 
  254. $result=CurlPost($url,arrzxml($param)); 
  255. //echo '<pre/>'; 
  256. var_dump($result); 
  257. exit; 
  258. $api_arr=json_decode(simplexml_load_string($result,'SimpleXMLElement',LIBXML_NOCDATA),true); 
  259. if($result['return_code']=='SUCCESS'){ 
  260.  include __DIR__.'/phpqrcode.php'; 
  261.  header('content-type:image/png'); 
  262.  echo Qrcode::png($api_arr['code_url'],false,'H',6,2); 
  263.    
  264. }

Tags: php微信扫码支付 php公众号支付

分享到: