简体   繁体   English

SOAP PHP:如何将请求文件转换为PHP函数调用

[英]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 : 我从PHP的SOAP库开始,我不知道如何执行我的请求:

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. 服务器具有用户友好的API,该API向我发送了传递请求,但我不知道应该怎么做。

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. SOAP客户端确实可以为没有参数的其他功能工作,但是当我调用此功能时,我得到了翻译的Java NPE异常。

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: 要将标头添加到soapcall中,请使用__setSoapHeaders方法,如下所示:

$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. 在PHP中,您可以使用对象或关联数组作为输入,因为它们都被解释为xml =键对值对。

$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. soapclient类的返回值始终是一个对象,因此请记住使用箭头符号“ $ object-> property”来获取相关数据。

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 您可以按照文档中所述的多种方式将参数传递给SOAP函数调用: 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. 请注意,大多数SOAP服务器都需要提供参数名称,在这种情况下,它必须是一个关联数组。

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. 希望对您有所帮助。

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

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