Soap 身份验证 header 并使用 PHP 请求

Soap authentication header and request using PHP

我正在尝试为在线餐厅 table 预订服务设置 SOAP 调用,但未能成功。 它使用身份验证 header 并尝试了以下代码但没有任何运气:

$username = '';
$password = '';

$soapURL = "http://m.cmd-it.dk/reservations.asmx?WSDL";

$client = new SoapClient($soapURL,array());

$auth = array(
    'UserName' => $username,
    'Password' => $password,
    'createReservation'=> array(
        'reservation2CompanyName' => 'Tester',
        'customerFirstName' => 'test',
        'customerLastName' => 'tester',
        'customerTelephoneNumber' => '22334455',
        'customerMail' => 'test@example.com',
        'reservationDate' => date("j/m/Y", time()),
        'reservationTime' => date("H:i", time()),
        'reservationPAX' => '3',
        'reservationRemarks' => 'test',
        'TestMode' => 1
    )
);
$header = new SoapHeader('http://cmd-it.dk/','authentification',$auth,false);
$client->__setSoapHeaders($header);

echo var_dump($client->__getLastRequestHeaders());
echo var_dump($client->__getLastRequest());

但它只是回显 NULL NULL... :-(

我不熟悉 PHP SOAP 调用并且根本不使用身份验证 headers,但希望有人能把我推向正确的方向。

您即将实现目标。在这里我修改了几行你的代码:

    <?php

    $username = '';
    $password = '';

    $soapURL = "http://m.cmd-it.dk/reservations.asmx?WSDL";

    $client = new SoapClient($soapURL,array());

    $auth = array(
        'UserName' => $username,
        'Password' => $password,
        'createReservation'=> array(
            'reservation2CompanyName' => 'Tester',
            'customerFirstName' => 'test',
            'customerLastName' => 'tester',
            'customerTelephoneNumber' => '22334455',
            'customerMail' => 'test@example.com',
            'reservationDate' => date("j/m/Y", time()),
            'reservationTime' => date("H:i", time()),
            'reservationPAX' => '3',
            'reservationRemarks' => 'test',
            'TestMode' => 1
        )
    );
    $header = new SoapHeader('http://cmd-it.dk/','authentification',$auth,false);
    $client->__setSoapHeaders($header);

    /* Requesting function list (interface) from SOAP server */
    echo "<pre>";
    var_dump($client->__getFunctions());

    /* Executing a fuction, for example isAlive method */
    $response = $client->__soapCall("isAlive", array("isAlive" => "true"));
    var_dump($response);



/* Here a list of functions available on your server that we have requested by getFucntions() method */
/*
array(4) {
  [0]=>
  string(44) "isAliveResponse isAlive(isAlive $parameters)"
  [1]=>
  string(74) "createReservationResponse createReservation(createReservation $parameters)"
  [2]=>
  string(44) "isAliveResponse isAlive(isAlive $parameters)"
  [3]=>
  string(74) "createReservationResponse createReservation(createReservation $parameters)"
}
*/

在上面的示例中,我们将请求接口 (getfunctions) 并执行 SOAP 方法 (__soapCall)。

此致

您需要两件事。根据 __getLastRequestHeaders()__getLastRequest()documentation

This function only works if the SoapClient object was created with the trace option set to TRUE.

所以,你需要这个:

$client = new SoapClient($soapURL,array(
        'trace' => 1
));

此外,文档中提到了以下内容:

Returns the SOAP headers from the last request.

Returns the XML sent in the last SOAP request.

因此,在调用 __getLastRequestHeaders()__getLastRequest() 之前,您必须提出请求。您可以通过以下方式检查可用功能:

var_dump($client->__getFunctions());

看到有函数isAlive()。所以,插入:

var_dump($client->isAlive());

__getLastRequestHeaders()__getLastRequest() 之前会给你结果。