当前位置:首页 > PHP > 正文内容

PHP的SoapClient选择服务端口

高老师6年前 (2020-10-30)PHP1371

我目前正在与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);
}

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/174.html

分享给朋友:

“PHP的SoapClient选择服务端口” 的相关文章

php引用变量的完全理解

php引用变量的完全理解

在PHP中,大部分变量类型,如字符串,整型,浮点,数组等都是值类型的,而类和对象是引用类型.和其他语言有点差距.(1).在值类型中我们直接使用&符号表示指向对应变量的内存地址,当前变量和被指向的变量只要有1个的值被修改都会直接影响另外一个变量的值发生变化。(ps:还是非常节省内存的,可以使用...

php arrayaccess的应用场景:配置管理器

php arrayaccess的应用场景:配置管理器

上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?PHP项目中大部分的框架都...

PHP获取站点根目录,PHP获取应用根目录,cgi和cli都支持

PHP获取站点根目录,PHP获取应用根目录,cgi和cli都支持

重构框架的时候想要考虑支持下cli模式,于是参考了thinkphp的底层。/**  * 获取应用根目录  * @return string  */ public static function getRootP...

php json_encode 使用注意

php json_encode 使用注意

参数中包含gb2312的字符串,返回结果是false或者null(不同PHP版本具有差异性)代码:<?php $dbms = 'mysql'; $host = '192.168.8.8'; $dbName =&n...

php finally使用

php finally使用

<?php /**  * @throws Exception  */ function curl() {     throw  new \Exception('err...

php代理下载,php代下载文件,php下载远程文件,php远程文件下载

php代理下载,php代下载文件,php下载远程文件,php远程文件下载

经常我们下载国外资源容易被墙,可以通过php脚本获取远程文件流然后输出给我们的浏览器来下载。<?php //设置下载文件的url $url = 'https://mirrors.huaweicloud.com/ubuntukylin/ubuntukylin-19....