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


Posted in PHP onMarch 24, 2019

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

<?php
 
# 微信统一下单接口
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
 
 
$param = [
 'appid' => '公众号id',
 'mch_id' => '商户id',
 'nonce_str' =>uniqid(),
 'sign_type' => 'MD5',
 'body' => 'test',
 'detail' => 'test detail',
 'out_trade_no' => date('Ymd').rand(10000,99999),
 'total_fee' => 1,
 'notify_url' => 'http://www.test.top/testpay/pay.php',
 'trade_type' =>'JSAPI',
];
 
 
ksort( $param );
 
$sign_str = urldecode(http_build_query( $param ));
 
 
$sign_str .= '&key=商户密钥';
 
 
//echo $sign_str;exit;
 
$sign_str = md5( $sign_str );
 
 
$param['sign'] = strtoupper( $sign_str );
 
function CurlPost($url, $param = [], $is_post = 1, $timeout = 5 )
{
 
 //初始化curl
 $curl = curl_init();
 
 // 设置请求的路径
 curl_setopt($curl, CURLOPT_URL, $url);
 
 if ($is_post == 1) {
  //设置POST提交
  curl_setopt($curl, CURLOPT_POST, 0);
 }
 
 //显示输出结果 1代表 把接口返回的结果当作一个字符串处理
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
 // 设置请求超时时间
 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 
 
 if ($is_post == 1) {
  //提交数据 -- 设置post提交的数据
  if (is_array($param)) {
 
   //http_build_query
   curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
  } else {
   curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  }
 }
 
 //执行请求
 $data = $data_str = curl_exec($curl);
 //处理错误
 if ($error = curl_error($curl)) {
  $log_data = array(
   'url' => $url,
   'param' => $param,
   'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
  );
 
  var_dump($log_data);
  exit;
 }
 
 # 关闭CURL
 curl_close($curl);
 
 
 //json数据转换为数组
 $data = json_decode($data, true);
 
 if (!is_array($data)) {
  $data = $data_str;
 }
 
 #调用玩接口之后写一个日志
 $log = [
  'url' => $url,
  'param' => $param,
  'response' => $data_str
 ];
 file_put_contents(__DIR__ . '/wechat.log', print_r($log, true), 8);
 
 return $data;
 
}
function arr2Xml( $arr ){
 
 $xml = '<xml version="1.0" encoding="UTF-8"> ';
 foreach( $arr as $key => $value ){
  if (is_numeric($value)){
   $xml.="<".$key.">".$value."</".$key.">";
  }else{
   $xml.="<".$key."><![CDATA[".$value."]]></".$key.">";
  }
 
 }
 $xml .= '</xml>';
 return $xml;
}
 
$xml = arr2Xml( $param );
 
$result = CurlPost( $url , arr2Xml($param) );
 
 
$api_arr = json_decode( json_encode(simplexml_load_string( $result , 'SimpleXMLElement', LIBXML_NOCDATA) ), true );
 
if( $api_arr['return_code'] == 'SUCCESS' ){
 
 include __DIR__ . '/phpqrcode.php';
 header('content-type:image/png');
 echo Qrcode::png( $api_arr['code_url'] , false , 'H' , 6 ,2 );
}
<?php
 
#微信统一下单接口
$url='https://api.mch.weixin.qq.com/pay/unifiedorder';
 
$param=[
 #公众账号ID
 'appid'=>'****',
 'mch_id'=>'***',
 'nonce_str'=>uniqid(),
 'sign_type'=>'MD5',
 'body'=>'test',
 'detail'=>'detail',
 'out_trade_no'=>date('Ymd').rand(10000,99999),
 'total_fee'=>1,
 'spbill_create_ip'=>$_SERVER['SERVER_ADDR'],
 'notify_url'=>'http://****/test.php',
 'trade_type'=>'NATIVE',
];
ksort($param);
 
$sign_str=urldecode(http_build_query($param));
 
$sign_str.='&key=8934e7d15453e97507ef794cf7b0519d';
 
$sign_str=md5($sign_str);
 
$param['sign']=strtoupper($sign_str);
 
//print_r($param);exit;
function CurlPost($url, $param = [], $is_post = 1, $timeout = 5 )
{
 //初始化curl
 $curl = curl_init();
 
 // 设置请求的路径
 curl_setopt($curl, CURLOPT_URL, $url);
 
 if ($is_post == 1) {
  //设置POST提交
  curl_setopt($curl, CURLOPT_POST, 0);
 }
 
 //显示输出结果 1代表 把接口返回的结果当作一个字符串处理
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
 // 设置请求超时时间
 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 
 
 if ($is_post == 1) {
  //提交数据 -- 设置post提交的数据
  if (is_array($param)) {
 
   //http_build_query
   curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
  } else {
   curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  }
 }
 
 //执行请求
 $data = $data_str = curl_exec($curl);
 //处理错误
 if ($error = curl_error($curl)) {
  $log_data = array(
   'url' => $url,
   'param' => $param,
   'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
  );
 
  var_dump($log_data);
  exit;
 }
 
 # 关闭CURL
 curl_close($curl);
 
 
 //json数据转换为数组
 $data = json_decode($data, true);
 
 if (!is_array($data)) {
  $data = $data_str;
 }
 
 #调用玩接口之后写一个日志
 $log = [
  'url' => $url,
  'param' => $param,
  'response' => $data_str
 ];
 file_put_contents(__DIR__ . '/wechat.log', print_r($log, true), 8);
 
 return $data;
 
}
 
