2017-10-17 68 views
2

我試着使用形式的URL編碼下面的代碼如何正確地從PHP和捲曲後轉換爲C#HTTP客戶端後爲團購API

<?php 
    // requires PHP cURL http://no.php.net/curl 
    $datatopost = array (
     "supplier_id" => "1", 
     "token" => "xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT", 
     "ci_lineitem_ids" => json_encode (array (54553919, 54553920)), 
    ); 
    $ch = curl_init ("https://scm.commerceinterface.com/api/v4/mark_exported"); 
    curl_setopt ($ch, CURLOPT_POST, true); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
    $response = curl_exec ($ch); 
    if($response) { 
     $response_json = json_decode($response); 
     if($response_json->success == true) { 
     //Successfully marked as exported (only items which are not already marked exported 
     } else { 

     } 
    } 

與C#中的HTTP客戶端工作的轉換如下內容類代替$datatopost陣列在PHP

FormUrlEncodedContent markExportedContent = new FormUrlEncodedContent(new[] { 
    new KeyValuePair<string,string>("supplier_id",country.supplier_id), 
    new KeyValuePair<string, string>("token",country.token), 
    new KeyValuePair<string, string>("ci_lineitem_id", JsonConvert.SerializeObject(country.ci_lineitem_ids)) 
}); 

,然後使用一個HTTP客戶端發佈此的API,但是我得到以下響應

{"reason": "Missing Parameters (ci_lineitem_ids).", "reason_code": 400, "success": false} 

我認爲它與我使用的Newtonsoft Json包中的JsonConvert.SerializeObject(country.ci_lineitem_ids)有關,它將字符串數組轉換爲json編碼數組,如PHP代碼中所示。

任何人都能夠與一些想法,以幫助,爲什麼這是不工作的期望,因爲我全力以赴在現在這個嘗試過多種不同的方式這個人之前做到這一點的想法?

+1

你忘了 「S」 的'ci_lineitem_id' – Crowcoder

回答

1

除非是拼寫錯誤,否則您在c#代碼中的第三個參數的末尾忘記了s。

ci_lineitem_ids是在你的PHP

ci_lineitem_id是在C#

+0

簡單的錯別字是令人問題關閉。在投票結束之前,我一直在等待確定情況。 – Crowcoder

+0

我已接受的答案,因爲這是在代碼中的錯字。我將不得不等待實際測試這一變化,但毫無疑問,它會起作用。 –