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

PHP的SoapClient选择服务端口

高老师5年前 (2020-10-30)PHP1278

我目前正在与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 getenv函数和putenv函数的学习

PHP getenv函数和putenv函数的学习

(1).在PHP中可以查看的环境变量包括: (1.1).电脑环境变量 (2.1).服务器环境变量(2).getenv()函数获取一个环境变量的值.参数1是环境变量的key,参数2值为true的时候仅从你的电脑环境变量中查找,参数2值为false会从两种变量中全部查询//获取我电脑登录的用户名,输出A...

php redis事务

php redis事务

概念请参考w3school文章: redis watch ,redis exec (看完基本秒懂)(1)基本事务://连接本地的 Redis 服务 $redis = new Redis(); $redis->con...

php redis队列实现秒杀 , php用redis 实现秒杀功能

php redis队列实现秒杀 , php用redis 实现秒杀功能

为什么使用队列?因为pop取队列具有原子性。假如我们需要秒杀一个商品id,我们先将商品的库存保存到一个队列。例如:<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6...

php位运算符详解

php位运算符详解

文章篇幅较长,如果不喜欢看文章的,此处抛出韩顺丰老师的位运算视频,韩老师应该是全网讲php位运算符最详细的一个老师了。链接:https://pan.baidu.com/s/14xj7er8eVSUcJ-jYXyA0GA  提取码:731m 链接:https://pan.baidu.com...

php中0和字符串比较时注意的问题

php中0和字符串比较时注意的问题

在正式介绍前先抛出一段代码:<?php //输入的密码 $password = empty($_POST['password']) ? 0 : $_POST['password']; //设置的密码...

php event异常处理,php set_exception_handler无效

php event异常处理,php set_exception_handler无效

(1).今天遇到一件奇怪的事情,在event事件中是无法自定义异常处理,例如我们使用set_exception_handler来统一处理异常。例如下面的代码:<?php error_reporting(E_ALL); set_error_handler(function ($errn...