Zbuduj kopertę SOAP i odczytaj odpowiedź MTOM
Każde wywołanie to koperta SOAP 1.2 z nagłówkami WS-Addressing: wsa:To z adresem usługi i wsa:Action z nazwą metody. Content-Type żądania to application/soap+xml.
Odpowiedź przychodzi jako application/xop+xml, czyli MTOM: koperta jest zawinięta w granice multipart. Wycinamy samą kopertę wyrażeniem regularnym i dopiero ją parsujemy.
<?phpdeclare(strict_types=1);const BIR_URL = 'https://wyszukiwarkaregontest.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc';const BIR_NS = 'http://CIS/BIR/PUBL/2014/07';function bir(string $action, string $body, ?string $sid = null): string{ $envelope = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"' . ' xmlns:ns="' . BIR_NS . '" xmlns:dat="' . BIR_NS . '/DataContract">' . '<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">' . '<wsa:To>' . BIR_URL . '</wsa:To>' . '<wsa:Action>' . BIR_NS . '/IUslugaBIRzewnPubl/' . $action . '</wsa:Action>' . '</soap:Header><soap:Body>' . $body . '</soap:Body></soap:Envelope>'; $headers = ['Content-Type: application/soap+xml; charset=utf-8']; if ($sid !== null) { $headers[] = 'sid: ' . $sid; } $curl = curl_init(BIR_URL); curl_setopt_array($curl, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $envelope, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, ]); $raw = (string) curl_exec($curl); curl_close($curl); // Odpowiedź to MTOM: koperta SOAP leży między granicami multipart. if (!preg_match('/<s:Envelope.*<\/s:Envelope>/s', $raw, $match)) { throw new RuntimeException('BIR nie zwrócił koperty SOAP'); } $xml = new SimpleXMLElement($match[0]); $xml->registerXPathNamespace('b', BIR_NS); $result = $xml->xpath('//b:' . $action . 'Result'); return $result ? (string) $result[0] : '';}