使用 php SOAP 和 WSDL 的正确步骤是什么? [英] What are the right steps to work with php SOAP and WSDL?

查看:24
本文介绍了使用 php SOAP 和 WSDL 的正确步骤是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了许多问题和困惑,请参阅

关于这个问题的最终代码和问题

<小时>

在开发 PHP SOAP 服务时(第一次),我遇到了很多错误.

让我画出场景以便更好地理解 &在我的理解中

我手上有什么

  • 一个网址(.asmx)
  • 2 个 WSDL 链接
    • ==> https :.../Schemas/Informationservice.WSDL
    • ==> https :.../Schemas/Orderuploadservice.WSDL
  • 3 个证书文件(.pem.CER.PFX )

PHP SOAP 文档 的参考中,我创建了这个

PS 此网络服务位于 ASP.netSOAP 1.1 上,因此请使用 https

以下是主要代码部分

 $x = new SoapClient(, [//'local_cert' =>'<cert>.pem',//'allow_self_signed' =>真的,//'cache_wsdl' =>WSDL_CACHE_NONE,'位置' => 'https://192.168.xx.xx/wsdl/try.php','uri' =><other_wsdlink>,'密码' =>$密码,'例外' =>错误的,'跟踪' =>真的,'soap_version' =>肥皂_1_1//'connection_timeout'=>2000,//'编码'=>'ISO-8859-1'//'UTF-8']);

这里有两个案例

<小时>

1.当我运行这个(注释 3 行)时,响应时间太长,最后在 XML 中给出错误,

  • 过程requestedMethodRequest"不存在.
  • 函数requestedMethod"不存在.

查看输出

<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>过程requestedMethodRequest"不存在</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

或有时(通过评论其他几个选项或更改 uri )它给出

<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>函数requestedMethod"不存在</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

尽管函数存在于返回SoapServer::getFunctions()

数组([0] =>无参数的第一个函数[1] =>无参数的第二个函数[2] =>RequestedMethodResponse RequestedMethod(requestedMethodRequest $messagePart)[3] =>第二个函数类参数)

<小时>

2.当我在 3 行以上取消注释时,会给出多个警告和致命错误

<块引用>

警告:SoapClient::SoapClient():

 - 无法设置私钥文件 <cert>.pem- 未能创建 SSL 句柄- 无法启用加密- (): 无法打开流:操作失败- I/O 警告:无法加载外部实体 <wsdlink>

致命错误:SOAP-ERROR:解析 WSDL:无法从 加载:无法加载外部实体

我搜索并阅读了各种博客中的所有错误,并采纳了他们的建议,但到目前为止还没有成功.从 这个博客 之一,我删除了任何临时文件来自 /tmp/wsdl* 和使用 ini_set

禁用的 wsdl 缓存

 ini_set('soap.wsdl_cache_enabled',0);ini_set('soap.wsdl_cache_ttl',0);

我的疑问和困惑是:

  1. 我需要在构造函数中设置 soapClientsoapServer 吗?

  2. 我需要为证书文件设置一些额外的规则吗?

  3. $uri 选项有什么作用(这是强制性的),哪个是为请求发送的,或者

  4. 这是证书问题还是可能是系统或网络问题(UBUNTU 14.04),如果是,如何检查?

更新

更改权限证书

 openssl pkcs12 -in certificatefile.PFX -out certfile.pem -nodes -clcerts

并取消注释 cert 选项并注释 __doRequest 方法中的所有 preg_replace 代码

但现在标题问题提出.我注意到我发送的请求正在被修剪或切断.

我通过SoapClient::__soapCall

<肥皂:身体><addressVerificationRequest xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common"><地址验证><samId xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">string</samId>...<postalCode xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">string</postalCode></addressVerification></addressVerificationRequest></soap:Body></soap:Envelope>;

var_dump($client->__getLastRequest());

上面的标题问题已修复,它是由错误的 uri value 生成的.这将是命名空间

解决方案

确保您在 PHP 中获得正确类的任何简单方法是使用 https://github.com/wsdl2phpgenerator/wsdl2phpgenerator 他们提供了一个命令行生成器,你可以使用你拥有的 WSDL 运行它,它会为你生成一堆 PHP 类来处理所有的请求和响应,最重要的是服务.

从您调用网络服务的类中,您可以执行以下操作:

//构造服务$service = 新服务();//构造请求$request = new RequestMethod();$request->element = "test";//调用函数$response = $service->callRequest($request);//处理响应死(var_dump($响应));

如果您使用框架之类的东西,您可能需要重构生成的类,但它为您提供了一个很好的起点.

I have fixed many of issues and confusions, Please see

Final Code and issue on this question


while developing PHP SOAP service (first time) I m facing many errors.

Let me draw the scenario to understand better & in my understandings

What I have on my hand

  • A web url (<weburl>.asmx)
  • 2 WSDL links
    • <wsdlink> ==> https :.../Schemas/Informationservice.WSDL
    • <other_wsdlink> ==> https :.../Schemas/Orderuploadservice.WSDL
  • 3 certificate files (<cert>.pem and <cert>.CER and <cert>.PFX )

From Reference of PHP SOAP Documentation , I have created this class

P.S. this web service is on ASP.net and SOAP 1.1 so run this with https

below is the main code section

  $x = new SoapClient(<wsdlink>, [
                                // 'local_cert' => '<cert>.pem',
                                // 'allow_self_signed' => true,
                                // 'cache_wsdl' => WSDL_CACHE_NONE,
                                'location' =>'https://192.168.xx.xx/wsdl/try.php',
                                'uri' =><other_wsdlink>,
                                'passphrase' => $passphrase,
                                'exceptions' => false,
                                'trace' => true,
                                'soap_version' => SOAP_1_1
                                //'connection_timeout'=>2000,
                                //'encoding'=> 'ISO-8859-1' // 'UTF-8'
                            ]);

Here are 2 cases


1. When I run this ( commented 3 lines) then it takes too much time to response and finally gives the error in XML,

  • Procedure 'requestedMethodRequest' not present.
  • Function 'requestedMethod' doesn't exist.

see the output

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>Procedure 'requestedMethodRequest' not present</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

or sometimes ( by commenting few other options or chaning the uri ) it gives

<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
    <SOAP-ENV:Fault>
        <faultcode>SOAP-ENV:Server</faultcode>
        <faultstring>Function 'requestedMethod' doesn't exist</faultstring>
    </SOAP-ENV:Fault>
</SOAP-ENV:Body></SOAP-ENV:Envelope>

despite the fact that the function exist in the return SoapServer::getFunctions()

Array
(
[0] => firstFunctionWithoutParam
[1] => secondFunctionWithoutParam
[2] => requestedMethodResponse RequestedMethod(requestedMethodRequest $messagePart)
[3] => secondFunctionClassParam
)


2.When I un comment above 3 lines then gives multiple Warnings and FATAL Error

Warning: SoapClient::SoapClient():

  - Unable to set private key file <cert>.pem
  - failed to create an SSL handle
  - Failed to enable crypto
  - (<wsdlink>):failed to open stream: operation failed
  - I/O warning : failed to load external entity <wsdlink>

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from <wsdlink>:failed to load external entity <wsdlink>

I have searched and read various blogs for all errors and applied their suggestions but no luck so far. from one of this blog, I have deleted any temp file from /tmp/wsdl* and wsdl cache disabled using ini_set

    ini_set('soap.wsdl_cache_enabled',0);
    ini_set('soap.wsdl_cache_ttl',0);

My queries and confusions are :

  1. Do I need to set up soapClient and soapServer both in constructor?

  2. Do I need to set something extra rules for certificate file?

  3. what does the $uri option do ( is that mandatory) and which is sending for the request or

  4. Is this a certificate issue or may be the system or network issue (UBUNTU 14.04), If it is then how do check this?

update

change permission certificate

 openssl pkcs12 -in certificatefile.PFX -out certfile.pem -nodes -clcerts  

and uncomment cert option and commented all preg_replace code from __doRequest method

but now Header issue raised. I notice that the request I sent is being trimmed or chopped off.

Request which I sent through SoapClient::__soapCall is

<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
          <soap:Body>
            <addressVerificationRequest xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">
              <addressVerification>
                <samId xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">string</samId>
                ...
                <postalCode xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">string</postalCode>
              </addressVerification>
            </addressVerificationRequest>
          </soap:Body>
        </soap:Envelope>;

and var_dump($client->__getLastRequest());

<?xml version="1.0" encoding="UTF-8"?>  
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:DCNZ.MSP.Wireline.Wholesale.Messages">  
 <SOAP-ENV:Body><ns1:addressVerificationRequest/></SOAP-ENV:Body>
</SOAP-ENV:Envelope>'

above header issue fixed, it was generated cause of wrong uri value . It would be namespace

解决方案

Any easy way to ensure you get the right classes in PHP is to use https://github.com/wsdl2phpgenerator/wsdl2phpgenerator they supply a command line generator that you can run with the WSDL that you have and it will generate you a bunch of PHP classes for all the requests and responses and most importantly the Service.

From your class where you are calling the webservice you can then do something like:

// Construct the service
$service = new Service();
// Construct the request
$request = new RequestMethod();
$request->element = "test";
// Call the function
$response = $service->callRequest($request);
// process response
die(var_dump($response));

You will likely have to refactor the generated classes if you are working with something like a framework, but it gives you a great starting point.

这篇关于使用 php SOAP 和 WSDL 的正确步骤是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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