繁体   English   中英

PHP NuSoap中的XML请求-“ HTTP / 1.1 400错误请求”

[英]XML request in PHP NuSoap - “HTTP/1.1 400 Bad Request”

我在PHP中使用NuSoap连接到API。

目前,我只是尝试发送由提供API的公司提供的XML字符串示例,但是我收到了HTTP/1.1 400 Bad Request错误。

我直接向公司查询,但是他们说,当他们使用提供的XML以及其他客户时,他们没有任何问题。

我尝试发送的XML是:

![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<Service>
  <Name><![CDATA[Demo]]></Name>
  <Description><![CDATA[Demo Service]]></Description>
  <Version>1.0</Version>
  <Nodes>
    <Node NodeType="Start" NodeID="5" Description="">
      <!-- Comments can be included -->
      <MandatoryProperties>
        <Property Name="MaxLength" Value="0"/>
        <Property Name="EnableSpeedDial" Value="False"/>
      </MandatoryProperties>
     <OptionalSettings/>
      <Branches>
        <Branch Name="Continue" NodeID="20" Description=""/>
      </Branches>
      <DynamicBranches/>
      <DynamicProperties/>
    </Node>
    <Node NodeType="DeliverCall" NodeID="20" Description="Home Phone">
      <MandatoryProperties>
        <Property Name="DestinationNumber" Value="01132211444"/>
        <Property Name="RingDuration" Value="60"/>
      </MandatoryProperties>
      <OptionalSettings/>
      <Branches>
      </Branches>
      <DynamicBranches/>
      <DynamicProperties/>
    </Node>
  </Nodes>
</Service>]]

我得到的答复是:

Response:
HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Date: Fri, 24 Jan 2014 15:47:16 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET 2.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 2
Connection: close

我已经使用http://www.validome.org/xml/validate/来验证XML,因为我对XML不熟悉,这是我不得不使用它的几次,所以我不会出现语法错误,但验证工具提供的错误是:

XML错误

我检查了XML是否没有空格前后。

我注意到的是,在XML中,它将编码设置为utf-8 但是,我的请求中的编码似乎是ISO -尽管看起来好像XML字符串中没有任何特殊字符或任何东西。

请求数据:

POST /dev/aspapi/api.asmx HTTP/1.0
Host: www.xxxxx.com
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "https://www.xxxxx.com/ASPAPI/ValidateService"
Authorization: Basic bWVkaWFoYXdrOm1lZGlhaGF3aw==
Content-Length: 2127

如果有人能发现任何问题,我将不胜感激!

完整的连接脚本:

<?php

require_once('./nusoap.php');

error_reporting(E_ALL);
ini_set("display_errors", true);

$client = new nusoap_client('http://xxx.xxx/api.asmx', 'wsdl');

$client->setCredentials('xxxxx', 'xxxxx'); 
$client->response_timeout = 120;

$err = $client->getError();

if($err) {
    echo 'ERROR<br /><pre>' . $err . '</pre><br />';
    exit;
} else {
    echo "<strong><em>Sucessfull connection</em></strong><br /><br />";
}

$proxy = $client->getProxy();

$proxy->setHeaders('<soap:Header><AuthenticationSoapHeader xmlns="https://www.xxxxx.com/aspapi"><Token>xxxxx</Token></AuthenticationSoapHeader></soap:Header>');

$xml = '
![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<Service>
  <Name><![CDATA[Demo]]></Name>
  <Description><![CDATA[Demo Service]]></Description>
  <Version>1.0</Version>
  <Nodes>
    <Node NodeType="Start" NodeID="5" Description="">
      <!-- Comments can be included -->
      <MandatoryProperties>
        <Property Name="MaxLength" Value="0"/>
        <Property Name="EnableSpeedDial" Value="False"/>
      </MandatoryProperties>
     <OptionalSettings/>
      <Branches>
        <Branch Name="Continue" NodeID="20" Description=""/>
      </Branches>
      <DynamicBranches/>
      <DynamicProperties/>
    </Node>
    <Node NodeType="DeliverCall" NodeID="20" Description="Home Phone">
      <MandatoryProperties>
        <Property Name="DestinationNumber" Value="01132211444"/>
        <Property Name="RingDuration" Value="60"/>
      </MandatoryProperties>
      <OptionalSettings/>
      <Branches>
      </Branches>
      <DynamicBranches/>
      <DynamicProperties/>
    </Node>
  </Nodes>
</Service>]]';

$result = $proxy->call('ValidateService', array('serviceXML' => $xml));

$err = $proxy->getError();
if($err) {
    echo 'ERROR<br /><pre>' . $err . '</pre><br />';
    echo '<strong>Request:</strong>';
    echo '<pre>'; print_r($proxy->request); echo '</pre>';
    echo '<strong>Response:</strong>';
    echo '<pre>'; print_r($proxy->response); echo '</pre>';
    exit;
} else {
    echo "<strong><em>Sucessfull connection</em></strong><br /><br />";
}
?>

上面脚本的输出:

Sucessfull connection

ERROR
HTTP Error: Unsupported HTTP response status 400 Bad Request (soapclient->response has contents of the response)

Request:
POST /dev/aspapi/api.asmx HTTP/1.0
Host: www.xxxxx.com
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "https://www.xxxxx.com/ASPAPI/ValidateService"
Authorization: Basic bWVkaWFoYXdrOm1lZGlhaGF3aw==
Content-Length: 2127

<TOKEN IS HERE IN OUTPUT>
![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<Service>
  <Name><![CDATA[Demo]]></Name>
  <Description><![CDATA[Demo Service]]></Description>
  <Version>1.0</Version>
  <Nodes>
    <Node NodeType="Start" NodeID="5" Description="">
      <!-- Comments can be included -->
      <MandatoryProperties>
        <Property Name="MaxLength" Value="0"/>
        <Property Name="EnableSpeedDial" Value="False"/>
      </MandatoryProperties>
     <OptionalSettings/>
      <Branches>
        <Branch Name="Continue" NodeID="20" Description=""/>
      </Branches>
      <DynamicBranches/>
      <DynamicProperties/>
    </Node>
    <Node NodeType="DeliverCall" NodeID="20" Description="Home Phone">
      <MandatoryProperties>
        <Property Name="DestinationNumber" Value="01132211444"/>
        <Property Name="RingDuration" Value="60"/>
      </MandatoryProperties>
      <OptionalSettings/>
      <Branches>
      </Branches>
      <DynamicBranches/>
      <DynamicProperties/>
    </Node>
  </Nodes>
</Service>]]

Response:

HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Date: Fri, 24 Jan 2014 16:28:39 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET 2.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 2
Connection: close

附加信息

到目前为止还算不上好运,可以在http://pastebin.com/hF3DYGWP上找到调试字符串的输出。

问题出在标题上。

<soap:Header>开头和结尾标记导致HTTP Error 400 "Bad Request"

$proxy->setHeaders('<soap:Header><AuthenticationSoapHeader xmlns="https://www.xxxxx.com/aspapi"><Token>xxxxx</Token></AuthenticationSoapHeader></soap:Header>');

应该

$proxy->setHeaders('<AuthenticationSoapHeader xmlns="https://www.xxxxx.com/aspapi"><Token>xxxxx</Token></AuthenticationSoapHeader>');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM