如何从 PHP 发布 SOAP 请求 [英] How to post SOAP Request from PHP

查看:30
本文介绍了如何从 PHP 发布 SOAP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何从 PHP 发布 SOAP 请求吗?

Anyone know how can I post a SOAP Request from PHP?

推荐答案

根据我的经验,事情没那么简单.内置的 PHP SOAP 客户端 不适用于 .NET我们必须使用的基于 SOAP 服务器.它抱怨无效的架构定义.即使 .NET 客户端与该服务器一起工作也很好.顺便说一句,让我声明 SOAP 互操作性是一个神话.

In my experience, it's not quite that simple. The built-in PHP SOAP client didn't work with the .NET-based SOAP server we had to use. It complained about an invalid schema definition. Even though .NET client worked with that server just fine. By the way, let me claim that SOAP interoperability is a myth.

下一步是NuSOAP.这工作了很长一段时间.顺便说一句,看在上帝的份上,不要忘记缓存 WSDL!但即使使用 WSDL 缓存,用户也抱怨该死的东西很慢.

The next step was NuSOAP. This worked for quite a while. By the way, for God's sake, don't forget to cache WSDL! But even with WSDL cached users complained the damn thing is slow.

然后,我们决定采用裸 HTTP,使用 SimpleXMLElemnt 组合请求并读取响应,如下所示:

Then, we decided to go bare HTTP, assembling the requests and reading the responses with SimpleXMLElemnt, like this:

$request_info = array();

$full_response = @http_post_data(
    'http://example.com/OTA_WS.asmx',
    $REQUEST_BODY,
    array(
        'headers' => array(
            'Content-Type' => 'text/xml; charset=UTF-8',
            'SOAPAction'   => 'HotelAvail',
        ),
        'timeout' => 60,

    ),
    $request_info
);

$response_xml = new SimpleXMLElement(strstr($full_response, '<?xml'));

foreach ($response_xml->xpath('//@HotelName') as $HotelName) {
    echo strval($HotelName) . "
";
}

请注意,在 PHP 5.2 中,您将需要 pecl_http,因为(惊喜!)没有内置 HTTP 客户端.

Note that in PHP 5.2 you'll need pecl_http, as far as (surprise-surpise!) there's no HTTP client built in.

使用裸 HTTP 使我们的 SOAP 请求时间缩短了 30% 以上.从那时起,我们将所有性能投诉重定向到服务器人员.

Going to bare HTTP gained us over 30% in SOAP request times. And from then on we redirect all the performance complains to the server guys.

最后,我推荐后一种方法,而不是因为性能.我认为,一般来说,在像 PHP 这样的动态语言中,没有从所有 WSDL/类型控制中受益.您不需要花哨的库来读取和编写 XML,以及所有存根生成和动态代理.您的语言已经是动态的,SimpleXMLElement 工作得很好,而且非常易于使用.此外,您将拥有更少的代码,这总是好的.

In the end, I'd recommend this latter approach, and not because of the performance. I think that, in general, in a dynamic language like PHP there's no benefit from all that WSDL/type-control. You don't need a fancy library to read and write XML, with all that stubs generation and dynamic proxies. Your language is already dynamic, and SimpleXMLElement works just fine, and is so easy to use. Also, you'll have less code, which is always good.

这篇关于如何从 PHP 发布 SOAP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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