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

时间:2016-08-10 15:23:22

标签: php xml web-services soap wsdl

我正在使用Php调用Zefix Webservice Schnittstelle。我不知道如何使用SOAP。详细信息如下

                                        

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

我尝试过以下php代码和其他一些

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



$soap_request  = "<?xml version=\"1.0\"?>\n";
  $soap_request .= "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n";
  $soap_request .= "   <soap:Body>\n";
  $soap_request .= "    <SearchByName xmlns=\"http://soap.zefix.admin.ch/SearchByName\">\n";
  $soap_request .= "      <Name>Autocenter</Name>\n";
  $soap_request .= "    </SearchByName>\n";
  $soap_request .= "  </soap:Body>\n";
  $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 个答案:

答案 0 :(得分:0)

    // set your headers
    $ctx_opts = [
        'http' => [
            'header' => 'Content-Type: text/xml;charset=utf-8'
        ]
    ];

    // create stream context 
    $ctx = stream_context_create($ctx_opts);

    // array of options
    $options = [
        'stream_context' => $ctx,
        'proxy_host' => "yourhost.net", // host
        'proxy_port' => 80 // port
    ];

    // init your client
    $client = new \SoapClient('http://address.net/webservice', $options);

    // add function parameters
    $data = ['parameter' = > 'value'];

    // than invoke your function
    $response = $client->SearchByName($data);

答案 1 :(得分:0)

最后我得到了答案

我改变了SoapAction

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

肥皂请求体如下     

$user = "XXXXXX";
$password = "XXXX";

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

$soap_request  = "<?xml version=\"1.0\"?>\n";
$soap_request .= "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
$soap_request .= "   <Body>\n";
$soap_request .= "    <searchByNameRequest xmlns=\"http://www.e-service.admin.ch/zefix/2015-06-26\">\n";
$soap_request .= "      <name>Online Consulting AG</name>\n";
$soap_request .= "      <active>true</active>\n";
$soap_request .= "      <maxSize>2</maxSize>\n";
$soap_request .= "    </searchByNameRequest>\n";
$soap_request .= "  </Body>\n";
$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;

?>