|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Fetch URL with fopen() && returns a well formed array like the structure of the xml-document. |
|
5
|
|
|
* If somethins goes wrong return an empty array. |
|
6
|
|
|
* @param string $url |
|
7
|
|
|
* @param int $get_attributes |
|
8
|
|
|
* @param string $priority |
|
9
|
|
|
* @return array |
|
10
|
|
|
*/ |
|
11
|
|
|
function xmlUrl2array(string $url, int $get_attributes = 1, string $priority = 'tag') : array |
|
12
|
|
|
{ |
|
13
|
|
|
$contents = ""; |
|
14
|
|
|
if (!function_exists('xml_parser_create')) { |
|
15
|
|
|
return array(); |
|
16
|
|
|
} |
|
17
|
|
|
if (!($fp = @ fopen($url, 'rb'))) { |
|
18
|
|
|
return array(); |
|
19
|
|
|
} |
|
20
|
|
|
while (!feof($fp)) { |
|
21
|
|
|
$contents .= fread($fp, 8192); |
|
22
|
|
|
} |
|
23
|
|
|
fclose($fp); |
|
24
|
|
|
|
|
25
|
|
|
if (!$contents) { |
|
26
|
|
|
return array(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return xmlUrl2array($contents, $get_attributes, $priority); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns a well formed array like the structure of the xml-document |
|
34
|
|
|
* If somethins goes wrong return an empty array. |
|
35
|
|
|
* @param string $xml |
|
36
|
|
|
* @param int $get_attributes |
|
37
|
|
|
* @param string $priority |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
function xml2array(string $xml, int $get_attributes = 1, string $priority = 'tag') : array |
|
41
|
|
|
{ |
|
42
|
|
|
if (!function_exists('xml_parser_create')) { |
|
43
|
|
|
return array(); |
|
44
|
|
|
} |
|
45
|
|
|
if (!$xml) { |
|
46
|
|
|
return array(); |
|
47
|
|
|
} |
|
48
|
|
|
$contents = $xml; |
|
49
|
|
|
$parser = xml_parser_create(''); |
|
50
|
|
|
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); |
|
51
|
|
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
|
52
|
|
|
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); |
|
53
|
|
|
xml_parse_into_struct($parser, trim($contents), $xml_values); |
|
54
|
|
|
xml_parser_free($parser); |
|
55
|
|
|
if (!$xml_values) { |
|
|
|
|
|
|
56
|
|
|
return array(); |
|
57
|
|
|
} //Hmm... |
|
58
|
|
|
$xml_array = array(); |
|
59
|
|
|
$parents = array(); |
|
|
|
|
|
|
60
|
|
|
$opened_tags = array(); |
|
|
|
|
|
|
61
|
|
|
$arr = array(); |
|
|
|
|
|
|
62
|
|
|
$current = &$xml_array; |
|
63
|
|
|
$repeated_tag_index = array(); |
|
64
|
|
|
foreach ($xml_values as $data) { |
|
65
|
|
|
unset ($attributes, $value); |
|
66
|
|
|
extract($data); |
|
67
|
|
|
$result = array(); |
|
68
|
|
|
$attributes_data = array(); |
|
69
|
|
|
if (isset ($value)) { |
|
70
|
|
|
if ($priority == 'tag') { |
|
71
|
|
|
$result = $value; |
|
72
|
|
|
} else { |
|
73
|
|
|
$result['value'] = $value; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
if (isset ($attributes) && $get_attributes) { |
|
77
|
|
|
foreach ($attributes as $attr => $val) { |
|
78
|
|
|
if ($priority == 'tag') { |
|
79
|
|
|
$attributes_data[$attr] = $val; |
|
80
|
|
|
} else { |
|
81
|
|
|
$result['attr'][$attr] = $val; |
|
82
|
|
|
} //Set all the attributes in a array called 'attr' |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
if ($type == "open") { |
|
86
|
|
|
$parent[$level - 1] = &$current; |
|
87
|
|
|
if (!is_array($current) or (!in_array($tag, array_keys($current)))) { |
|
|
|
|
|
|
88
|
|
|
$current[$tag] = $result; |
|
89
|
|
|
if ($attributes_data) { |
|
|
|
|
|
|
90
|
|
|
$current[$tag . '_attr'] = $attributes_data; |
|
91
|
|
|
} |
|
92
|
|
|
$repeated_tag_index[$tag . '_' . $level] = 1; |
|
93
|
|
|
$current = &$current[$tag]; |
|
94
|
|
|
} else { |
|
95
|
|
|
if (isset ($current[$tag][0])) { |
|
96
|
|
|
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; |
|
97
|
|
|
$repeated_tag_index[$tag . '_' . $level]++; |
|
98
|
|
|
} else { |
|
99
|
|
|
$current[$tag] = array( |
|
100
|
|
|
$current[$tag], |
|
101
|
|
|
$result |
|
102
|
|
|
); |
|
103
|
|
|
$repeated_tag_index[$tag . '_' . $level] = 2; |
|
104
|
|
View Code Duplication |
if (isset ($current[$tag . '_attr'])) { |
|
|
|
|
|
|
105
|
|
|
$current[$tag]['0_attr'] = $current[$tag . '_attr']; |
|
106
|
|
|
unset ($current[$tag . '_attr']); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; |
|
110
|
|
|
$current = &$current[$tag][$last_item_index]; |
|
111
|
|
|
} |
|
112
|
|
|
} elseif ($type == "complete") { |
|
113
|
|
|
if (!isset ($current[$tag])) { |
|
114
|
|
|
$current[$tag] = $result; |
|
115
|
|
|
$repeated_tag_index[$tag . '_' . $level] = 1; |
|
116
|
|
|
if ($priority == 'tag' && $attributes_data) { |
|
|
|
|
|
|
117
|
|
|
$current[$tag . '_attr'] = $attributes_data; |
|
118
|
|
|
} |
|
119
|
|
|
} else { |
|
120
|
|
|
if (is_array($current[$tag]) && isset ($current[$tag][0])) { |
|
121
|
|
|
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; |
|
122
|
|
|
if ($priority == 'tag' && $get_attributes && $attributes_data) { |
|
|
|
|
|
|
123
|
|
|
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; |
|
124
|
|
|
} |
|
125
|
|
|
$repeated_tag_index[$tag . '_' . $level]++; |
|
126
|
|
|
} else { |
|
127
|
|
|
$current[$tag] = array( |
|
128
|
|
|
$current[$tag], |
|
129
|
|
|
$result |
|
130
|
|
|
); |
|
131
|
|
|
$repeated_tag_index[$tag . '_' . $level] = 1; |
|
132
|
|
|
if ($priority == 'tag' && $get_attributes) { |
|
133
|
|
View Code Duplication |
if (isset ($current[$tag . '_attr'])) { |
|
|
|
|
|
|
134
|
|
|
$current[$tag]['0_attr'] = $current[$tag . '_attr']; |
|
135
|
|
|
unset ($current[$tag . '_attr']); |
|
136
|
|
|
} |
|
137
|
|
|
if ($attributes_data) { |
|
|
|
|
|
|
138
|
|
|
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
$repeated_tag_index[$tag . '_' . $level]++; //0 && 1 index is already taken |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} elseif ($type == 'close') { |
|
145
|
|
|
$current = &$parent[$level - 1]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
return $xml_array; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param array $arrData |
|
153
|
|
|
* @param string $rootXml |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
function array2xml(array $arrData, string $rootXml = '<root></root>') : string |
|
157
|
|
|
{ |
|
158
|
|
|
// creating object of SimpleXMLElement |
|
159
|
|
|
$objXml = new SimpleXMLElement('<?xml version="1.0"?>' . ($rootXml ? $rootXml : '<root></root>')); |
|
160
|
|
|
array2SimpleXMLElement($arrData, $objXml); |
|
161
|
|
|
return trim($objXml->asXML()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param array $arrData |
|
166
|
|
|
* @param SimpleXMLElement $objXml |
|
167
|
|
|
*/ |
|
168
|
|
|
function array2SimpleXMLElement(array $arrData, SimpleXMLElement $objXml) |
|
169
|
|
|
{ |
|
170
|
|
|
foreach ($arrData as $key => $value) { |
|
171
|
|
|
if (is_array($value)) { |
|
172
|
|
|
if (!is_numeric($key)) { |
|
173
|
|
|
$subnode = $objXml->addChild("$key"); |
|
174
|
|
|
array2SimpleXMLElement($value, $subnode); |
|
175
|
|
|
} else { |
|
176
|
|
|
array2SimpleXMLElement($value, $objXml); |
|
177
|
|
|
} |
|
178
|
|
|
} else { |
|
179
|
|
|
$objXml->addChild("$key", "$value"); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.