php将把xml转换成数组,并打印文本文件查看。

最近使用支付接口的时候,经常需要测试回调的参数内容进行后续的处理

<?php
class study{
    
    
	/**
	 * 递归生成目录
	 */
	public function RecursiveMkdir($path){
    
    
		if (!file_exists($path)) {
    
    
	        $this->RecursiveMkdir(dirname($path));
	        @mkdir($path, 0777);
	    }
	}
	/**
	 * 把xml转换成数组
	 * @param  [type] $xml [description]
	 * @return [arr]      [description]
	 */
	private function  xmlToArray($xml){
    
    
	    //禁止引用外部xml实体
	    libxml_disable_entity_loader(true);
	    $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
	    $val = json_decode(json_encode($xmlstring),true);
	    return $val;
	}
	public function notiy(){
    
    
		//ROOT_PATH:根目录
		define('ROOT_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/");
		$data_path = ROOT_PATH.'/Data/wxpaylogs/'.date('Y-m-d')."/";
		$this->RecursiveMkdir($data_path);
		$file = $data_path.date('Y-m-d').'.txt';
		$handl = fopen($file,'a');
		$data   = "
		<note>
		<to>George</to>
		<from>John</from>
		<heading>Reminder</heading>
		<body>Don't forget the meeting!</body>
		</note>";
	
		$xmlarr = $this->xmlToArray($data);
		fwrite($handl,"Queryorder");
		fwrite($handl,"call back:" . json_encode($xmlarr));
		fwrite($handl,"开始查询:");
		fclose($handl);
	}
}
$res = new study;
$re = $res->notiy();
var_dump($re);

猜你喜欢

转载自blog.csdn.net/hgb24660/article/details/107833109
今日推荐