2011-10-13 73 views
1

我正在使用BlueHornet API发送电子邮件,具体为legacy.send_campaign方法。我更新客户现有的API调用,并已指示通过POST发送的PHP未通过PHP发送

"POST the XML message using the arguments specified below. Be sure to include the <send> element and set its value to 'Y'. The POST response message will include a <message_id> element and a <message_key> element.

这是所提供的格式:

<api> 
    <authentication> 
     <api_key>ClientAPIKey</api_key> 
     <shared_secret>ClientSharedSecret</shared_secret> 
     <response_type>xml</response_type> 
    </authentication> 
    <data> 
     <methodCall> 
      <methodName>legacy.send_campaign</methodName> 
      <grp>ClientEmailGroupCode</grp> 
      <rich_mbody><![CDATA[<html...LONG HTML BLOCK...</html>]]></rich_mbody> 
      <text_mbody><![CDATA[...LONG TEXT BODY...]]></text_mbody> 
      <reply_email>ClientReplyEmail</reply_email> 
      <from_email>ClientFromEmail</from_email> 
      <fromdesc>ClientFromName</fromdesc> 
      <msubject>ClientEmailSubject</msubject> 
      <send>Y</send> 
      <track_links>1</track_links> 
     </methodCall> 
    </data> 
</api> 

的API是密码保护的,但我可以根据要求博士后的。

我已经证实了ClientAPIKeyClientSharedSecret,并ClientEmailGroupCode是有效的,但我的测试一直未果。发送以下内容(使用的fsockopen() FWIW):

POST /api/xmlrpc/index.php HTTP/1.0 
Host: echoN.bluehornet.com 
Content-Type: text/xml;charset=utf-8 
Content-Length: 21551 

<?xml version="1.0"?> 
<api> 
    <authentication>...as above...</authentication> 
    <data>...as above...</data> 
</api> 

导致第三方服务器发送回这个XML响应指示错误:增加了可读性

HTTP/1.1 200 OK 
Date: Thu, 13 Oct 2011 13:18:50 GMT 
Server: Apache/1.3.41 (Unix) 
Cache-Control: max-age=18000 
Expires: Thu, 13 Oct 2011 18:18:50 GMT 
Connection: close 
Content-Type: text/xml;charset=utf-8 
Set-Cookie: BIGipServerBH-gen-80=387268618.20480.0000; path=/ 

<!--?xml version="1.0" encoding="utf-8"?--> 
<methodresponse><item><error><!--[CDATA[1]]--></error> 
<responsetext><!--[CDATA[No XML Data Passed.]]--></responsetext> 
<responsedata><responsenum><!--[CDATA[1]]--></responsenum> 
<totalrequests><!--[CDATA[0]]--></totalrequests> 
<totalcompleted><!--[CDATA[0]]--></totalcompleted> 
</responsedata></item></methodresponse> 

换行符。

“没有XML数据通过”的responseText。关注我,所以我联系了供应商,并被告知要仔细检查我的URL(http与https,正确的N在echo * N * .bluehornet ...等),并确保我发布了“数据“参数。建议是放置“数据=?”在查询URL的HTTP报头的末尾:

POST /api/xmlrpc/index.php?data= HTTP/1.0 

或前面加上“数据=”到XML块在请求体:

data=<?xml version="1.0"?> 

这似乎是由一个.NET姊妹项目编写的代码暗示:

private void ConstructData() 
{ 
    data.Append("data="); 
    data.Append("<api>"); 
    data.Append("<authentication>"); 
    data.Append(authenticationData.ToString()); 
    data.Append("</authentication>"); 
    data.Append("<data><methodCall>"); 
    data.Append(methodCallData.ToString()); 
    data.Append("</methodCall></data>"); 
    data.Append("</api>"); 
} 

修改查询网址没有效果,而XML块前面放置“数据=”引起了长时间的加载时间之后没有服务器respo NSE。

此时,供应商正试图挖掘一位开发人员,他之前曾使用API​​来权衡这个问题。与此同时,我想我会分享上述内容,看看是否有人可以指出任何问题–细微的疏忽或明显遗漏,因为他们可能是–这可能会导致我的XML数据不能发送。

+0

第三方供应商终于找回了他们现有代码的修订版本,它使用一个完全不同的方法(基于cURL)。看起来,即使供应商在上述技术方面也存在问题。 –

回答

2

如果真的后应该像数据= XXX不是你应该与此头

Content-Type: application/x-www-form-urlencoded 

并与后机身发送POST数据,那么,URL编码:)

+0

感谢@Slartibartfast的帮助我曾尝试过“application/x-www-form-urlencoded”以及带有和不带charset信息的“text/xml”,但都没有成功。甚至发现供应商在使用此API发布时遇到问题。我们将采用建议的cURL方法,现在就放弃这一点。 –

1

这工作。是时候到了这里。最后意识到?data =是在有效载荷中传递的字段。

import urllib 

from google.appengine.api import urlfetch 

url = "https://echoN.bluehornet.com/api/xmlrpc/index.php" 


    XML="""<api> 
    <authentication> 
    <api_key>000000000000000000</api_key> 
    <shared_secret>00000000</shared_secret> 
    <response_type>xml</response_type> 
    </authentication> 
    <data> 
    <methodCall> 
    <methodName>legacy.retrieve_active</methodName> 
    <email>[email protected]</email> 
    <return_groups>1</return_groups> 
    </methodCall> 
    </data> 
    </api>""" 


form_fields = {'data': XML} 
form_data = urllib.urlencode(form_fields) 

result = urlfetch.fetch(url=url, 
         payload=form_data, 
         method=urlfetch.POST, 
         headers={'Content-Type': 'application/x-www-form-urlencoded'}) 

print result.content 
1

它的工作原理,当我删除了标题行:

//curl_setopt($ch, CURLOPT_HTTPHEADER, ... 

最终代码:

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $xml)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
+0

感谢您的回答。我不再在这个项目上,所以我不能验证它,但我很欣赏这个贡献。 –

0

得到它的工作的唯一办法是将XML作为字节数组内容类型=“application/x-www-form-urlencoded”