简体   繁体   English

变量发送后,WSDL服务响应,PHP

[英]wsdl service response once variables are sent, php

I am new to SOAP WSDL FUNCTIONS. 我是SOAP WSDL函数的新手。 I have a client who has been given a wsdl file from a company that deals in car testing. 我有一个客户,该客户从一家从事汽车测试的公司获得了wsdl文件。 My client is a subcontractor for them. 我的客户是他们的分包商。 They have told us to upload the information about the car plate, category etc and once the details are sent through,There will be a response from server of either success or failure. 他们告诉我们上载有关车牌,类别等的信息,并且一旦详细信息发送出去,服务器就会响应成功或失败。 Kindly assist in this. 请协助。

Browsing through different information, I tried to do something like below but it is not working 浏览不同的信息,我试图做下面的事情,但是它不起作用

    <?php
$data = array('1'=>'value','2'=>'value','3'=>'value','4'='value','5'=>'value');
$wsdl ='http://181.24.80.32/ws/services/servicename';
$client = new SoapClient($wsdl); 
$response = $client->servicenamerequest($data);  

echo $response->servicenamereturn;  
    ?>

First of all: Go get SoapUI if you are dealing with an unknown Soap service. 首先:如果要处理未知的Soap服务,请获取SoapUI SoapUI will read the WSDL file and allow you to look at nearly everything related to Soap methods, parameters, and return values (if you dare to make a call to the live service - hopefully there is a sandbox server that does nothing critical). SoapUI将读取WSDL文件,并允许您查看与Soap方法,参数和返回值相关的几乎所有内容(如果您敢于调用实时服务-希望有一个沙盒服务器可以执行任何重要操作)。

But SoapUI can help you there by creating a mock service that receives your request and responds with a canned request that you prepared. 但是,SoapUI可以通过创建一个模拟服务来帮助您,该服务可以接收您的请求并以您准备的罐装请求进行响应。 Here's how I got from your linked WSDL to a working code example without touching the real service. 这是我从您链接的WSDL到工作代码示例的方法,而无需涉及实际服务。

Setting up SoapUI 设置SoapUI

Create a new project in SoapUI and give the location of the WSDL. 在SoapUI中创建一个新项目,并提供WSDL的位置。 You might name this project. 您可以为这个项目命名。

SoapUI then reads the contents of the WSDL and creates the project containing all described requests. 然后,SoapUI读取WSDL的内容,并创建包含所有描述的请求的项目。 After that you see what methods the service offers, and what kind of parameters have to go into it, as a tree. 之后,您将以树的形式查看服务提供的方法以及必须输入的参数类型。 Opening this tree will get you to "Request 1" for every method detected, which displays some XML in the free version (paid version is slightly more comfortable with a form to fill) like this (from the vehiclePassedTest method): 打开此树将使您进入检测到的每个方法的“请求1”,它以如下形式(来自vehiclePassedTest方法)显示一些免费版本的XML(付费版本稍微适合填写表格):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tr="http://tr.gov.tp.stp.ws.PassedVehicleTestService">
   <soapenv:Header>
      <tr:password>?</tr:password>
      <tr:username>?</tr:username>
   </soapenv:Header>
   <soapenv:Body>
      <tr:vehiclePassedTest>
         <chassisNo>?</chassisNo>
         <plateNo>?</plateNo>
         <plateCode>?</plateCode>
         <plateCategory>?</plateCategory>
         <plcEmiCode>?</plcEmiCode>
         <currentUserName>?</currentUserName>
      </tr:vehiclePassedTest>
   </soapenv:Body>
</soapenv:Envelope>

The questionmarks are where you have to provide data. 问号是您必须提供数据的位置。 This request structure has to be created by you using the SoapClient of PHP. 您必须使用PHP的SoapClient创建此请求结构。

Let's mock this. 让我们来模拟一下。 Mocking means that SoapUI starts it's own Soap server that accepts requests. 模拟意味着SoapUI启动它自己的接受请求的Soap服务器。 It usually runs on 127.0.0.1:8080. 它通常在127.0.0.1:8080上运行。 Right-click on the project "PassedVehicleTestService" and select "New MockService". 右键单击项目“ PassedVehicleTestService”,然后选择“ New MockService”。 Accept the name, press Ok. 接受名称,然后按确定。

