簡體   English   中英

如何使用 WSDL 創建 php SOAP 服務器功能?

[英]How to create php SOAP Server functions using WSDL?

我想創建 PHP SOAP 服務器,但不明白如何正確執行。 所以下面是server.php文件:

<?php

class NewOperation {
    public function NewOperation()
    {

    }
}

ini_set("soap.wsdl_cache_enabled", "0");
$server = new SOAPServer('http://localhost:9080/soap-websiteservice-        wsdl/CalculatorService.wsdl', array(
    'soap_version' => SOAP_1_2,
    'style' => SOAP_RPC,
    'use' => SOAP_LITERAL
));
$server->setClass('NewOperation');
$server->handle();

client.php 文件

<?php
// client.php
$options = array(
    'trace' => true
);
$client = new SOAPClient('http://localhost:9080/soap-websiteservice-    wsdl/server.php?wsdl', $options);
var_dump($client->NewOperation());

wsdl 文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"     xmlns:tns="http://localhost:9080/soap-websiteservice-wsdl/CalculatorService/"     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CalculatorService"     targetNamespace="http://localhost:9080/soap-websiteservice-    wsdl/CalculatorService/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost:9080/soap-websiteservice-    wsdl/CalculatorService/">
      <xsd:element name="add" type="xsd:string">

      </xsd:element>
  <xsd:element name="addResponse">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="addResponse" type="tns:addResponseType" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="addRequestType">
    <xsd:sequence>
        <xsd:element name="num1" type="xsd:int"></xsd:element>
        <xsd:element name="num2" type="xsd:int"></xsd:element>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="addResponseType">
    <xsd:sequence>
        <xsd:element name="result" type="xsd:string"></xsd:element>
    </xsd:sequence>
  </xsd:complexType>
        <xsd:element name="in" type="xsd:string"></xsd:element>
        <xsd:element name="NewOperationResponse">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="out" type="xsd:string"></xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
</xsd:schema>
  </wsdl:types>
  <wsdl:message name="addRequest">
<wsdl:part element="tns:add" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="NewOperationRequest">
<wsdl:part name="NewOperationRequest" element="tns:in"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="NewOperationResponse">
    <wsdl:part name="parameters" element="tns:NewOperationResponse">        </wsdl:part>
  </wsdl:message>
<wsdl:portType name="CalculatorService">

<wsdl:operation name="NewOperation">
    <wsdl:input message="tns:NewOperationRequest"></wsdl:input>
    <wsdl:output message="tns:NewOperationResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorServiceSOAP"
type="tns:CalculatorService">
<soap:binding style="document"
    transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="NewOperation">
    <soap:operation
        soapAction="http://localhost:9080/soap-websiteservice-        wsdl/CalculatorService/NewOperation" />
    <wsdl:input>
        <soap:body use="literal" />
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal" />
    </wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalculatorService">
<wsdl:port binding="tns:CalculatorServiceSOAP" name="CalculatorServiceSOAP">
  <soap:address location="http://localhost:9080/soap-websiteservice-wsdl/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

來自日食的圖像

壓縮所有文件

我使用 Eclipse Web 服務 XML 編輯器創建了 WSDL 文件。 現在我不知道如何創建函數。 我收到以下錯誤:

致命錯誤:未捕獲的 SoapFault 異常:[HTTP] Not Found in C:\\wamp\\www\\soap-websiteservice-wsdl\\client.php:7 堆棧跟蹤:#0 [內部函數]:SoapClient->__doRequest('http:/ /localhos...', ' http://localhos ...', 1, 0) #1 C:\\wamp\\www\\soap-websiteservice-wsdl\\client.php(7): SoapClient->__call(' NewOperation', Array) #2 C:\\wamp\\www\\soap-websiteservice-wsdl\\client.php(7): SoapClient->NewOperation() #3 {main} 被拋出到 C:\\wamp\\www\\soap-websiteservice -wsdl\\client.php 第 7 行

嘗試一下

服務器對象:

class Server{

 protected $class_name = '';

 public function __construct($class_name)
    {
        $this->class_name = $class_name;
    }
 public function AuthHeader($Header)
    {
        //if($Header->username == 'foo' && $Header->password == 'bar')
         //   $this->authenticated = true;

    }

 public function log($method_name,$data)
    {
        $filename = 'log.txt';
        $handle = fopen($filename, 'a+');
        fwrite($handle, date("l dS of F Y h:i:s A").' - '.$_SERVER['REMOTE_ADDR']."\r\n".$method_name."\r\n".print_r($data,true));
        fclose($handle);
    }   

