PHP SOAP XML请求问题

时间:2017-05-12 10:34:03

标签: php xml web-services soap soap-client

我在PHP中创建了SOAP XML请求,以便从另一个应用程序中获取一些数据。

我的PHP脚本是:

try {
    class AuthHeader {
        var $LoginName;
        var $Password;
        var $Culture;
        var $Version;

    function __construct($loginInfo) {
      $this->LoginName  = $loginInfo['LoginName'];
      $this->Password   = $loginInfo['Password'];
      $this->Culture    = $loginInfo['Culture'];
      $this->Version    = $loginInfo['Version'];
    }
}

// set current soap header with login info
$client = new SoapClient("http://demo-example.abc.com/hoteldata.svc?wsdl", array('trace' => TRUE));

// create header params array
$headerParams = array('LoginName'   => 'username',
                      'Password'    => 'password',
                      'Culture'     => 'en_US',
                      'Version'     => '7.123');

// create AuthHeader object
$auth = new AuthHeader($headerParams);

// Turn auth header into a SOAP Header
$header = new SoapHeader('http://schemas.abc.com/webservices/authentication', 'AuthenticationHeader', $auth, false);

// set the header
$client->__setSoapHeaders($header);

$b = new stdClass;
$b->HotelID = '3813';
$b->HotelID = '1322208';

$p = new stdClass;
$p->request->HotelIds            = array($b);

$quote = $client->GetHotelDetailsV3($p);

echo("<br />REQUEST :<br />" . htmlspecialchars($client->__getLastRequest()) . "<br/>");  
echo("<br />RESPONSE:<br />" .htmlspecialchars($client->__getLastResponse()) . "</pre>"); 
}
catch (SoapFault $ex)
{
    echo "Error:<br />" . nl2br($ex->faultcode) . '<br /><br />Error Details:<br />'. nl2br($ex->faultstring) . '<br />';  
    echo("<br />REQUEST :<br />" . htmlspecialchars($client->__getLastRequest()) . "<br/>");  
echo("<br /><span style=color:green;>RESPONSE:</span><br />" .htmlspecialchars($client->__getLastResponse()) . "<br />");
}

?>

但它显示错误:无法完成请求。 我认为我的代码中存在一些问题。

我需要为此服务生成的XML请求是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aut="http://schemas.example.com/webservices/authentication" xmlns:hot="http://example.com/webservices/hotelv3">
   <soapenv:Header>
      <aut:AuthenticationHeader>
         <aut:LoginName>John</aut:LoginName>
         <aut:Password>Johnpass</aut:Password>
         <aut:Culture>en_US</aut:Culture>
         <aut:Version>8</aut:Version>
      </aut:AuthenticationHeader>
   </soapenv:Header>
   <soapenv:Body>
      <hot:GetHotelDetailsV3>
         <hot:HotelIds>
            <hot:HotelID id="3813"/>
            <hot:HotelID id="1322208"/>
         </hot:HotelIds>
      </hot:GetHotelDetailsV3>
   </soapenv:Body>
</soapenv:Envelope>

我认为问题在于包含XML节点属性值。即,这些行:

<hot:HotelIds>
        <hot:HotelID id="3813"/>
        <hot:HotelID id="1322208"/>
     </hot:HotelIds>

我创建的这个xml的PHP脚本是:

$b = new stdClass;
$b->HotelID = '3813';
$b->HotelID = '1322208';

这是对的吗?什么是使用节点属性创建xml请求的最佳方法?请帮忙。谢谢。

0 个答案:

没有答案