我目前正在与SOAP服务集成,该服务定义了两个不同的服务。
WSDL的相关部分是:
<wsdl:service name="Config"> <wsdl:port name="BasicHttpBinding_IConfiguration" binding="tns:BasicHttpBinding_IConfiguration"> <soap:address location="http://nonsecure.example.com/Configuration.svc"/> </wsdl:port> <wsdl:port name="BasicHttpsBinding_IConfiguration" binding="tns:BasicHttpsBinding_IConfiguration"> <soap:address location="https://secure.example.com/Configuration.svc"/> </wsdl:port> </wsdl:service>
通过研究,我发现可以使用__setLocation()方法来控制
$client->__setLocation('https://secure.example.com/Configuration.svc');
但是,我不应该直接写死它,应该根据端口优先选择https。
于是封装一个方法来优先获取https端口
function getLocationForPort($wsdl, $portName) { $file = file_get_contents($wsdl); $xml = new SimpleXmlElement($file); $query = "wsdl:service/wsdl:port[@name='$portName']/soap:address"; $address = $xml->xpath($query); if (!empty($address)) { $location = (string)$address[0]['location']; return $location; } return false; }
用法很简单:
$client = new SoapClient($wsdl); $sslLocation = getLocationForPort($wsdl, 'BasicHttpsBinding_IConfiguration'); if ($sslLocation) { $client->__setLocation($location); }