 public function __call($method_name, $arguments)
    {
        $this->log($method_name,$arguments); //  log
        if($arguments[0]!=AUTH) return 'Authorization required'; // auth check
        $_method_name = '_'.$method_name; //  method name replace
        if(!method_exists($this->class_name, $_method_name )) return 'Method '.$method_name.' not found'; // methot exist check
        return call_user_func_array(array($this->class_name, $_method_name ), $arguments); //call method
    }
}

這是我的帶有日志記錄請求的工作代碼。 我有同樣的問題。

服務器:

$Service = new Server('YouClassHere');

$server->setObject($Service);

最后我弄清楚它是如何工作的。

現在我在 Eclipse 中的 wsdl 看起來像: 在此處輸入圖片說明

和 Calculator.wsdl:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:9080/soap-websiteservice-wsdl/Calculator" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Calculator" targetNamespace="http://localhost:9080/soap-websiteservice-wsdl/Calculator">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost:9080/soap-websiteservice-wsdl/Calculator">
      <xsd:element name="Average">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="num1" type="xsd:int" />
            <xsd:element name="num2" type="xsd:int"></xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="AverageResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Result" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>

      <xsd:complexType name="AverageRequestType">
        <xsd:sequence>
            <xsd:element name="num1" type="xsd:int"></xsd:element>
            <xsd:element name="num2" type="xsd:int"></xsd:element>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="AverageRequest">
    <wsdl:part element="tns:Average" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="AverageResponse">
    <wsdl:part element="tns:AverageResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:portType name="Calculator">
    <wsdl:operation name="Average">
      <wsdl:input message="tns:AverageRequest"/>
      <wsdl:output message="tns:AverageResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="CalculatorSOAP" type="tns:Calculator">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="Average">
      <soap:operation soapAction="http://localhost:9080/soap-websiteservice-wsdl/Calculator/NewOperation"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Calculator">
    <wsdl:port binding="tns:CalculatorSOAP" name="CalculatorSOAP">
      <soap:address location="http://localhost:9080/soap-websiteservice-wsdl/server.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

我的 server.php:

        <?php
    ini_set("soap.wsdl_cache_enabled", "0");

    class Server{

        protected $class_name = '';

        public function __construct($class_name)
        {
            $this->class_name = $class_name;
        }
        public function AuthHeader($Header)
        {
            //if($Header->username == 'foo' && $Header->password == 'bar')
            //   $this->authenticated = true;

        }

        public function log($method_name,$data)
        {
            $filename = 'log.txt';
            $handle = fopen($filename, 'a+');
            fwrite($handle, date("l dS of F Y h:i:s A").' - '.$_SERVER['REMOTE_ADDR']."\r\n".$method_name."\r\n".print_r($data,true));
            fclose($handle);
        }

        public function __call($method_name, $parameters)
        {
            $this->log($method_name,$parameters); //  log
            //if($arguments[0]!=AUTH) return 'Authorization required'; // auth check
            if(!method_exists($this->class_name, $method_name )) return 'Method '.$method_name.' not found'; // methot exist check
            return call_user_func_array(array($this->class_name, $method_name ), $parameters); //call method
        }
    }


    class Calculator {
        public function Average ($parameters)
        {
            $num1 = $parameters->num1;
            $num2 = $parameters->num2;

            return self::AverageResponse(($num1 + $num2) / 2);
        }

        public function AverageResponse ($message)
        {
            return ['Result' => $message];
        }
    }

    class in {

    }

    $Service = new Server('Calculator');
    $classmap=[
        'in' => 'in'
    ];
    $server = new SOAPServer('http://localhost:9080/soap-websiteservice-wsdl/Calculator.wsdl', array(
        'soap_version' => SOAP_1_2,
        'style' => SOAP_RPC,
        'use' => SOAP_LITERAL,
        'classmap'=>$classmap
    ));
    $server->setObject($Service);
    //$server->setClass('Calculator');
    $server->handle();

客戶端.php

        <?php
    // client.php
    $options = array(
        'trace' => true
    );
    $client = new SOAPClient('http://localhost:9080/soap-websiteservice-wsdl/server.php?wsdl', $options);
    var_dump($client->Average(['num1' => 10, 'num2' => 6])->Result);

所以我用 Virtual Studio 進行了測試。 所以我創建了表單: 在此處輸入圖片說明

創建的 Web 服務引用:localhost。 我的按鈕單擊事件如下所示:

        using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                localhost.Calculator client = new localhost.Calculator();

                string returnString;

                returnString = client.Average(10,8);
                label1.Text = returnString;
            }
        }
    }

最后: 在此處輸入圖片說明

最終測試代碼位於: https : //bitbucket.org/Ernestyno/laravel-soap-server-and-client/src/51d144f41bbcc7680bb3d4a7f6e8aedcbef0cb77? at =master

在這里我分享如何使用 Laravel 制作 SOAP 服務器和客戶端!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM