简体   繁体   English

Soap身份验证标头和使用PHP的请求

[英]Soap authentication header and request using PHP

I am trying to setup a SOAP call for an online restaurant table booking service but haven't been able to get it working. 我正在尝试为在线餐厅餐桌预订服务设置SOAP调用,但是无法使其正常工作。 It uses authentication header and have tried the following code without any luck: 它使用身份验证标头,并尝试了以下代码,但没有任何运气:

$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());

But it just echo's NULL NULL... :-( 但这只是echo的NULL NULL ... :-(

I am not familiar with PHP SOAP calls and not at all using authentication headers, but hope somebody can push me in the right direction. 我对PHP SOAP调用并不熟悉,也不完全使用身份验证标头,但是希望有人可以将我推向正确的方向。

you was near to reach your goal. 您快要实现目标了。 Here I have modified few lines of your code: 在这里,我修改了几行代码:

    <?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)"
}
*/

In the above example we will ask for interface (getfunctions) and execute a SOAP method (__soapCall). 在上面的示例中,我们将要求提供接口(getfunctions)并执行SOAP方法(__soapCall)。

Regards 问候

There are two things that you need. 您需要两件事。 As per the documentation of __getLastRequestHeaders() and __getLastRequest() : 根据__getLastRequestHeaders()__getLastRequest()文档

This function only works if the SoapClient object was created with the trace option set to TRUE. 仅当在跟踪选项设置为TRUE的情况下创建SoapClient对象时,此功能才有效。

So, you need this: 因此,您需要这样做:

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

Also, the documentation mentions the following: 此外,文档还提到以下内容:

Returns the SOAP headers from the last request. 返回上一个请求的SOAP标头。

Returns the XML sent in the last SOAP request. 返回在上一个SOAP请求中发送的XML。

So, before calling __getLastRequestHeaders() and __getLastRequest() , you have to make a request. 因此,在调用__getLastRequestHeaders()__getLastRequest() ,您必须发出请求。 You can check the available functions with: 您可以使用以下方法检查可用功能:

var_dump($client->__getFunctions());

and see that there is function isAlive() . 并看到有功能isAlive() So, inserting: 因此,插入:

var_dump($client->isAlive());

before __getLastRequestHeaders() and __getLastRequest() will give you results. __getLastRequestHeaders()__getLastRequest()会为您提供结果之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM