dongpuchao1680 2017-10-17 13:03
浏览 81
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dqpdb82600 2017-10-17 13:29
    关注
    $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'
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?