发送变量后的 wsdl 服务响应,php [英] wsdl service response once variables are sent, php

查看:62
本文介绍了发送变量后的 wsdl 服务响应,php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SOAP WSDL 函数的新手.我有一个客户,他从一家从事汽车测试的公司获得了 wsdl 文件.我的客户是他们的分包商.他们告诉我们要上传有关车牌、类别等信息,一旦发送详细信息,服务器就会有成功或失败的响应.请对此提供帮助.

I am new to SOAP WSDL FUNCTIONS. I have a client who has been given a wsdl file from a company that deals in car testing. 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;  
    ?>

推荐答案

首先:去获取 SoapUI 如果您正在处理未知的 Soap 服务.SoapUI 将读取 WSDL 文件并允许您查看与 Soap 方法、参数和返回值相关的几乎所有内容(如果您敢于调用实时服务 - 希望有一个沙盒服务器不做任何重要的事情).

First of all: Go get SoapUI if you are dealing with an unknown Soap service. 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 获得工作代码示例.

But SoapUI can help you there by creating a mock service that receives your request and responds with a canned request that you prepared. Here's how I got from your linked WSDL to a working code example without touching the real service.

在 SoapUI 中创建一个新项目并提供 WSDL 的位置.您可以为这个项目命名.

Create a new project in SoapUI and give the location of the WSDL. You might name this project.

SoapUI 然后读取 WSDL 的内容并创建包含所有描述请求的项目.之后,您会看到服务提供了哪些方法,以及必须将哪些类型的参数放入其中,就像一棵树一样.打开这棵树会让你请求 1"对于检测到的每个方法,它会在免费版本中显示一些 XML(付费版本更适合填写表单),如下所示(来自 vehiclePassedTest 方法):

SoapUI then reads the contents of the WSDL and creates the project containing all described requests. 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):

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

问号是您必须提供数据的地方.此请求结构必须由您使用 PHP 的 SoapClient 创建.

The questionmarks are where you have to provide data. This request structure has to be created by you using the SoapClient of PHP.

让我们嘲笑一下.模拟意味着 SoapUI 启动它自己的接受请求的 Soap 服务器.它通常在 127.0.0.1:8080 上运行.右键点击项目PassedVehicleTestService";并选择New MockService".接受名称,按确定.

Let's mock this. Mocking means that SoapUI starts it's own Soap server that accepts requests. It usually runs on 127.0.0.1:8080. Right-click on the project "PassedVehicleTestService" and select "New MockService". Accept the name, press Ok.

右键单击要模拟的方法.选择添加到模拟服务"并选择上一步中创建的.回答 yes 打开模拟响应编辑器.在那里您会看到答案的 XML 结构,以及服务将填写的数据的问号.来自此服务的所有响应都允许将一个答案作为字符串,因此请编写一些将返回的好内容.如果需要,您可以稍后将其他方法添加到此服务.

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

右键单击您的MockService 1";并选择开始最小化".这将启动 Soap 服务器,它将等待传入的请求.

Right-click your "MockService 1" and select "Start minimized". This will start the Soap server, it will wait for an incoming request.

你需要知道两件事.首先是WSDL 的位置.该文件包含用于请求的真实服务器的地址,但 PHP 允许覆盖它.所以你需要的第二个信息是正在运行的模拟服务的地址.

You need to know two things. First the location of the WSDL. This file contains the address of the real server to be used for the requests, but PHP allows to override this. So the second info you need is the address of the running mock service.

SoapUI 在请求编辑器中显示来自 WSDL 的地址.在您的情况下,它是 http://181.24.80.32/ws/services/PassedVehicleTestService - 模拟服务在 http://127.0.0.1:8080/ws/services/PassedVehicleTestService<上运行/code> - IP 和端口被替换,路径被保留.

SoapUI displays the address from the WSDL in the request editor. 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.

这引出了 PHP 代码的第一行:

This leads to the first lines of PHP code:

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

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

之后,您有一个配置好的 SoapClient 能够与您的模拟服务对话.如果以后要使用真正的服务,把位置"扔掉就行了不在数组中,如果您碰巧添加了其他配置参数,请保留其他配置参数.

After that you have a configured SoapClient able to talk to your mock service. 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.

使用客户端,您可以执行$client->nameOfSoapMethod(paramStructure).参数结构通常是数组或对象和标量值(如字符串)的混合.所以这就是我首先尝试的:

With the client, you can do $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);

然后我查看 SoapUI 看看会发生什么.php 脚本的输出:

Then I look at SoapUI to see what happens. Output from the php script:

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

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>

那不是正确的方法.该数组被转换为字符串Array",并作为第一个参数放置.其余为空.很好,因为我知道soap调用可能接受不止一个参数,这就是这些服务之一.

That was not the right approach. The array got converted into a string "Array", and was placed as the first parameter. The rest is empty. Nice, because I know that soap calls may accept more that one parameter, and this is one of these services.

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

这在 SoapUI 中正确显示,但标题仍然丢失.

This shows up correctly in SoapUI, but the headers are still missing.

SoapClient 提供了一个名为 __setSoapHeaders() 的方法,并且有一个类 SoapHeader.

The SoapClient offers a method named __setSoapHeaders(), and there is a class SoapHeader.

描述说 setSoapHeaders() 接受一个 SoapHeader 对象或一组 SoapHeader 对象.让我们创建一个,传递它,看看会发生什么:

The description says that setSoapHeaders() accepts one SoapHeader object or an array of SoapHeader objects. 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 说:警告:SoapHeader::SoapHeader() 需要至少 2 个参数,0 给定

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

这有效.SoapUI 说:

This works. SoapUI says:

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

注意 SOAP-ENV:Envelope 元素中的两个 xmlns 属性.原始请求要求用户名位于命名空间tr"中.- 但是tr"只是定义为xlmns:tr"的字符串的快捷方式;- 这背后的字符串是您需要的命名空间(尽管它可能已经与实际服务一起使用).

Note the TWO xmlns attributes in the SOAP-ENV:Envelope element. 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).

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

信封元素中没有两个命名空间,并且每个元素都属于命名空间ns1".这应该有效.

No two namespaces in the envelope element, and every element is of namespace "ns1". This should work.

您的最终代码:

$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);

这篇关于发送变量后的 wsdl 服务响应,php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