如题,有没遇到这种情况的?
$trade_type = "JSAPI";
$signA = array("appid"=>$appid,"mch_id"=>$mch_id,"nonce_str"=>$nonce_str,
"body"=>$body,"out_trade_no"=>$out_trade_no,"total_fee"=>$total_fee,
"spbill_create_ip"=>$spbill_create_ip,"notify_url"=>$notify_url,
"trade_type"=>$trade_type,"openid"=>$openid,"device_info"=>$device_info);
$sign = GetPrePayId_Sign_ASCII_ASC_Encode($signA,$mykey="",$isencode=false);
$data = '<xml>';
$data .= Construct_XmlTagStr("appid",$appid);
$data .= Construct_XmlTagStr("mch_id",$mch_id);
$data .= Construct_XmlTagStr("nonce_str",$nonce_str);
$data .= Construct_XmlTagStr("body",$body);
$data .= Construct_XmlTagStr("out_trade_no",$out_trade_no);
$data .= Construct_XmlTagStr("total_fee",$total_fee);
$data .= Construct_XmlTagStr("spbill_create_ip",$spbill_create_ip);
$data .= Construct_XmlTagStr("notify_url",$notify_url);
$data .= Construct_XmlTagStr("trade_type",$trade_type);
$data .= Construct_XmlTagStr("openid",$openid);
$data .= Construct_XmlTagStr("device_info",$device_info);
$data .= Construct_XmlTagStr("sign",$sign);
$data .= "</xml>";
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$res = WxPayCurlPost($url,$data);
function WxPayCurlPost($url,$xml_data,$useCert=false){//post推送
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if($useCert){
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
//证书文件请放入服务器的非web目录下
$sslCertPath = "";
$sslKeyPath = "";
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $sslCertPath);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $sslKeyPath);
}
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $xml_data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
// curl_setopt ( $ch, CURLOPT_TIMEOUT, true );
$res = curl_exec($ch);
// var_dump($res);
curl_close($ch);
return $res;
}
function GetPrePayId_Sign_ASCII_ASC_Encode($paramArray,$mykey, $isencode=false){
$paramStr = '';
ksort($paramArray);
$i = 0;
foreach ($paramArray as $iKey => $value)
{
if ($iKey == 'Signature'){
continue;
}
if ($i == 0){
$paramStr .= '';
}else{
$paramStr .= '&';
}
$paramStr .= $iKey . '=' . ($isencode?urlencode($value):$value);
++$i;
}
$stringSignTemp=$paramStr."&key=".$mykey;
$sign=strtoupper(md5($stringSignTemp));
return $sign;
}
还是自己来回来吧,这里应该把curl回来的$res 直接调用 XML解析后才能得到返回码和信息,而不是直接返回结果:
function JieXi_XmlRes($return_xml){//解释返回的XML数据
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
//先把xml转换为simplexml对象,再把simplexml对象转换成 json,再将 json 转换成数组。
$value_array = json_decode(json_encode(simplexml_load_string($return_xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
var_dump($value_array);exit;
$return_xml = $value_array['prepay_id'];
return $return_xml;
}