MCPcopy Create free account
hub / github.com/EqualifyEverything/equalify / xml_site_adder

Function xml_site_adder

models/adders.php:88–128  ·  view source on GitHub ↗

* XML Site Adder */

($site_url)

Source from the content-addressed store, hash-verified

86 * XML Site Adder
87 */
88function xml_site_adder($site_url){
89
90 // Instantiate Guzzle client to accept XML
91 $options = [
92 'headers' => ['Accept' => 'application/xml'],
93 'verify' => false,
94 ];
95 $client = new Client($options);
96
97 // Valid XML files are only allowed!
98 $xml_contents = $client->get($site_url)->getBody();
99 if(!str_starts_with($xml_contents, '<?xml')) {
100 throw new Exception("$site_url did not return XML");
101 }
102
103 // Convert XML to JSON, so we can use it later
104 $xml = simplexml_load_string($xml_contents);
105 $json = json_encode($xml);
106 $json_entries = json_decode($json, true);
107
108 // Push JSON to pages array.
109 $pages = [];
110 if(array_key_exists('url', $json_entries)){
111
112 // This gets around a weird bug where json_decode
113 // doesn't wrap single entry XMLs into an array.
114 if(!empty($json_entries['url']['loc'])){
115 $json_entries['url'] = [$json_entries['url']];
116 }
117
118 // Now we can prepare our output
119 foreach ($json_entries['url'] as $page):
120 array_push($pages, $page['loc']);
121 endforeach;
122
123 }
124
125 // Prepare contents and return them.
126 return $pages;
127
128}

Callers 2

add_site.phpFile · 0.85
process_sitesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected