问题
I am trying to setup a SOAP call for an online restaurant table booking service but haven't been able to get it working. 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... :-(
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.
回答1:
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).
Regards
回答2:
There are two things that you need. As per the documentation of __getLastRequestHeaders()
and __getLastRequest()
:
This function only works if the SoapClient object was created with the trace option set to TRUE.
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.
Returns the XML sent in the last SOAP request.
So, before calling __getLastRequestHeaders()
and __getLastRequest()
, you have to make a request. You can check the available functions with:
var_dump($client->__getFunctions());
and see that there is function isAlive()
. So, inserting:
var_dump($client->isAlive());
before __getLastRequestHeaders()
and __getLastRequest()
will give you results.
来源:https://stackoverflow.com/questions/30225281/soap-authentication-header-and-request-using-php