我正在从c#打电话给c#。但是,当我必须将关联数组传递给API时,我的问题就出现了。我不知道C#中PHP关联数组的确切实现,但我使用过字典。这不管用。
我一直在使用RestSharp调用API。
代码实现:
var client = new RestClient(BaseUrl);
var request = new RestRequest(ResourceUrl, Method.POST);
IDictionary<string,string> dicRequeset = new Dictionary<string, string>
{
{"request-id", "1234"},
{"hardware-id", "CCCCXXX"},
};
request.AddParameter("request", dicRequeset);
var response = client.Execute(request);
var content = response.Content;PHP实现(简称):
* Expected input:
* string request[request-id,hardware-id]
* Return:
* code = 0 for success
* string activation_code
*/
function activate()
{
$license = $this->checkFetchLicense();
if (!$license instanceof License) return;
$response = $license->activate((array)$this->_request->getParam('request'));
}有人能帮我从C#把数组传递给PHP吗?
发布于 2017-03-15 19:21:38
虽然迟发,但我已经通过以下方法解决了这个问题:
var request = new RestRequest(ResourceUrl, Method.POST);
request.AddParameter("request[request-id]", hardwareId);
request.AddParameter("request[hardware-id]", hardwareId);发布于 2014-01-06 08:40:29
在C#和PHP中添加对可能会造成约定上的差异吗?你试过使用Add吗?
IDictionary<string,string> dicRequeset = new Dictionary<string, string>();
dicRequeset.Add("request-id", "1234");
dicRequeset.Add("hardware-id", "CCCCXXX");还是用索引器?
dicRequeset["request-id"] = "1234";
dicRequeset["hardware-id"] = "CCCXXX";或者我能想象的最好的就是JSON,因为它是为传输而设计的。
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new {request-id = "1234", hardware-id = "CCCXXX"});尽管我将第三个变体标记为最佳,但第三个变体的问题可能是PHP可能无法解码JSON字符串,因为它可能不是这样设计的。但是在一般情况下,JSON是用来解决这些问题的。
发布于 2014-01-06 10:04:47
如果我猜得对,RestSharp的RestSharp方法不会自动将对象序列化为json,因此只调用对象的toString方法。因此,尝试获取一个JSON.net库并手动进行json编码,
IDictionary<string,string> dicRequeset = new Dictionary<string, string>
{
{"request-id", "1234"},
{"hardware-id", "CCCCXXX"},
};
var jsonstr = JsonConvert.SerializeObject(dicRequeset);
request.AddParameter("request", jsonstr);这应该能行。
https://stackoverflow.com/questions/20945840
复制相似问题