dongyoudi1342 2016-08-10 15:23
浏览 60
已采纳

如何使用php进行简单的SOAP调用?

I am making call to Zefix Webservice Schnittstelle using Php. I don't know how to consume SOAP. Following details are given

<message name="SearchByNameRequestMsg">
    <part name="body" element="zefix:searchByNameRequest"/>
</message>

<operation name="SearchByName">
    <input message="zefix:SearchByNameRequestMsg"/>
    <output message="zefix:ShortResponseMsg"/>
</operation>

<operation name="SearchByName">
    <soap:operation soapAction="http://soap.zefix.admin.ch/SearchByName"/>
    <input>
    <soap:body use="literal"/>
    </input>
    <output>
        <soap:body use="literal"/>
    </output>
</operation>

I have tried following php code and some other

$url = "http://test-e-service.fenceit.ch:80/ws-zefix-1.7/ZefixService";



$soap_request  = "<?xml version=\"1.0\"?>
";
  $soap_request .= "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
";
  $soap_request .= "   <soap:Body>
";
  $soap_request .= "    <SearchByName xmlns=\"http://soap.zefix.admin.ch/SearchByName\">
";
  $soap_request .= "      <Name>Autocenter</Name>
";
  $soap_request .= "    </SearchByName>
";
  $soap_request .= "  </soap:Body>
";
  $soap_request .= "</soap:Envelope>";

  $header = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: \"run\"",
    "Content-length: ".strlen($soap_request),
  );


$soap_do = curl_init(); 
curl_setopt($soap_do, CURLOPT_URL,            $url );   
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($soap_do, CURLOPT_POST,           true ); 
curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $soap_request); 
curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($soap_request) )); 
curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);

$result = curl_exec($soap_do);
 if($result === false) {
    $err = 'Curl error: ' . curl_error($soap_do);
    curl_close($soap_do);
    print $err;
 } else {
    curl_close($soap_do);
    print 'Operation completed without any errors';
 }


print_r($result);

XML provided

  • 写回答

2条回答 默认 最新

  • dpzzkfb1244 2016-08-15 17:55
    关注

    Finally I got answer

    I have changed SoapAction

    "SOAPAction: \"http://soap.zefix.admin.ch/SearchByName\"",
    

    And Soap Request Body like below

    $user = "XXXXXX";
    $password = "XXXX";
    
    $url = "http://test-e-service.fenceit.ch:80/ws-zefix-1.7/ZefixService";
    
    $soap_request  = "<?xml version=\"1.0\"?>
    ";
    $soap_request .= "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">
    ";
    $soap_request .= "   <Body>
    ";
    $soap_request .= "    <searchByNameRequest xmlns=\"http://www.e-service.admin.ch/zefix/2015-06-26\">
    ";
    $soap_request .= "      <name>Online Consulting AG</name>
    ";
    $soap_request .= "      <active>true</active>
    ";
    $soap_request .= "      <maxSize>2</maxSize>
    ";
    $soap_request .= "    </searchByNameRequest>
    ";
    $soap_request .= "  </Body>
    ";
    $soap_request .= "</Envelope>";
    
    $header = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: \"http://soap.zefix.admin.ch/SearchByName\"",
        "Content-length: ".strlen($soap_request),
      );
    
    
    $soap_do = curl_init(); 
    curl_setopt($soap_do, CURLOPT_URL,            $url );   
    curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
    curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
    curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($soap_do, CURLOPT_POST,           true ); 
    curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $soap_request); 
    curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($soap_request) )); 
    curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);
    
    $result = curl_exec($soap_do);
     if($result === false) {
        $err = 'Curl error: ' . curl_error($soap_do);
        curl_close($soap_do);
        print $err;
     } else {
        curl_close($soap_do);
        //print 'Operation completed without any errors';
     }
    
    echo $result;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量
  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入