php - 使用 SoapClient 在 PHP 中为 WebServices API 创建 WSSE Auth SOAP 调用

标签 php api soap-client wsse

<分区>

我有来自供应商 API 的以下内容: enter image description here

我需要使用 php 创建一个 Soap 调用。我该怎么做呢? 到目前为止我有这个:但它不起作用:

private function booking_soap_connect() {
        // the wsdl URL of your service to test 
        $serviceWsdl = 'https://apitest.com/bookingapi.asmx?WSDL';

        // the parmeters to initialize the client with 
        $serviceParams = array(
            'login' => 'un',
            'password' => 'pw'
        );

        // create the SOAP client 
        $client = new SoapClient($serviceWsdl, $serviceParams);

        $data['Brand'] = "All";
        $data['TourCode'] = "QBE";
        $data['DepartureCode'] = "8000321";
        $data['VendorId'] = "test";
        $data['VendorPassword'] = "test";

        $results = $client->GVI_DepartureInfo($data)
        echo $results;
    }

请帮忙

最佳答案

已解决:我终于通过以下代码解决了这个问题:

class WsseAuthHeader extends SoapHeader {

    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

    function __construct($user, $pass, $ns = null) {
        if ($ns) {
            $this->wss_ns = $ns;
        }

        $auth = new stdClass();
        $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);

        $username_token = new stdClass();
        $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        $security_sv = new SoapVar(
                new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns), SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }

}

$username = 'test';
$password = 'test123';
$wsdl = 'https://yoururl.com/api.asmx?WSDL';

$wsse_header = new WsseAuthHeader($username, $password);

$client = new SoapClient($wsdl, array(
    //"trace" => 1,
    //"exceptions" => 0
        )
);

$client->__setSoapHeaders(array($wsse_header));


$request = array(
    "functionResponseName" => array(
        'param1' => "string",
        "param2" => "string"
    )
);

$results = $client->FunctionName($request);

var_dump($results);

关于php - 使用 SoapClient 在 PHP 中为 WebServices API 创建 WSSE Auth SOAP 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361180/

相关文章:

Python:QuickBooks API 集成

java - CXF 客户端安全

java - SOAP 错误 : Axis 2 AbstractMethodError

php - 错误 ODBC MS SQL

javascript - 当只请求一个时,API 响应会给出两个响应

php - 使用 DateInterval 将星期添加到 DateTime 对象

api - 在 REST API 中使用 LINK 和 UNLINK HTTP 动词

node.js - 是否可以从 NodeJS 访问 Microsoft Dynamics NAV Web 服务?

javascript - Laravel 无法使用模态和 ajax 的增量方法添加数量

php - 如何避免 SQL 1. SELECT、2. <change value>、3. UPDATE 出现竞争条件?