gpt4 book ai didi

php - 在 PHP 中不使用 WSDL 与 soap API 交互

转载 作者:行者123 更新时间:2023-12-05 01:44:03 25 4
gpt4 key购买 nike

最近访问了酒店管理服务的 soap API。他们提供了显示请求基本示例的文档:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">    
<soapenv:Header>
<Auth xmlns="http://xxxx/xxxxAPI">
<FromSystemId ID="1">CompanyName</FromSystemId>
<UserName>username</UserName>
<Password>password</Password>
</Auth>
</soapenv:Header>
<soapenv:Body>
<GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en"
xmlns="http://xxxx/xxxxAPI">
<Country Code="GB" />
</GetRegions>
</soapenv:Body>
</soapenv:Envelope>

他们还在其文档中提供了函数列表以及每个函数所需的参数。但是我对如何执行请求有点困惑,因为我以前从未使用过 soap API。他们也没有提供 WSDL,这有关系吗?

无论如何,这就是我想尝试执行请求的方式

$soapURL = "http://xxxx/xxxxAPI" ;
$soapParameters = Array('login' => "username", 'password' => "password") ;
$soapFunction = "getRegions";
$soapFunctionParameters = Array('countrycode' => 'GB');

$soapClient = new SoapClient($soapURL, $soapParameters);

$soapResult = $soapClient->__soapCall($soapFunction,
$soapFunctionParameters) ;

if(is_array($soapResult) && isset($soapResult['someFunctionResult'])) {
// Process result.
} else {
// Unexpected result
if(function_exists("debug_message")) {
debug_message("Unexpected soapResult for {$soapFunction}: ".print_r($soapResult, TRUE)) ;
}
}

我的做法是否正确?我现在无法测试这个,因为我还没有收到我的身份验证,但想现在就开始。

任何帮助都会很棒。

最佳答案

这是一个小例子。

$opts = array(
'location' => 'http://xxxx/xxxxAPI',
'uri' => 'urn:http://test-uri/'
);

$client = new SOAPClient(null, $opts);

$headerData = array(
'FromSystemId' => 'CompanyName',
'UserName' => 'username',
'Password' => 'password',
);

// Create Soap Header.
$header = new SOAPHeader('http://xxxx/xxxxAPI', 'Auth', $headerData);

// Set the Headers of Soap Client.
$client->__setSoapHeaders($header);


$result = $client->__soapCall('getRegions', array('GB'));

// $return = $client->__soapCall('getRegions', array(new SoapParam(new SoapVar('GB', XSD_STRING), 'countryCode')));

var_dump($result);

They also haven't provided a WSDL, does this matter?

为了能够添加 HEADER 属性,它们必须在 WSDL 中提及。如果它们不存在于 WSDL 中,它们将不会显示为属性,而是显示为 <item><key/><value/></item>元素。

提示:如果您知道请求的方式并且没有 WSDL,请尝试手动生成 HTTP header 和 XML 正文并使用 CURL 或 Guzzle 执行请求。

Guzzle 示例:

$soapContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">    
<soapenv:Header>
<Auth xmlns="http://xxxx/xxxxAPI">
<FromSystemId ID="1">CompanyName</FromSystemId>
<UserName>username</UserName>
<Password>password</Password>
</Auth>
</soapenv:Header>
<soapenv:Body>
<GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en"
xmlns="http://xxxx/xxxxAPI">
<Country Code="GB" />
</GetRegions>
</soapenv:Body>
</soapenv:Envelope>';

$client = new GuzzleHttp\Client([
'headers' => [ 'SOAPAction' => '"urn:http://xxxx/xxxxAPI/#getRegions"' ]
]);

$response = $client->post('http://xxxx/xxxxAPI',
['body' => $soapContent]
);

echo $response;

关于php - 在 PHP 中不使用 WSDL 与 soap API 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com