/** * 返回解析后的xml为数组格式 * @author caihaibin * @param $url 提交的url * @param $data 提交的xml本体 * @param $domtoxml 返回值是否做将xml转换为array * @param $checkxml 检查提交的xml格式 * @param $replace_empty 将xml转换成单行 * @throws Exception * @return mixed */ function postxml($url,$data,$domtoxml=true,$checkxml=false,$replace_empty=false){ //过滤换行 必要可以开启调试 if($replace_empty){ $data=preg_replace('/(?<=>)([^<]*?)(?=<[/])/sm', '', $data); $data=preg_replace('/(?<=>)([s]+?)(?=<[^/])/sm', '', $data); } //检查dom if($checkxml){ $res = @simplexml_load_string ( $data, NULL, LIBXML_NOCDATA ); if (! $res) { throw new Exception("提交xml异常!"); } } $header[] = "Content-type: application/xml"; //定义content-type为xml,注意是数组 $ch = curl_init ($url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $response = curl_exec($ch); if(curl_errno($ch)){ //print curl_error($ch); } curl_close($ch); if(!$domtoxml) return $response; $res = @simplexml_load_string ( $response, NULL, LIBXML_NOCDATA ); if (! $res) { throw new Exception("解析xml异常!"); } $res = json_decode ( json_encode ( $res ), true ); if (! $res) { throw new Exception("解析json异常!"); } return $res; }
转发请注明出处http://blog.martoo.cn
如有漏缺,请联系我 QQ 243008827