php微信掃碼支付 php公眾號(hào)支付
更新時(shí)間:2019年03月24日 15:17:46 作者:奇葩也是花
這篇文章主要為大家詳細(xì)介紹了php微信掃碼支付,php公眾號(hào)支付功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了php微信掃碼支付,公眾號(hào)支付的具體代碼,供大家參考,具體內(nèi)容如下
<?php
# 微信統(tǒng)一下單接口
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$param = [
'appid' => '公眾號(hào)id',
'mch_id' => '商戶(hù)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=商戶(hù)密鑰';
//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();
// 設(shè)置請(qǐng)求的路徑
curl_setopt($curl, CURLOPT_URL, $url);
if ($is_post == 1) {
//設(shè)置POST提交
curl_setopt($curl, CURLOPT_POST, 0);
}
//顯示輸出結(jié)果 1代表 把接口返回的結(jié)果當(dāng)作一個(gè)字符串處理
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 設(shè)置請(qǐng)求超時(shí)時(shí)間
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if ($is_post == 1) {
//提交數(shù)據(jù) -- 設(shè)置post提交的數(shù)據(jù)
if (is_array($param)) {
//http_build_query
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
} else {
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
}
}
//執(zhí)行請(qǐng)求
$data = $data_str = curl_exec($curl);
//處理錯(cuò)誤
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;
}
# 關(guān)閉CURL
curl_close($curl);
//json數(shù)據(jù)轉(zhuǎn)換為數(shù)組
$data = json_decode($data, true);
if (!is_array($data)) {
$data = $data_str;
}
#調(diào)用玩接口之后寫(xiě)一個(gè)日志
$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
#微信統(tǒng)一下單接口
$url='https://api.mch.weixin.qq.com/pay/unifiedorder';
$param=[
#公眾賬號(hào)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();
// 設(shè)置請(qǐng)求的路徑
curl_setopt($curl, CURLOPT_URL, $url);
if ($is_post == 1) {
//設(shè)置POST提交
curl_setopt($curl, CURLOPT_POST, 0);
}
//顯示輸出結(jié)果 1代表 把接口返回的結(jié)果當(dāng)作一個(gè)字符串處理
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 設(shè)置請(qǐng)求超時(shí)時(shí)間
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if ($is_post == 1) {
//提交數(shù)據(jù) -- 設(shè)置post提交的數(shù)據(jù)
if (is_array($param)) {
//http_build_query
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
} else {
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
}
}
//執(zhí)行請(qǐng)求
$data = $data_str = curl_exec($curl);
//處理錯(cuò)誤
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;
}
# 關(guān)閉CURL
curl_close($curl);
//json數(shù)據(jù)轉(zhuǎn)換為數(shù)組
$data = json_decode($data, true);
if (!is_array($data)) {
$data = $data_str;
}
#調(diào)用玩接口之后寫(xiě)一個(gè)日志
$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);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP?在?Microsoft?Windows?下的命令行方式
PHP?在?Microsoft?Windows?下的命令行方式,要在命令行下運(yùn)行?PHP,可以無(wú)需對(duì)?Windows?做任何改動(dòng),今天通過(guò)本文給大家介紹下PHP?在?Microsoft?Windows?下的命令行方式,感興趣的朋友一起看看吧2022-09-09
Laravel監(jiān)聽(tīng)數(shù)據(jù)庫(kù)訪問(wèn),打印SQL的例子
今天小編就為大家分享一篇Laravel監(jiān)聽(tīng)數(shù)據(jù)庫(kù)訪問(wèn),打印SQL的例子,有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Laravel 5框架學(xué)習(xí)之環(huán)境與配置
本文給大家主要介紹的是Laravel5框架中的環(huán)境配置,給大家詳細(xì)介紹了.env的配置文件,包含的數(shù)據(jù)庫(kù)配置信息的詳細(xì)解釋?zhuān)@里推薦給大家,有需要的小伙伴參考下。2015-04-04
ThinkPHP 3.2.3實(shí)現(xiàn)加減乘除圖片驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了ThinkPHP 3.2.3實(shí)現(xiàn)加減乘除圖片驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Yii2框架數(shù)據(jù)庫(kù)簡(jiǎn)單的增刪改查語(yǔ)法小結(jié)
這篇文章主要介紹了Yii2框架數(shù)據(jù)庫(kù)簡(jiǎn)單的增刪改查語(yǔ)法小結(jié),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08

