我在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中,它将编码设置为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
找到答案 0 :(得分:1)
问题在于标题。
<soap:Header>
开场&amp;结束标记导致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>');