PHP Nusoap - SOAP Request not working

女生的网名这么多〃 提交于 2019-12-11 06:28:49

问题


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

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

Questions:

  1. How to pass headers?
  2. How to pass FetchCalendarRequest with request like in XML?

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

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

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

<?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;
}

回答1:


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:

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

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




回答2:


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



回答3:


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.

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

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.

You have also listed the WSDL document as http://webservices.test.com/ows/5.1/Availability.wsdl, which is also an invalid 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

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



回答4:


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:

  1. How to pass headers?
  2. How to pass FetchCalendarRequest with request like in XML?

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

  • NuSoap PHP webservice with soap headers

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:

  • Nusoap use existing WSDL how to?

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.



来源:https://stackoverflow.com/questions/18125751/php-nusoap-soap-request-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!