当前位置: 首页 > 知识库问答 >
问题:

如何使用WSDL创建php SOAP服务器函数?

郑琦
2023-03-14

我想创建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文件。现在我不知道如何创建功能。我得到以下错误:

致命错误:在C:\wamp\www\soap websiteservice wsdl\client中未找到未捕获的SoapFault异常:[HTTP]。php:7堆栈跟踪:#0[内部函数]:SoapClient-

共有2个答案

公冶渝
2023-03-14

最后我弄明白它是如何工作的。

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

我的服务器。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);

创建的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服务器和客户端

丌官丰羽
2023-03-14

试试看

服务器对象:

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);
 类似资料:
  • nodejs版本:0.8.6 我使用openssl创建了一个ssl csr文件,并使用以下命令:openssl req-nodes-newkey rsa:2048-keyout myserver.key-out myserver.csr null 我试着检查这个问题:openssl s_client-connect my_dns:443//my_dns当然指向我的nodejs服务器 结果:已连接(

  • 根据这个http://www.grpc.io/docs/tutorials/basic/python.html#creating-这里的服务器和示例https://github.com/grpc/grpc/tree/v1.0.0/examples/python/route_guide,当我生成我的pb2时。py文件中,应创建几个名为Stub和Servicer的类。但是,我生成了pb2。py文件不包

  • 我需要从现有的WSDL文件为使用OCPP协议的系统创建一个中心系统(SOAP服务器)。(开放充电桩协议) 我已经成功地在非wsdl模式和wsdl模式下编写了测试SOAP服务器和客户机,但是当我尝试使用现有的wsdl时,我得到的只是一堆错误(这并不能告诉我太多) 可以在此处找到WSDL文件(中央系统和chargepoint。抱歉,无法发布更多链接..) 我已经修复了一些错误,但是不能越过这个。 我想

  • 创建 http 服务器 package main   import ( "net/http"   "github.com/hprose/hprose-golang/rpc" )   func hello(name string) string { return "Hello " + name + "!" }   func main() { service := rp

  • 我正试图在谷歌云平台上创建一个服务账户,该账户只能访问谷歌驱动API。然而,我根本找不到这个角色: 此外,如果我创建了一个“通用服务帐户”,那么该用户可以添加实例等。这在过去是一个可怕的安全问题,因为拥有该密钥的用户实际上创建了未经授权的实例(因此出现了上述问题)。 我该如何创建一个可以访问Google Drive API的服务帐户,而不是别的?

  • 问题内容: 是否可以为SQL Server的链接服务器之类的功能创建/配置MySQL? 如果是,请告诉我如何?我正在使用MySQL 5.5。 问题答案: MySQL的FEDERATED引擎提供的功能类似于SQL Server的链接服务器(和Oracle的dblink)功能,但不支持连接到MySQL以外的供应商。从这个问题尚不清楚,是否需要功能来连接到除MySQL以外的供应商。 您可能需要研究MyS