2014-03-05 45 views
0

我想將一個base64_encoded xml字符串捲曲到一個c#WebAPI,我沒有控制權。我可以成功地強制字符串,但它不被API接受。cURL base64_encode XML

記錄輸出顯示cURL正在剝離base64字符串中的字符,我認爲這是問題所在。

的代碼,我是:

$username = "username"; 
$password = "$23hrlkbl"; 
$xml = "<Envelope><Header><User>".$username."</User><Password>".$password."</Password></Header></Envelope>"; 

$passThru = "https://api.domain.com/SignIn.aspx?passthruUrl=/Management/Api/DataEnrichment/GetAddresses/?buildingNumber=1%26streetName=Nightingale%20Road%26postcode=L12%200QN 
$post_packet_data = 'XMLdataPacket='.urlencode(base64_encode($xml)); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $passThru); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_packet_data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

$response = curl_exec ($ch); 
curl_close($ch); 
print_r($response); 

發佈通過HTML表單到$中繼地址下面的字符串(非urlencoded的$ post_packet_data)的作品成功

XMLdataPacket=PEVudmVsb3BlPjxIZWFkZXI+PFVzZXI+dXNlcm5hbWU8L1VzZXI+PFBhc3N3b3JkPiQyM2hybGtibDwvUGFzc3dvcmQ+PC9IZWFkZXI+PC9FbnZlbG9wZT4= 

然而張貼了同當字符串通過cURL發送以下內容並且不被接受

XMLdataPacket=PEVudmVsb3BlPjxIZWFkZXI PFVzZXI dXNlcm5hbWU8L1VzZXI PFBhc3N3b3JkPiQyM2hybGtibDwvUGFzc3dvcmQ PC9IZWFkZXI PC9FbnZlbG9wZT4= 

UPDATE

說話的API的開發者之後,他們已經證實現在請求來在正確的格式,但相信呼叫不被處理由於不正確的頭部被髮送。他們給我發了,他們得到了使用招的工作電話的標題:

POST https://api.domain.com/SignIn.aspx?passthruUrl=/Management/Api/DataEnrichment/GetAddresses/?buildingNumber=1%26streetName=Nightingale%20Road%26postcode=L12%200QN HTTP/1.1 
Host: test.domain.com 
Connection: keep-alive 
Content-Length: 164 
Cache-Control: max-age=0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Origin: null 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36 
Content-Type: application/x-www-form-urlencoded 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 
Cookie: ASP.NET_SessionId=uzwfjq3zojao3l5fw141l453 

XMLdataPacket=PEVudmVsb3BlPjxIZWFkZXI%2BPFVzZXI+dXNlcm5hbWU8L1VzZXI%2BPFBhc3N3b3JkPiQyM2hybGtibDwvUGFzc3dvcmQ%2BPC9IZWFkZXI%2BPC9FbnZlbG9wZT4%3D 

我將如何修改捲曲通話的頭複製以上。目前再次使用招捲曲通話頭顯示如下:

GET http://test.domain.com/test_call.php HTTP/1.1 
Host: test.domain.com 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-gb,en;q=0.5 
Accept-Encoding: gzip, deflate 
Cookie: CAKEPHP=g22d7kf7qs8b1v02uui2mnop30 
Authorization: Basic bmV0aG91c2VwcmljZXMxOnRlbXAxODkx 
Connection: keep-alive 
Cache-Control: max-age=0 

的數據在API端的POST receieved但捲曲頭是通過未來的GET根據上述

+0

另一個建議是嘗試這樣的:'$ post_packet_data = http_build_query(陣列( 'XMLdataPacket'=> BASE64_ENCODE($ bym_header)),NULL,「&');'這會照顧URL編碼 – Max

+0

的這確實使輸出POST數據來自cURL的外觀與HTML表單看起來一樣,但API仍然拒絕它。這可能與POST類型或發送的不正確標題有關嗎? – Leo

+0

您應該找回一些錯誤信息,可以發佈它嗎?可能它與頭文件有關,因爲如果你通過表單發佈它,它會起作用。它是什麼類型的API?是否有任何SOAP? – Max

回答

2

嘗試使用rawurlencode ()而不是urlencode() - 它使用'%20'編碼空格字符,而不是使用'+'字符。

+0

不幸的是,這似乎沒有幫助。查看日誌,從cURL輸出的POST數據現在看起來與從表單輸出的數據相同,但是通過cURL調用API仍然被拒絕。 – Leo