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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?