我在使用php curl消费WCF Web服务(PaySimple的v3.00 API)时遇到问题。我试图将xml有效负载发布到服务,并且我不断收到400 - 错误请求错误。当我使用Firefox的Poster附加组件时,我可以发布相同的有效负载,并获得预期的响应 - 200 OK,包含所需响应的XML响应。这是我的代码片段:
$ch = curl_init();
$payload = $createPayload();
$length = strlen($payload);
$headers = array("Expect:");
if($isPost)
{
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$headers[] = "Content-length: $length";
}
else {
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
}
if($requestType == 'xml')
{
$headers[] = 'Content-Type: text/xml; charset: utf-8';
$headers[] = 'Accept: application/xml';
}
else {
$headers[] = 'Content-Type: application/json; charset: utf-8';
$headers[] = 'Accept: application/json';
}
$headers[] = "Connection: close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
//debugging
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$fh = fopen('curl_debug.txt', 'w');
curl_setopt($ch, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$ payload包含以下值:
<AddCustomer xmlns="http://api.paysimple.com"><userkey>APIUser1989</userkey><apikey>pxTbp4F6y7JuaMu36qazEw51JiqUA7lGme0j62r3UeQyNojvEfpNo7tn4pLq0yEULHz5KykHWV6AjNQJEyskCw0tXSKZ0vcP5mCynh1IgfQEH4NdXRkXeMNAGEdx6Ee0</apikey><customer xmlns="http://api.paysimple.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ApiConsumerData/><PsReferenceId>0</PsReferenceId><AltEmail>testemail@paysimpledev.com</AltEmail><AltPhone/><BillingAddress1>TestBillingAddress1</BillingAddress1><BillingAddress2/><BillingCity>TestBillingCity</BillingCity><BillingCountryCode/><BillingPostalCode>80202</BillingPostalCode><BillingState>6</BillingState><CompanyName>TestCompany</CompanyName><CreatedOn>0001-01-01T00:00:00</CreatedOn><Email>testemail@paysimpledev.com</Email><Fax/><FirstName>TestFirstName</FirstName><LastModified>0001-01-01T00:00:00</LastModified><LastName>TestLastName</LastName><MiddleName/><Notes/><Phone>5551234567</Phone><ShippingAddress1/><ShippingAddress2/><ShippingCity/><ShippingCountryCode/><ShippingPostalCode/><ShippingSameAsBilling>true</ShippingSameAsBilling><ShippingState>0</ShippingState><WebSite/></customer></AddCustomer>
以下是curl_debug.txt中显示的内容:
POST /3.00/paysimpleapi.svc/xml/listcustomers HTTP / 1.1
主持人:sandbox-api.paysimple.com
内容长度:1153
Content-Type:text / xml; charset:utf-8
接受:application / xml
连接:关闭
HTTP / 1.1 400错误请求
连接:关闭
日期:2010年8月27日星期五,格林威治标准时间11:07:21
服务器:Microsoft-IIS / 6.0
X-Powered-By:ASP.NET
X-AspNet-Version:2.0.50727
Set-Cookie:ASP.NET_SessionId = oxpese454u4rr3552evybjue;路径= /;仅Http
缓存控制:私人
内容长度:0
关闭连接#0
F1! F1!
答案 0 :(得分:0)
您的POST内容类型应该是application / x-www-form-urlencoded或multipart / form-data,而不是application / xml
答案 1 :(得分:0)
问题可能出在其中之一:
Content-Type: text/xml; charset: utf-8
Accept: application/xml
为两者尝试text / xml或application / xml,尝试使用大写的UTF-8。尝试发送与Firefox相同的标题。
答案 2 :(得分:0)
塔什
发布到https://sandbox-api.paysimple.com/3.00/PaysimpleApi.svc/xml/addcustomer http / 1.1
使用标题: 内容长度:1262 内容类型:application / xml;字符集= utf-8的 接受:application / xml 方法:invokeMethod UserAgent:PaySimple 主持人:sandbox-api.paysimple.com
尝试请求 SandboxMelody qUWzSPxkcxDR6RJiR2ypzbznV4m6jva5N8kaosnNqQJYqNTGcVouWhKbN5NPGiZxJdmx2QnapYNcwq1bjtfmGwLhBcdMT6x3z7IdPVYA3H7HDBUqwW6eJDyaf8dhKqr0 0 1122 Boogie Woogie Avenue#6City 801116CompanyName99999 0001-01-01T00:00:00w@w.comTestUser0001-01-01T00:00:00 LastName这是一个新客户......来自Fiddler3035551212 1122 Boogie Woogie Avenue#5City 80111true6
在帖子网址和请求正文中使用ADDCUSTOMER