function postXmlCurl($xml, $url, $useCert = false, $second = 30)
{
//初始化curl
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
curl_close($ch);
//返回结果
if($data)
{
echo $data;
return $data;
} else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch);
return false;
}
}function postXml()
{
$xml = $this->createXml();
$this->response = $this->postXmlCurl($xml,'https://api.mch.weixin.qq.com/pay/unifiedorder');
return $this->response;
}// 设置标配的请求参数,生成签名,生成接口参数xml
function createXml()
{
$this->parameters["appid"] = $this->payments['wxpay_appid'];//公众账号ID
$this->parameters["mch_id"] = $this->payments['wxpay_mchid'];//商户号
$this->parameters["nonce_str"] = $this->create_noncestr();//随机字符串
$this->parameters["sign"] = $this->getWxSign($this->parameters);//签名
$res = $this->arrayToXml($this->parameters);
echo $res;
return $res;
}
function xmlToArray($xml)
{
//将XML转为array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
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;
}发送到https://api.mch.weixin.qq.com/pay/unifiedorder的XML数据都正确,返回的结果是空的,也没有任何报错,请教大神问题出在哪里?
{
//初始化curl
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
curl_close($ch);
//返回结果
if($data)
{
echo $data;
return $data;
} else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch);
return false;
}
}function postXml()
{
$xml = $this->createXml();
$this->response = $this->postXmlCurl($xml,'https://api.mch.weixin.qq.com/pay/unifiedorder');
return $this->response;
}// 设置标配的请求参数,生成签名,生成接口参数xml
function createXml()
{
$this->parameters["appid"] = $this->payments['wxpay_appid'];//公众账号ID
$this->parameters["mch_id"] = $this->payments['wxpay_mchid'];//商户号
$this->parameters["nonce_str"] = $this->create_noncestr();//随机字符串
$this->parameters["sign"] = $this->getWxSign($this->parameters);//签名
$res = $this->arrayToXml($this->parameters);
echo $res;
return $res;
}
function xmlToArray($xml)
{
//将XML转为array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
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;
}发送到https://api.mch.weixin.qq.com/pay/unifiedorder的XML数据都正确,返回的结果是空的,也没有任何报错,请教大神问题出在哪里?
我这边排查了下,情况主要是DNS解析api.weixin.qq.com这个域名的时候找不到对应的服务器IP,造成的超时,你可以把这个域名,本地ping一下,换成IP地址写到微信支付的,
https://api.mch.weixin.qq.com/pay/unifiedorder改成如https://xxx.xxx.xxx.xxx/pay/unifiedorder,这样子的话如果curl的时候有返回值,说明就是DNS的问题。
解决办法:DNS设置为119.29.29.29
后来我发现是notify_url 的问题,我写的notify_url 是乱写的,后来我改成一个有能访问的接口微信就有正常的消息返回了,
虽然现在还是错误码 (订单已关闭)
2.另外一个需要注意的是 若果使用tailLog显示日志xml返回数据可能不显示