PHP Nusoap - SOAP 请求不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18125751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:01:56  来源:igfitidea点击:

PHP Nusoap - SOAP Request not working

phpweb-servicesnusoap

提问by Bhavin Rana

Hello i am very new to webservice, in php with below code i am trying to make soap request as shown in below XML, but it says Error

您好,我对 webservice 非常陌生,在 php 中使用以下代码我正在尝试发出如下 XML 所示的soap请求,但它说错误

HTTP Error: Unsupported HTTP response status 405 Method Not Allowed (soapclient->response has contents of the response)

HTTP 错误:不支持的 HTTP 响应状态 405 Method Not Allowed(soapclient->response has content of the response)

Questions:

问题:

  1. How to pass headers?
  2. How to pass FetchCalendarRequest with request like in XML?
  1. 如何传递标题?
  2. 如何像在 XML 中一样通过请求传递 FetchCalendarRequest?

I have used Nusoap here but if you have a SOAP PHP class solution it is also invited.

我在这里使用了 Nusoap 但如果你有一个 SOAP PHP 类解决方案,它也会被邀请。

My code:

我的代码:

<?php

require_once('../lib/nusoap.php');

$client = new nusoap_client("http://webservices.test.com/ows/5.1/Availability.wsdl");
$err    = $client->getError();

if ($err)
{
    client_debug_error_message('Constructor error', $err, $client);
    exit;
}

// Call the SOAP method
$result = $client->call(
    'FetchCalendar', 
    array(
        'StayDateRange' => array(
            'StartDate' => '2013-10-01', 
            'EndDate'   => '2013-10-10',
        ),
    ),
);

// Check for a fault
if ($client->fault) 
{
    debug_preformatted('Fault', $result);
} 
else 
{
    // Check for errors
    $err = $client->getError();

    if ($err) 
    {
        debug_preformatted('Error', $err);
    }
    else 
    {
        debug_preformatted('Result', $result);
    }
}

// Display the request and response
client_debug_dump($client);

XML:

XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <OGHeader transactionID="005435" timeStamp="2008-12-09T13:26:56.4056250-05:00" xmlns="http://webservices.test.com/og/4.3/Core/">
      <Origin entityID="OWS" systemType="WEB" />
      <Destination entityID="WEST" systemType="ORS" />
    </OGHeader>
  </soap:Header>
  <soap:Body>
    <FetchCalendarRequest xmlns:a="http://webservices.test.com/og/4.3/Availability/" xmlns:hc="http://webservices.test.com/og/4.3/HotelCommon/" xmlns="http://webservices.test.com/ows/5.1/Availability.wsdl">
      <HotelReference chainCode="AXA" hotelCode="AXAMUM" />
      <StayDateRange>
        <hc:StartDate>2013-10-01</hc:StartDate>
        <hc:EndDate>2013-10-10</hc:EndDate>
      </StayDateRange>
      <GuestCount>
        <hc:GuestCount ageQualifyingCode="ADULT" count="1" />
        <hc:GuestCount ageQualifyingCode="CHILD" count="0" />
      </GuestCount>
    </FetchCalendarRequest>
  </soap:Body>
</soap:Envelope>


    Post Url :http://000.000.000.00:8080/ows_ws_51/Availability.asmx 
Soap Action :  http://webservices.test.com/ows/5.1/Availability.wsdl#FetchCalendar

Edit:Working Solution 16 Sep 2013

编辑:工作解决方案 2013 年 9 月 16 日

This solution is with Soap PHP Class only I want it to work with Nusoap bow.

此解决方案仅适用于 Soap PHP Class,我希望它与 Nusoap 弓一起使用。

<?php

$wsdl   = "http://###.###.###.##:8080/ows_ws_51/Availability.asmx?wsdl"; 
$client = new SoapClient($wsdl, array(  'soap_version' => SOAP_1_1,'trace' => true,)); 

//=========== Header Setting ============

$ns                         = 'http://webservices.micros.com/og/4.3/Availability/'; //Namespace of the WS.//Body of the Soap Header.
$strHeaderComponent_Session = <<<XML
<OGHeader transactionID="005435" timeStamp="2008-12-09T13:26:56.4056250-05:00" xmlns="http://webservices.micros.com/og/4.3/Core/">
  <Origin entityID="OWS" systemType="WEB" />
  <Destination entityID="WEST" systemType="ORS" />
</OGHeader>
XML;
$objVar_Session_Inside      = new SoapVar($strHeaderComponent_Session, XSD_ANYXML, null, null, null);
$objHeader_Session_Outside  = new SoapHeader($ns , 'SessionHeader', $objVar_Session_Inside);

// More than one header can be provided in this array.
$client->__setSoapHeaders(array($objHeader_Session_Outside));

//============== Request ================

$xml = <<<XML
<FetchCalendarRequest xmlns:a="http://webservices.micros.com/og/4.3/Availability/" xmlns:hc="http://webservices.micros.com/og/4.3/HotelCommon/" xmlns="http://webservices.micros.com/ows/5.1/Availability.wsdl">
  <HotelReference chainCode="AXA" hotelCode="{$DdlHotels}" />
  <StayDateRange>
    <hc:StartDate>{$Arrive}</hc:StartDate>
    <hc:EndDate>{$Depart}</hc:EndDate>
  </StayDateRange>
  <GuestCount>
    <hc:GuestCount ageQualifyingCode="ADULT" count="1" />
    <hc:GuestCount ageQualifyingCode="CHILD" count="0" />
  </GuestCount>
</FetchCalendarRequest>
XML;

$args = array(new SoapVar($xml, XSD_ANYXML));

try 
{

    $response = $client->__soapCall( 'FetchCalendar', $args );
}
catch (SoapFault $e) 
{
    echo "Error: {$e}"; exit;
}

回答by quaspas

You can use PHP's built in SOAP library to create a SOAP client and call a method from the WSDL, try something like this:

您可以使用 PHP 内置的 SOAP 库来创建 SOAP 客户端并从 WSDL 调用方法,尝试如下操作:

$client = new SoapClient($wsdl, array(  'soap_version' => SOAP_1_1,
                                        'trace' => true,
                                        )); 
try {
$params = array(
         //Your parameters here
          );    
$res = $client->__soapCall( 'SoapMethod', $params );
return $res;
} catch (SoapFault $e) {
echo "Error: {$e}";
}

//for debugging what the outgoing xml looks like
$client->__getLastRequest();

The WSDL should help structure the xml for your params. If you cannot get that to work the way you want you could try passing the xml yourself using SoapVar() and setting the encode to XSD_ANYXML.

WSDL 应该有助于为您的参数构建 xml。如果你不能让它按照你想要的方式工作,你可以尝试使用SoapVar()自己传递 xml并将编码设置为 XSD_ANYXML。

If you also need to add additional header information take a look at this example from the PHP docs.

如果您还需要添加其他标题信息,请查看 PHP 文档中的此示例。

回答by Pranay Bhardwaj

