Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Rican7/2140f8590fcebe72a17e to your computer and use it in GitHub Desktop.
Save Rican7/2140f8590fcebe72a17e to your computer and use it in GitHub Desktop.
Parsing an EWS (Exchange Web Services) WSDL to determine the supported input headers per service operation in PHP
<?php
// The location of the EWS WSDL here (Office365 here for example)
const WSDL_URL = 'https://outlook.office365.com/EWS/Services.wsdl';
const NAMESPACE_WSDL_PREFIX = 'wsdl';
const NAMESPACE_WSDL_URI = 'http://schemas.xmlsoap.org/wsdl/';
const NAMESPACE_SOAP_PREFIX = 'soap';
const NAMESPACE_SOAP_URI = 'http://schemas.xmlsoap.org/wsdl/soap/';
const XML_OPERATION_XPATH = '/wsdl:definitions/wsdl:binding/wsdl:operation';
const XML_OPERATION_INPUT_HEADER_XPATH = 'wsdl:input/soap:header';
const OPERATION_NAME_ATTRIBUTE = 'name';
const HEADER_PART_ATTRIBUTE = 'part';
// Load the remote XML document
$xml = new SimpleXmlElement(WSDL_URL, 0, true);
// Register some element namespaces for stability in XPath queries
$xml->registerXPathNamespace(NAMESPACE_WSDL_PREFIX, NAMESPACE_WSDL_URI);
$xml->registerXPathNamespace(NAMESPACE_SOAP_PREFIX, NAMESPACE_SOAP_URI);
// Build the input header map
$operation_input_header_map = array_reduce(
$xml->xpath(XML_OPERATION_XPATH), // Query for the WSDL binding operations
function ($map, SimpleXmlElement $element) {
if (!isset($element[OPERATION_NAME_ATTRIBUTE])) {
return $map;
}
// Grab the name of each input header for the operation
$map[(string) $element[OPERATION_NAME_ATTRIBUTE]] = array_map(
function (SimpleXmlElement $element) {
if (!isset($element[HEADER_PART_ATTRIBUTE])) {
return null;
}
return (string) $element[HEADER_PART_ATTRIBUTE];
},
$element->xpath(XML_OPERATION_INPUT_HEADER_XPATH) // Query for the input headers
);
return $map;
},
[]
);
var_dump($operation_input_header_map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment