美文网首页
php提交xml数据

php提交xml数据

作者: LauEl | 来源:发表于2019-05-07 14:41 被阅读0次

<?php
header("Content-type:text/xml;charset=utf-8");
date_default_timezone_set("PRC");
$data["service"] = 'pay.xdpay.qpay';
$data["version"] = '1.3';
$data["sign_type"] = 'MD5';
$data["mch_id"] = '708557244914661589';
$data["out_trade_no"] = date('YmdHis');
$data["body"] = 'chongzhi';
//$data['total_fee']=$_POST['money']*100;
$data['total_fee'] = '1000' * 100;
$data['mch_create_ip'] = '51.154.11.55';
$data['notify_url'] = 'http://www.ju57l.cn/pinganfu/server.php';
$data['nonce_str'] = '123456789';
$key = 'QEOOurNOi3emNSxWXTIaRasHLPOwA22z';//秘钥
$str_to_sign = prepareSign($data);
//var_dump($str_to_sign);die;
$sign = sign($str_to_sign, $key);
$data['sign'] = $sign;
//$result = buildForm($data, "url");
//echo $result;
echo '<?xml version="1.0" encoding="utf-8"?>';
//echo arr2xml($data);
$xmlData = "";
$xmlData = arr2xml($data);
//echo $xmlData;die;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,"url");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
curl_exec($ch);

/**
 *   将数组转换为xml
 * @param array $data 要转换的数组
 * @param bool $root 是否要根节点
 * @return string         xml字符串
 * @author Dragondean
 */
function arr2xml($data, $root = true)
{
    ksort($data);
    $str = "";
    if ($root) $str .= "<xml>";
    foreach ($data as $key => $val) {
        if (is_array($val)) {
            $child = arr2xml($val, false);
            $str .= "<$key>$child</$key>";
        } else {
            $str .= "<$key><![CDATA[$val]]></$key>";
        }
    }
    if ($root) $str .= "</xml>";
    return $str;
}

/**
 * 创建表单
 * @data        表单内容
 * @gateway 支付网关地址
 */
function buildForm($data, $gateway)
{
    $sHtml = "
<form id='payform' name='payform' action='" . $gateway . "' method='post'>";
    while (list ($key, $val) = each($data)) {
        $sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
    }
    $sHtml .= "
</form>";
    $sHtml .= "
<script>document.forms['payform'].submit();</script>";
    return $sHtml;
}

/**
 * @name    准备签名/验签字符串
 */
function prepareSign($data)
{
    ksort($data);
    $array = array();
    foreach ($data as $key => $value) {
        if ($value == null) {
            continue;
        }
        array_push($array, $key . '=' . $value);
    }
    return implode($array, '&');
}


/**
 * @name    生成签名
 * @param    sourceData
 * @return    签名数据
 */
function sign($data, $key)
{
    $signature = strtoupper(md5($data . '&key=' . $key));
    return $signature;
}

?>

相关文章

网友评论

      本文标题:php提交xml数据

      本文链接:https://www.haomeiwen.com/subject/xbbeoqtx.html