如何在php中创建soap XML请求


how to create soap xml request in php

<?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。我有站点id,用户名,密码。

下面的字符串
<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 xml在PHP中的响应。以上xml是ARN(联盟保留)

如果您只想发送一个准备好的原始xml请求,调用webservices非常容易。例如,您可以使用CURL。

这里是使用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
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()}";
    }
}

?>

谢谢

php有一个内置的Soap客户端: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));

您可以尝试使用__doRequest函数来修改请求。

<?php
require_once("MySoapClient.php");
$client = new MySoapClient($wsdUrl,array(
                                       'location' => "http://tripauthority.com/hotel",
                                       'trace'    => 1,
                                       'cache_wsdl' =>  WSDL_CACHE_NONE
                                       )
                         );
$parameters=array('siteID'=>'string','username'=>'string','password'=>'string');
$err=0;
try{
    $info = $client->__soapCall("SubmitRequest",array($parameters));
}
catch (SoapFault $e) {
    echo "<pre>faultcode: '".$e->faultcode."'</pre>";
    echo "<pre>faultstring: '".$e->getMessage()."'</pre>";
    $err=1;
}
if($err==0)
    print_r($info);
else
    echo $client->__getLastRequest();
?>

MySoapClient.php

<?php
class MySoapClient extends SoapClient
{
    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        $request=str_replace('</SubmitRequest>','<xmlFormattedString>string</xmlFormattedString></SubmitRequest>',$request);
        return $request;
    }
}
?>