$params = array(//your params as specified by documentation);
$result = $client->call(array("Availability"=>$params));

回答by Rick Suggs

Might I suggest trying to learn SOAP with a different example web service. The wsdl document published for this service appears to be incomplete, as they have entered the incorrect SOAP address for the Availability service. With an incorrect or incomplete WSDL document, the SOAP library you are using will not be able to form a valid SOAP request and send it to the correct endpoint.

我是否建议尝试使用不同的示例 Web 服务来学习 SOAP。为此服务发布的 wsdl 文档似乎不完整,因为他们为可用性服务输入了错误的 SOAP 地址。对于不正确或不完整的 WSDL 文档,您使用的 SOAP 库将无法形成有效的 SOAP 请求并将其发送到正确的端点。

<wsdl:service name="AvailabilityService">
    <wsdl:port name="AvailabilityPort" binding="tns:AvailabilityBinding">
        <soap:address location="http://tempuri.org"/>
    </wsdl:port>
</wsdl:service>

As you can see, the AvailabilityService endpoint is described as http://tempuri.org, which is explained as a default test namespace here.

正如你所看到的,AvailabilityService端点被描述为http://tempuri.org,这是一个默认的测试命名解释这里

In your original post, you have the 'POST URL' described as http://000.000.000.00:8080/ows_ws_51/Availability.asmx. How is anyone supposed to try the example, if you list the 'POST URL' an as obviously invalid URL.

在您的原始帖子中,您将“POST URL”描述为http://000.000.000.00:8080/ows_ws_51/Availability.asmx。如果您将“POST URL”列为明显无效的 URL,那么任何人都应该如何尝试该示例。

You have also listed the WSDL document as http://webservices.test.com/ows/5.1/Availability.wsdl, which is also an invalid URL.

您还将 WSDL 文档列为http://webservices.test.com/ows/5.1/Availability.wsdl,这也是一个无效的 URL。

Here is a PHP example, accessing a valid WSDL document, using PHP's built-in SOAP library, which can be enabled by enabling the php_soap.dll extension in your php.ini file.

这是一个 PHP 示例,它使用 PHP 的内置 SOAP 库访问有效的 WSDL 文档,该库可以通过在 php.ini 文件中启用 php_soap.dll 扩展来启用。

<?php

    $wsdl = "http://www.restfulwebservices.net/wcf/StockQuoteService.svc?wsdl";

    $client = new SoapClient($wsdl, array(
        "trace"      => 1,
        "exceptions" => 0));

    $parameters = array("request" => "IBM");

    $value = $client->GetStockQuote($parameters);

    $json = json_encode($value);

    echo $json;

回答by hakre

Note:The following is more of a comment than an answer. I leave it here for further reference as it would not fit into a comment box and it references existing Q&A possible worth for future visitors

注意:以下内容更多的是评论而不是答案。我将它留在这里以供进一步参考,因为它不适合评论框,并且它引用了可能对未来访问者有价值的现有问答

You ask two questions regarding Nusoap here:

你在这里问两个关于 Nusoap 的问题:

  1. How to pass headers?
  2. How to pass FetchCalendarRequest with request like in XML?
  1. 如何传递标题?
  2. 如何像在 XML 中一样通过请求传递 FetchCalendarRequest?

The first one How to pass headers?has been outlined already in this Q&A:

第一个如何传递标题?已在此问答中概述:

As you have not written specifically in your question what of this (and possible other of the) existing Q&A didn't work for you, this might not suit your needs but you need to give detailed feedback first I'd say.

由于您没有在您的问题中具体写下哪些(以及可能的其他)现有问答对您不起作用,这可能不适合您的需求,但您需要先提供详细的反馈,我会说。

The second question How to pass FetchCalendarRequest with request like in XML?you probably mean how to call a SOAP action or method ("Request") that has been named in a WSDL. This has already been covered as well for Nusoap on the Stackoverflow website:

第二个问题如何像在 XML 中一样通过请求传递 FetchCalendarRequest?您可能指的是如何调用已在 WSDL 中命名的 SOAP 操作或方法(“请求”)。Stackoverflow 网站上的 Nusoap 也已经涵盖了这一点:

As you have not written specifically in your question what of this (and possible other of the) existing Q&A didn't work for you, this might not suit your needs but you need to give detailed feedback first I'd say to turn this into a concrete programming question.

由于您没有在您的问题中具体写下哪些(以及可能的其他)现有问答对您不起作用,这可能不适合您的需求,但您需要先提供详细的反馈,我会说将其变成一个具体的编程问题。

BTW Stackoverflow works best by asking one question at a time.

顺便说一句,Stackoverflow 最好一次问一个问题。