82 lines
2.6 KiB
PHP
82 lines
2.6 KiB
PHP
<?php
|
|
// 微信企业付款API请求数据
|
|
$data = [
|
|
'mch_appid' => 'your_app_id', // 商户账号appid
|
|
'mchid' => 'your_mch_id', // 商户号
|
|
'nonce_str' => uniqid(), // 随机字符串
|
|
'partner_trade_no' => 'your_trade_no', // 商户订单号
|
|
'openid' => 'recipient_openid', // 用户openid
|
|
'check_name' => 'NO_CHECK', // 校验用户姓名选项
|
|
'amount' => 100, // 付款金额,单位分
|
|
'desc' => '付款描述信息', // 企业付款描述信息
|
|
'spbill_create_ip' => $_SERVER['SERVER_ADDR'] ?? '127.0.0.1' // 调用接口的机器Ip地址
|
|
];
|
|
|
|
// 生成签名
|
|
$apiKey = 'your_api_key'; // API密钥
|
|
ksort($data);
|
|
$signString = urldecode(http_build_query($data)) . "&key=" . $apiKey;
|
|
$data['sign'] = strtoupper(md5($signString));
|
|
|
|
// 转换为XML格式
|
|
$xmlData = arrayToXml($data);
|
|
|
|
$ch = curl_init('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers');
|
|
if ($ch === false) {
|
|
die('cURL初始化失败');
|
|
}
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/xml',
|
|
'Content-Length: ' . strlen($xmlData)
|
|
]);
|
|
|
|
// SSL证书设置 - 使用更兼容的设置
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
|
|
// 检查CA证书文件是否存在,如果不存在则禁用验证(仅开发环境)
|
|
$caFile = 'D:\phpstudy_pro\Extensions\curl\cacert.pem';
|
|
if (file_exists($caFile)) {
|
|
curl_setopt($ch, CURLOPT_CAINFO, $caFile);
|
|
} else {
|
|
// 开发环境警告
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
error_log('警告: CA证书文件未找到,SSL验证已禁用');
|
|
}
|
|
|
|
// 如果有证书文件,需要设置以下两项
|
|
// curl_setopt($ch, CURLOPT_SSLCERT, '/path/to/your/apiclient_cert.pem');
|
|
// curl_setopt($ch, CURLOPT_SSLKEY, '/path/to/your/apiclient_key.pem');
|
|
|
|
$response = curl_exec($ch);
|
|
if ($response === false) {
|
|
echo 'cURL Error: ' . curl_error($ch) . "\n";
|
|
echo 'cURL Error Code: ' . curl_errno($ch) . "\n";
|
|
} else {
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
echo "HTTP状态码: " . $httpCode . "\n";
|
|
echo "响应内容:\n";
|
|
var_dump($response);
|
|
}
|
|
curl_close($ch);
|
|
|
|
/**
|
|
* 数组转XML
|
|
*/
|
|
function arrayToXml($arr) {
|
|
$xml = "<xml>";
|
|
foreach ($arr as $key => $val) {
|
|
if (is_numeric($val)) {
|
|
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
|
} else {
|
|
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
|
}
|
|
}
|
|
$xml .= "</xml>";
|
|
return $xml;
|
|
} |