0

I am working in calling PHP API from c#. But, my problem arise when I have to pass associative array to API. I don't know exact implementation of PHP associative array in C# but I have used dictionary. It didn't works.

I have been using RestSharp to call API.

Code Implemenation:

  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 API Implementation(Short):

 * 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'));
    }

Can someone help me to pass array to PHP API from C#?

7
  • if this api is publicly available, posting which it is might help get some answers. Commented Jan 6, 2014 at 9:04
  • Updated the question.Please have a look. Commented Jan 6, 2014 at 9:29
  • Can you test the API locally? What does var_dump($this->_request); and var_dump($this->_request->getParam('request')); say Commented Jan 6, 2014 at 9:56
  • Sorry, I can not test API locally. It's third party API. Commented Jan 6, 2014 at 10:00
  • shot in the dark, but you could try adding each keyvaluepair as a separate parameter: request.AddParameter("request-id","1234");... Commented Jan 6, 2014 at 10:02

3 Answers 3

1

Maybe adding the pairs makes differences in conventions in C# and PHP? Have you tried using Add?

IDictionary<string,string> dicRequeset = new Dictionary<string, string>();
dicRequeset.Add("request-id", "1234"); 
dicRequeset.Add("hardware-id", "CCCCXXX");

Or using indexer?

dicRequeset["request-id"] = "1234";
dicRequeset["hardware-id"] = "CCCXXX";

Or the best I can imagine is JSON as it is designed for the purpose of transmission.

var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new {request-id = "1234", hardware-id = "CCCXXX"});

The problem in the third variant despite I marked it as the best, might be that the PHP API may not decode the JSON string, because it might not be designed that way. But in general purpose JSON is meant to solve that kind of problems.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. But I have tried all ways you specified but seems no luck.
Oh, sorry I couldn't help. Right now I'm out of ideas. I will do a research, but I guess you are doing to. If you find you answer you can post it. I will do the same if I succeed.
1

Though late post but I've solved this problem by using following approach :

        var request = new RestRequest(ResourceUrl, Method.POST);
        request.AddParameter("request[request-id]", hardwareId);
        request.AddParameter("request[hardware-id]", hardwareId);

Comments

0

if I guess right, the AddParameter method of RestSharp doesn't automatically serialize an object to json thus insteadly just calls the object's toString method. So try to get a JSON.net library and make the json encoding manually,

IDictionary<string,string> dicRequeset = new Dictionary<string, string>
                {
                    {"request-id", "1234"},
                    {"hardware-id", "CCCCXXX"},
                };
var jsonstr = JsonConvert.SerializeObject(dicRequeset);
request.AddParameter("request", jsonstr);

this should work.

6 Comments

Sorry, it does not work. There is no any json decoding in api method.
JSON.net has been removed from dependency of RestSharp, it needs to be added manually as a library.
@hago what will be the structure of request after this?
the http body would be like request=%5B%7B%22request-id%22%3A+%221234%22%2C+%22hardware-id%22%3A+%22CCCCXXX%22%7D%5D
this would not work as PHP is expecting $array['request']['request_id'] = "smth";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.