当前位置:首页 > PHP > 正文内容

php xml字符串转数组,phpxml转数组,php 将xml转换成数组

高老师9年前 (2018-01-27)PHP2163

001源码:

/*
 * $xml_str是xml字符串
 */
function  xmltoarray($xml_str)
{
	//禁止XML实体扩展攻击	
	libxml_disable_entity_loader(true);
	//拒绝包含HTML结构(避免出现html解析攻击);
	if (preg_match('/(\<\!DOCTYPE|\<\!ENTITY)/i', $xml_str)) {
		return false;
	}
	
	//返回的数组对象
	$result=array();
	
	//LIBXML_NOCDATA - 将 CDATA 设置为文本节点,微信支付的xml,可以自己定义
	$xmlobj=simplexml_load_string($xml_str, 'SimpleXMLElement',LIBXML_NOCDATA);
	
	//是否是SimpleXMLElement对象
	if($xmlobj instanceof  SimpleXMLElement)
	{
		$result=json_decode(json_encode($xmlobj),true);

	}

	return $result;

}


$xmlfile=file_get_contents('1.txt');

$xmlarray=xmltoarray($xmlfile);

002.原xml字符串:(微信支付通知的xml字符串)

<xml>
  <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
  <attach><![CDATA[支付测试]]></attach>
  <bank_type><![CDATA[CFT]]></bank_type>
  <fee_type><![CDATA[CNY]]></fee_type>
  <is_subscribe><![CDATA[Y]]></is_subscribe>
  <mch_id><![CDATA[10000100]]></mch_id>
  <nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
  <openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
  <out_trade_no><![CDATA[1409811653]]></out_trade_no>
  <result_code><![CDATA[SUCCESS]]></result_code>
  <return_code><![CDATA[SUCCESS]]></return_code>
  <sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
  <sub_mch_id><![CDATA[10000100]]></sub_mch_id>
  <time_end><![CDATA[20140903131540]]></time_end>
  <total_fee>1</total_fee>
<coupon_fee><![CDATA[10]]></coupon_fee>
<coupon_count><![CDATA[1]]></coupon_count>
<coupon_type><![CDATA[CASH]]></coupon_type>
<coupon_id><![CDATA[10000]]></coupon_id>
<coupon_fee><![CDATA[100]]></coupon_fee>
  <trade_type><![CDATA[JSAPI]]></trade_type>
  <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
</xml>

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/56.html

分享给朋友:

“ php xml字符串转数组,phpxml转数组,php 将xml转换成数组” 的相关文章

PHP Warning:  ftok(): Project identifier is invalid

PHP Warning: ftok(): Project identifier is invalid

在使用ftok生成ipc进程通信key尝试将第二个参数项目标识符传入字符串报错:PHP Warning:  ftok(): Project identifier is invalid,查阅资料发现第二个字符串只能是1个字符串,长度为1....

packagist包发布稳定版

packagist包发布稳定版

自己的composer已经发布到packagist,但是无法使用composer require easy-task/easy-task来安装,只能在配置文件使用如下方式安装:"require": {     "easy...

PHP最快方式模拟curl,PHP最快爬虫模拟方法

PHP最快方式模拟curl,PHP最快爬虫模拟方法

有时候我们需要爬一个接口,但是这个接口需要很多参数,包括header和cookie要去编写,使用php curl模拟实在太慢。我们可以通过浏览器的network来复制请求为curl命令。例如我需要模拟请求接口地址:https://www.xkmz.cc/Ajax/Debug/delly,我们只需要在...

PHP包含远程文件,PHP执行远程文件,PHP引入远程文件

PHP包含远程文件,PHP执行远程文件,PHP引入远程文件

(1).php.ini配置允许加载远程文件allow_url_fopen = On(2).创建本地文件invoke.php<?php require('https://blog.20230611.cn/test.txt');(3).创建远程文件https:/...

php redis队列

php redis队列

本篇文章是给新手学习php redis队列怎么玩的。我们模拟验证码发送,通过队列完成。(1).创建一个验证码发送接口/**  * 发送验证码  */ public function sendCaptcha() {    ...

 php连接sqlserver,php连接sql server数据库,php查询sqlserver数据库,php用sqlserver数据库

php连接sqlserver,php连接sql server数据库,php查询sqlserver数据库,php用sqlserver数据库

sqlsrv扩展是php操作SQL Server的驱动下载地址:http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx下载完成后打开解压到指定目录,找到对应php版本的扩展,记得下载nts版本的。下载完扩展后,然后复制指定的dll文件到对应ph...