如何在php中创建soap xml请求[英] how to create soap xml request in php

本文是小编为大家收集整理的关于如何在php中创建soap xml请求的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

<?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>
    <SubmitRequest xmlns="http://tripauthority.com/hotel">
      <siteID>string</siteID>
      <username>string</username>
      <password>string</password>
      <xmlFormattedString>string</xmlFormattedString>
    </SubmitRequest>
  </soap:Body>
</soap:Envelope>

我们必须调用以上SOAP XML.我有SiteId,用户名,密码. 其中字符串低于

<ArnRequest><Availability DisplayCurrency="USD" SearchTimeout="15"><HotelAvailability InDate="2007-04-26" OutDate="2007-04-29" Rooms="1" Adults="2" Children="0"><Hotel HotelID="8800"/></HotelAvailability></Availability></ArnRequest>

我对SOAP请求不知道.请帮助此帮助以获得PHP中的SOAP XML上面的响应.以上XML是ARN(联盟保留) 提前谢谢.

推荐答案

我已经解决了我的问题如果未来任何身体有任何问题,他们这段代码适用于他,请检查 -

<?php
error_reporting(E_ALL);

define('API_SITEID',    $your_siteid);
define('API_USERNAME',  $your_uname);
define('API_PASSWORD',  $your_pass);
define('API_WSDL',      'http://tripauthority.com/hotel.asmx?WSDL');
ini_set("soap.wsdl_cache_enabled", "0");

$xmlReq = '<ArnRequest>
<Availability DisplayCurrency="USD" SearchTimeout="15">
    <HotelAvailability InDate="2014-09-26" OutDate="2014-09-27" Rooms="1" Adults="1" Children="0">
    <Hotel HotelID="8800"/>
    </HotelAvailability>
</Availability>
</ArnRequest>';
echo '<form action="" method="post">
    <strong>XML Request:</strong>
    <p>
    <textarea style="width:100%;height:400px;" id="xmlReq" name="xmlReq">'.$xmlReq.'</textarea>
    </p>
    <input type="submit" name="submit" id="submit" value="Test Request">
    <input type="hidden" name="avail" id="avail" value="y">
</form>';

if($_POST['avail'] == "y") {
    $xmlRes = doSoapRequest((($_POST['xmlReq']) ? $_POST['xmlReq'] : $xmlReq));
    echo '<strong>XML Response:</strong>
    <p>
    <textarea style="width:100%;height:400px;" id="xmlRes" name="xmlRes">'.$xmlRes.'</textarea>
    </p>';
}

function doSoapRequest($xmlReq) {
        try {
    $client = new SoapClient(API_WSDL);
    return $client->SubmitRequestRpc(API_SITEID, API_USERNAME, API_PASSWORD, $xmlReq);
    } catch(SoapFault $exception) {
    return "Fault Code: {$exception->getMessage()}";
    }
}

?>

感谢

其他推荐答案

调用webservices非常简单,如果您只想发送准备好的原始XML请求.例如,你可以使用卷曲.

此处使用php soapclient的代码.我得到"无效凭据",但这应该是正常,因为你将有效的那里放在那里.

<?

$string ='<ArnRequest><Availability DisplayCurrency="USD" SearchTimeout="15"><HotelAvailability InDate="2007-04-26" OutDate="2007-04-29" Rooms="1" Adults="2" Children="0"><Hotel HotelID="8800"/></HotelAvailability></Availability></ArnRequest>';

$xmlrequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hot="http://tripauthority.com/hotel">
   <soapenv:Header/>
   <soapenv:Body>
      <hot:SubmitRequestDoc>
         <!--Optional:-->
         <hot:siteID>string</hot:siteID>
         <!--Optional:-->
         <hot:aUserName>string</hot:aUserName>
         <!--Optional:-->
         <hot:aPassword>string</hot:aPassword>
         <!--Optional:-->
         <hot:aRequestDoc>
'.$string.'
</hot:aRequestDoc>
      </hot:SubmitRequestDoc>
   </soapenv:Body>
</soapenv:Envelope>';


//Change this variables.
$location_URL = 'http://tripauthority.com/hotel.asmx';
$action_URL = "http://tripauthority.com/hotel/SubmitRequestDoc";

$client = new SoapClient(null, array(
'location' => $location_URL,
'uri'      => "http://tripauthority.com/hotel",
'trace'    => 1,
));

$order_return = $client->__doRequest($xmlrequest,$location_URL,$action_URL,1);

//Get response from here
print_r($order_return);

?>

其他推荐答案

php有一个内置的soap客户端,as 5: http://php.net/manual/en/class.soapclient.php

$wsdl = '
<?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>
    <SubmitRequest xmlns="http://tripauthority.com/hotel">
      <siteID>string</siteID>
      <username>string</username>
      <password>string</password>
      <xmlFormattedString>string</xmlFormattedString>
    </SubmitRequest>
  </soap:Body>
</soap:Envelope>
';

try {

     $client = @new SOAPClient($wsdl);  // or preferably, use a url for $wsdl

    // Be sure to replace soapMethodToUse with a mouthed for this specific web service.
    $response = $client->soapMethodToUse(array('key' => 'val')); // Any params for this method

} catch (Exception $e) {  

    echo $e->getMessage(); 

}