Right-click on the method you want to mock. 右键单击要模拟的方法。 Select "Add to mock service" and select the created one from the previous step. 选择“添加到模拟服务”,然后从上一步中选择创建的服务。 Answer yes to open the mock response editor. 回答是,以打开模拟响应编辑器。 There you see the XML structure of the answer, with question marks for the data that the service will fill out. 在那里,您将看到答案的XML结构,其中包含将要填写的服务数据的问号。 All the responses from this service allow one answer as a string, so write something nice that will be returned. 该服务的所有响应都允许将一个答案作为字符串,因此请写出可以返回的漂亮内容。 You can add the other methods to this service later if needed. 如果需要,您可以稍后将其他方法添加到此服务中。

Right-click your "MockService 1" and select "Start minimized". 右键单击“ MockService 1”,然后选择“最小化启动”。 This will start the Soap server, it will wait for an incoming request. 这将启动Soap服务器,它将等待传入的请求。

Setup PHP SoapClient for the mock. 设置PHP SoapClient进行模拟。

You need to know two things. 您需要知道两件事。 First the location of the WSDL. 首先是WSDL的位置。 This file contains the address of the real server to be used for the requests, but PHP allows to override this. 该文件包含用于请求的真实服务器的地址,但是PHP允许覆盖此地址。 So the second info you need is the address of the running mock service. 因此,您需要的第二个信息是正在运行的模拟服务的地址。

SoapUI displays the address from the WSDL in the request editor. SoapUI在请求编辑器中显示来自WSDL的地址。 In your case it is http://181.24.80.32/ws/services/PassedVehicleTestService - the mock service runs on http://127.0.0.1:8080/ws/services/PassedVehicleTestService - IP and port is replaced, the path is kept. 在您的情况下,它是http://181.24.80.32/ws/services/PassedVehicleTestService模拟服务在http://127.0.0.1:8080/ws/services/PassedVehicleTestService运行-IP和端口已替换,路径已保留。

This leads to the first lines of PHP code: 这导致PHP代码的第一行:

$options = array(
    'location' => 'http://127.0.0.1:8080/ws/services/PassedVehicleTestService',
);

$client = new SoapClient("http://www.quickregistration.ae/temp/PassedVehicleTestService.xml", $options);

After that you have a configured SoapClient able to talk to your mock service. 之后,您就可以配置一个SoapClient来与您的模拟服务进行对话。 If later you want to use the real service, throw the line with "location" out of the array, and keep the other config parameters if you happen to add some. 如果以后要使用真正的服务,请将带有“ location”的行从数组中删除,如果碰巧要添加一些,则保留其他配置参数。

Talk to the mock 跟模拟

With the client, you can do $client->nameOfSoapMethod(paramStructure) . 使用客户端,您可以执行$client->nameOfSoapMethod(paramStructure) The parameter structure usually is a mixture of arrays or objects and scalar values like strings. 参数结构通常是数组或对象以及标量值(如字符串)的混合体。 So that's what I try first: 所以这就是我首先尝试的:

$result = $client->vehiclePassedTest(array());
var_dump($result);

Then I look at SoapUI to see what happens. 然后,我看一下SoapUI,看看会发生什么。 Output from the php script: php脚本的输出:

Notice: Array to string conversion in [...]/soap.php on line 20
string(1) "?"

SoapUI says: SoapUI说:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tr.gov.tp.stp.ws.PassedVehicleTestService">
    <SOAP-ENV:Body>
        <ns1:vehiclePassedTest>
            <chassisNo>Array</chassisNo>
            <plateNo/>
            <plateCode/>
            <plateCategory/>
            <plcEmiCode/>
            <currentUserName/>
        </ns1:vehiclePassedTest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

That was not the right approach. 那不是正确的方法。 The array got converted into a string "Array", and was placed as the first parameter. 数组被转换为字符串“ Array”,并被放置为第一个参数。 The rest is empty. 其余为空。 Nice, because I know that soap calls may accept more that one parameter, and this is one of these services. 很好,因为我知道soap调用可以接受多个参数,而这是这些服务之一。

$result = $client->vehiclePassedTest('chassisNo', 'plateNo', 'plateCode', 'plateCategory', 'plcEmiCode', 'currentUserName');

This shows up correctly in SoapUI, but the headers are still missing. 这在SoapUI中正确显示,但是标题仍然丢失。

Adding SoapHeader to the request 将SoapHeader添加到请求中