function arrzxml($arr){
 $xml='<xml version="1.0" encoding="UTF-8">';
 foreach($arr as $key=>$value){
  if(is_numeric($value)){
   $xml.="<".$key.">".$value."</".$key.">";
  }else{
   $xml.="<".$key."><![CDATA[".$value."]]></".$key.">";
  }
 
 }
 $xml.='</xml>';
// var_dump($xml);exit;
 return $xml;
 
}
 
$xml=arrzxml($param);
$result=CurlPost($url,arrzxml($param));
//echo '<pre/>';
var_dump($result);
exit;
$api_arr=json_decode(simplexml_load_string($result,'SimpleXMLElement',LIBXML_NOCDATA),true);
if($result['return_code']=='SUCCESS'){
 include __DIR__.'/phpqrcode.php';
 header('content-type:image/png');
 echo Qrcode::png($api_arr['code_url'],false,'H',6,2);
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
Ajax+PHP 边学边练之四 表单
Nov 27 PHP
php 文件上传实例代码
Apr 19 PHP
对象失去焦点时自己动提交数据的实现代码
Nov 06 PHP
如何使用PHP计算上一个月的今天
May 23 PHP
php计算几分钟前、几小时前、几天前的几个函数、类分享
Apr 09 PHP
正确的PHP匹配UTF-8中文的正则表达式
May 13 PHP
微信支付开发维权通知实例
Jul 12 PHP
详解cookie验证的php应用的一种SSO解决办法
Oct 20 PHP
php实现的rc4加密解密类定义与用法示例
Aug 16 PHP
PHP PDOStatement::debugDumpParams讲解
Jan 30 PHP
php项目中类的自动加载实例讲解
Sep 12 PHP
使用laravel的migrate创建数据表的方法
Sep 30 PHP
PHP实现支持CURL字符串证书传输的方法
Mar 23 #PHP
详解PHP变量传值赋值和引用赋值变量销毁
Mar 23 #PHP
laravel实现按时间日期进行分组统计方法示例
Mar 23 #PHP
PHP使用OB缓存实现静态化功能示例
Mar 23 #PHP
Laravel如何自定义command命令浅析
Mar 23 #PHP
PHP使用mysqli同时执行多条sql查询语句的实例
Mar 22 #PHP
PHP中命名空间的使用例子
Mar 22 #PHP
You might like
基于php在各种web服务器的运行模式详解
2013/06/03 PHP
PHP中数组定义的几种方法
2013/09/01 PHP
PHP中数组的分组排序实例
2014/06/01 PHP
php经典算法集锦
2015/11/14 PHP
PHP十六进制颜色随机生成器功能示例
2017/07/24 PHP
PHP+MySQL实现模糊查询员工信息功能示例
2018/06/01 PHP
thinkPHP框架动态配置用法实例分析
2018/06/14 PHP
项目实践之javascript技巧
2007/12/06 Javascript
JS 自动完成 AutoComplete(Ajax 查询)
2009/07/07 Javascript
JavaScript学习笔记(十七)js 优化
2010/02/04 Javascript
jQuery实现点击小图显示大图代码分享
2015/08/25 Javascript
js实现仿爱微网两级导航菜单效果代码
2015/08/31 Javascript
js弹出对话框方式小结
2015/11/17 Javascript
JavaScript Date对象详解
2016/03/01 Javascript
深入理解JavaScript中的块级作用域、私有变量与模块模式
2016/10/31 Javascript
video.js 一个页面同时播放多个视频的实例代码
2018/11/27 Javascript
JavaScript中数组去重的5种方法
2020/07/04 Javascript
Python3读取文件常用方法实例分析
2015/05/22 Python
django session完成状态保持的方法
2018/11/27 Python
python实现贪吃蛇小游戏
2020/03/21 Python
Pytorch中的自动求梯度机制和Variable类实例
2020/02/29 Python
jupyter notebook 添加kernel permission denied的操作
2020/04/21 Python
网页中的电话号码如何实现一键直呼效果_附示例
2016/03/15 HTML / CSS
使用PDF.JS插件在HTML中预览PDF文件的方法
2018/08/29 HTML / CSS
Tenstickers法国:墙贴和装饰贴纸
2019/08/26 全球购物
护理专业优质毕业生自荐书
2014/01/31 职场文书
法律六进活动方案
2014/03/13 职场文书
爱我中华教学反思
2014/04/28 职场文书
勤俭节约演讲稿
2014/05/08 职场文书
激励员工的口号
2014/06/16 职场文书
群众路线领导对照材料
2014/08/23 职场文书
2014年房产销售工作总结
2014/12/08 职场文书
党风廉政建设个人总结
2015/03/06 职场文书
2015年学习部工作总结范文
2015/03/31 职场文书
中学推普周活动总结
2015/05/07 职场文书
Pytorch中的数据集划分&正则化方法
2021/05/27 Python