die(var_dump($response));

本文地址:https://www.itbaoku.cn/post/1937804.html

问题描述

<?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>
    <SubmitRequest xmlns="http://tripauthority.com/hotel">
      <siteID>string</siteID>
      <username>string</username>
      <password>string</password>
      <xmlFormattedString>string</xmlFormattedString>
    </SubmitRequest>
  </soap:Body>
</soap:Envelope>

we have to call above soap xml. i have siteID, username, password. where string is below

<ArnRequest><Availability DisplayCurrency="USD" SearchTimeout="15"><HotelAvailability InDate="2007-04-26" OutDate="2007-04-29" Rooms="1" Adults="2" Children="0"><Hotel HotelID="8800"/></HotelAvailability></Availability></ArnRequest>

i have no idea on soap request. Please help with this to get response on above soap xml in PHP. The above xml is of ARN(Alliance reservations) thanks in advance.

推荐答案

I have solve my question if any body in future have any problem they this code works for him, please check-

<?php
error_reporting(E_ALL);

define('API_SITEID',    $your_siteid);
define('API_USERNAME',  $your_uname);
define('API_PASSWORD',  $your_pass);
define('API_WSDL',      'http://tripauthority.com/hotel.asmx?WSDL');
ini_set("soap.wsdl_cache_enabled", "0");

$xmlReq = '<ArnRequest>
<Availability DisplayCurrency="USD" SearchTimeout="15">
    <HotelAvailability InDate="2014-09-26" OutDate="2014-09-27" Rooms="1" Adults="1" Children="0">
    <Hotel HotelID="8800"/>
    </HotelAvailability>
</Availability>
</ArnRequest>';
echo '<form action="" method="post">
    <strong>XML Request:</strong>
    <p>
    <textarea style="width:100%;height:400px;" id="xmlReq" name="xmlReq">'.$xmlReq.'</textarea>
    </p>
    <input type="submit" name="submit" id="submit" value="Test Request">
    <input type="hidden" name="avail" id="avail" value="y">
</form>';

if($_POST['avail'] == "y") {
    $xmlRes = doSoapRequest((($_POST['xmlReq']) ? $_POST['xmlReq'] : $xmlReq));
    echo '<strong>XML Response:</strong>
    <p>
    <textarea style="width:100%;height:400px;" id="xmlRes" name="xmlRes">'.$xmlRes.'</textarea>
    </p>';
}

function doSoapRequest($xmlReq) {
        try {
    $client = new SoapClient(API_WSDL);
    return $client->SubmitRequestRpc(API_SITEID, API_USERNAME, API_PASSWORD, $xmlReq);
    } catch(SoapFault $exception) {
    return "Fault Code: {$exception->getMessage()}";
    }
}

?>

Thanks

其他推荐答案

Calling webservices is quite easy, if you just want to send a prepared raw xml request. You could for instance use CURL for this.

Here the code which uses the php soapclient. I get "invalid credentials", but this should be ok as you'd put your valid ones in there.

<?

$string ='<ArnRequest><Availability DisplayCurrency="USD" SearchTimeout="15"><HotelAvailability InDate="2007-04-26" OutDate="2007-04-29" Rooms="1" Adults="2" Children="0"><Hotel HotelID="8800"/></HotelAvailability></Availability></ArnRequest>';

$xmlrequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hot="http://tripauthority.com/hotel">
   <soapenv:Header/>
   <soapenv:Body>
      <hot:SubmitRequestDoc>
         <!--Optional:-->
         <hot:siteID>string</hot:siteID>
         <!--Optional:-->
         <hot:aUserName>string</hot:aUserName>
         <!--Optional:-->
         <hot:aPassword>string</hot:aPassword>
         <!--Optional:-->
         <hot:aRequestDoc>
'.$string.'
</hot:aRequestDoc>
      </hot:SubmitRequestDoc>
   </soapenv:Body>
</soapenv:Envelope>';


//Change this variables.
$location_URL = 'http://tripauthority.com/hotel.asmx';
$action_URL = "http://tripauthority.com/hotel/SubmitRequestDoc";

$client = new SoapClient(null, array(
'location' => $location_URL,
'uri'      => "http://tripauthority.com/hotel",
'trace'    => 1,
));

$order_return = $client->__doRequest($xmlrequest,$location_URL,$action_URL,1);

//Get response from here
print_r($order_return);

?>

其他推荐答案

php has a built in Soap Client as of 5: http://php.net/manual/en/class.soapclient.php

$wsdl = '
<?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>
    <SubmitRequest xmlns="http://tripauthority.com/hotel">
      <siteID>string</siteID>
      <username>string</username>
      <password>string</password>
      <xmlFormattedString>string</xmlFormattedString>
    </SubmitRequest>
  </soap:Body>
</soap:Envelope>
';

try {

     $client = @new SOAPClient($wsdl);  // or preferably, use a url for $wsdl

    // Be sure to replace soapMethodToUse with a mouthed for this specific web service.
    $response = $client->soapMethodToUse(array('key' => 'val')); // Any params for this method

} catch (Exception $e) {  

    echo $e->getMessage(); 

}

die(var_dump($response));