The SoapClient offers a method named __setSoapHeaders() , and there is a class SoapHeader . SoapClient提供了一个名为__setSoapHeaders()的方法,还有一个SoapHeader类。

The description says that setSoapHeaders() accepts one SoapHeader object or an array of SoapHeader objects. 该描述说setSoapHeaders()接受一个SoapHeader对象或一组SoapHeader对象。 Let's create one, pass it and see what happens: 让我们创建一个,传递它,看看会发生什么:

$username = new SoapHeader();
$client->__setSoapHeaders($username);
$result = $client->vehiclePassedTest('chassisNo', 'plateNo', 'plateCode', 'plateCategory', 'plcEmiCode', 'currentUserName');
var_dump($result);

PHP says: Warning: SoapHeader::SoapHeader() expects at least 2 parameters, 0 given PHP说: Warning: SoapHeader::SoapHeader() expects at least 2 parameters, 0 given

$username = new SoapHeader('namespace', 'username', 'MyUserName');

This works. 这可行。 SoapUI says: SoapUI说:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tr.gov.tp.stp.ws.PassedVehicleTestService" xmlns:ns2="namespace">
    <SOAP-ENV:Header>
        <ns2:username>MyUserName</ns2:username>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:vehiclePassedTest><chassisNo>chassisNo</chassisNo><plateNo>plateNo</plateNo><plateCode>plateCode</plateCode><plateCategory>plateCategory</plateCategory><plcEmiCode>plcEmiCode</plcEmiCode><currentUserName>currentUserName</currentUserName></ns1:vehiclePassedTest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Note the TWO xmlns attributes in the SOAP-ENV:Envelope element. 注意SOAP-ENV:Envelope元素中的两个xmlns属性。 The original request requires the username be in the namespace "tr" - but "tr" is only a shortcut to the string that is defined as "xlmns:tr" - the string behind this is your namespace needed (although it might already work with the real service). 原始请求要求用户名位于名称空间“ tr”中-但是“ tr”只是定义为“ xlmns:tr”的字符串的快捷方式-该字符串背后的字符串是您所需的名称空间(尽管它可能已经与真正的服务)。

$username = new SoapHeader("http://tr.gov.tp.stp.ws.PassedVehicleTestService", 'username', 'myUser');
$password = new SoapHeader("http://tr.gov.tp.stp.ws.PassedVehicleTestService", 'password', 'yetAnotherPassword');
$client->__setSoapHeaders(array($username, $password));

This correctly defines the headers, as you can see: 如您所见,这正确定义了标题:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tr.gov.tp.stp.ws.PassedVehicleTestService">
    <SOAP-ENV:Header>
        <ns1:username>myUser</ns1:username>
        <ns1:password>yetAnotherPassword</ns1:password>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:vehiclePassedTest><chassisNo>chassisNo</chassisNo><plateNo>plateNo</plateNo><plateCode>plateCode</plateCode><plateCategory>plateCategory</plateCategory><plcEmiCode>plcEmiCode</plcEmiCode><currentUserName>currentUserName</currentUserName></ns1:vehiclePassedTest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

No two namespaces in the envelope element, and every element is of namespace "ns1". 信封元素中没有两个名称空间,每个元素都具有名称空间“ ns1”。 This should work. 这应该工作。

Your final code: 您的最终代码:

$options = array(
    'location' => 'http://127.0.0.1:8080/ws/services/PassedVehicleTestService',
);

$client = new SoapClient("http://www.quickregistration.ae/temp/PassedVehicleTestService.xml", $options);

$username = new SoapHeader("http://tr.gov.tp.stp.ws.PassedVehicleTestService", 'username', 'myUser');
$password = new SoapHeader("http://tr.gov.tp.stp.ws.PassedVehicleTestService", 'password', 'yetAnotherPassword');
$client->__setSoapHeaders(array($username, $password));

$result = $client->vehiclePassedTest('chassisNo', 'plateNo', 'plateCode', 'plateCategory', 'plcEmiCode', 'currentUserName');

var_dump($result);

try something like this. 尝试这样的事情。 Works for me. 为我工作。

try {
  $client = new SoapClient("http://wsdl",  array('trace' => 1));
  $data = $client->someFunction(array('parma1' => 'value1', 'param2' => 'value2'));
  print_r($data);
} catch (SoapFault $fault) {
   trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
   exit();
}

And check is you have installed php-soap package in your server. 并检查您是否已在服务器中安装了php-soap软件包。

Greatings. Greatings。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM