* XML Site Adder */
($site_url)
| 86 | * XML Site Adder |
| 87 | */ |
| 88 | function 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 | } |
no outgoing calls
no test coverage detected