简体   繁体   中英

SOAP PHP : how to translate request file to PHP function call

I'm beginning with the SOAP lib of PHP and i can't figure out how to execute my request :

The server has a user friendly API which gives me the request to pass but i can't tell how I am supposed to do so.

Here is the point I currently am :

$soap = new SoapClient("https://www.dmc.sfr-sh.fr/DmcWS/1.5.6/MessagesUnitairesWS?wsdl");

$soap->getSingleCallCra();

and the request i should pass :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://servicedata.ws.dmc.sfrbt/">
 <soapenv:Header>
  <ser:authenticate>
   <serviceId>********</serviceId>
   <servicePassword>******</servicePassword>
   <spaceId>*******</spaceId>
   <lang>fr_FR</lang>
  </ser:authenticate>
 </soapenv:Header>
 <soapenv:Body>
  <ser:getSingleCallCra>
   <beginDate>2017-10-17T00:00:00</beginDate>
  </ser:getSingleCallCra>
 </soapenv:Body>
</soapenv:Envelope>

The SOAP client does work for other function with no parameter but i get a translated java NPE exception when i call this function.

Can anyone tell me how i can pass the parameters and authentification to the function ?

Thanks.

$soap = new SoapClient("https://www.dmc.sfr-sh.fr/DmcWS/1.5.6/MessagesUnitairesWS?wsdl");

To add headers to a soapcall use the __setSoapHeaders method like this:

$soap->__setSoapHeaders(array(
  //(namespace, name, data)
  new SoapHeader("http://servicedata.ws.dmc.sfrbt/",'authenticate',array(
    'serviceId' => '********',
    'servicePassword' => '******',
    'spaceId' => '*******',
    'lang' => 'fr_FR',
  ))
));

These parameters will go into the soap body. In PHP you can use objects or associative arrays as input as they are both interpreted into xml as key => value pairs.

$soap_body_parameters = array(
  'beginDate' => '2017-10-17T00:00:00',
);

$response = $soap->getSingleCallCra($soap_body_parameters);

print_r($response);

The return value of the soapclient class is always an object, so remember to use the arrow notation '$object->property' to get the relevant data out.

You can also create a class like this, that will deal with the headers, data extraction, etc. in the background for each call

class sfr_soap {
  function __construct($serviceId, $servicePassword, $spaceId, $lang = 'fr_FR'){
    $url = "https://www.dmc.sfr-sh.fr/DmcWS/1.5.6/MessagesUnitairesWS?wsdl";
    $this->client = new SoapClient($url);
    $soap->__setSoapHeaders(array(
        new SoapHeader("http://servicedata.ws.dmc.sfrbt/",'authenticate',array(
            'serviceId' => $serviceId,
            'servicePassword' => $servicePassword,
            'spaceId' => $spaceId,
            'lang' => $lang,
        ))
    ));
  }
  public function __call($name, $args = array()){
    $response = $this->client->$name($args);
    // do something with the response here, like extract the meaningful parts of the data
    return $response;
  }
}

init like this

$sfr = new sfr_soap($serviceId, $servicePassword, $spaceId);

or like this if you want to specify the language

$sfr = new sfr_soap($serviceId, $servicePassword, $spaceId, $lang);

use like this

$data = $sfr->getSingleCallCra(array(
  'beginDate' => '2017-10-17T00:00:00'
));

You can pass arguments to a SOAP function call multiple ways as it is stated in the documentation: SoapClient::__soapCall

An array of the arguments to pass to the function. This can be either an ordered or an associative array. Note that most SOAP servers require parameter names to be provided, in which case this must be an associative array.

So in your case, the call should be:

$soap->getSingleCallCra(array(
   'beginDate'  => '2017-10-17T00:00:00',
));

I hope, I could be of any help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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