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

时间:2015-05-13 21:11:50

标签: php authentication soap soap-client

我正在尝试为在线餐厅餐桌预订服务设置SOAP调用,但无法使其正常工作。 它使用身份验证标头,并尝试了以下代码,没有任何运气:

$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调用,完全不熟悉使用身份验证标头,但希望有人能帮助我朝着正确的方向发展。

2 个答案:

答案 0 :(得分:3)

你快要达到目的了。在这里,我修改了几行代码:

    <?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)。

此致

答案 1 :(得分:2)

您需要两件事。根据{{​​1}}和__getLastRequestHeaders()的{​​{3}}:

  

此功能仅在使用。创建SoapClient对象时有效   trace选项设置为TRUE。

所以,你需要这个:

__getLastRequest()

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

  

返回上一个请求的SOAP标头。

     

返回上次SOAP请求中发送的XML。

因此,在致电$client = new SoapClient($soapURL,array( 'trace' => 1 )); __getLastRequestHeaders()之前,您必须提出请求。您可以使用以下方法检查可用的功能:

__getLastRequest()

并看到有var_dump($client->__getFunctions()); 函数。所以,插入:

isAlive()

之前var_dump($client->isAlive()); __getLastRequestHeaders()会给你结果。