2

I am currently working on a c#/php project where I need to retrieve some data from the database in c# and the data to an array and send the array to php.

While I am looping through the database I am adding string values into a string list array and once it has finished I then call:

string[] myData = myList.toArray();

I am then trying to post myData to php by using the following code

Dictionary<string, string> report = new Dictionary<string, string>();
report.add("data", myData.toString());
    WebRequest request = WebRequest.Create(domain + appRoot + "/administrator/engine/email-alarms.php");
                    request.Method = "POST";
                    StringBuilder b = new StringBuilder();
                    foreach (KeyValuePair<string, string> data in report)
                    {
                        b.Append(HttpUtility.UrlEncode(data.Key)).Append("=").Append(HttpUtility.UrlEncode(data.Value ?? "")).Append("&");
                    }
                    string postData = b.ToString(0, b.Length - 1);
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = byteArray.Length;

                    Stream dataStream = request.GetRequestStream();
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Close();

                    WebResponse response = request.GetResponse();
    Console.writeLine(response);

I realise saying myData.toString() isn't the correct thing to do as it just prints System.String[] but if I don't do this then I get an error in the c# code saying that there is an invalid argument.

How can I do post the array to php?

Thanks for any help you can provide.

4 Answers 4

3

you can always use the json format as your exchange contract format and convert it back to their native representation and deal with your data accordingly. Both c# and php have functions/libraries that support the json format.

C# you serialize using:

var postData=new JavascriptSerializer().Serialize(object);

and in PHP you can use the json_decode() method. I'm not sure how it works in php.

hope this helps.

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

Comments

2

Update: Changed to not even need a List<string>. You can iterate your original array. Added PHP code.

If you are expecting $_POST["data"] in your PHP script to be a string array, there is no need to serialize to JSON or some other format. PHP can accept arrays posted just fine. You just have to go about it the right way.

Change this:

Dictionary<string, string> report = new Dictionary<string, string>();
report.add("data", myData.toString());
// ...
foreach (KeyValuePair<string, string> data in report)
{
    b.Append(HttpUtility.UrlEncode(data.Key)).Append("=").Append(HttpUtility.UrlEncode(data.Value ?? "")).Append("&");
}

to this (you no longer need the List<string> since you can iterate through your array directly and create a postable array:

foreach (string data in myData)
{
    b.Append(HttpUtility.UrlEncode("data[]")).Append("=").Append(HttpUtility.UrlEncode(data ?? "")).Append("&");
}

This will result in the data key in your POST from this:

string[] myData = new [] { "blah", "something", "else" };

being posted as this:

data[]=blah&data[]=something&data[]=else

This will result in the array being created successfully on the PHP side (I hope).

In PHP, you should then be able to do this:

foreach ($_POST["data"] as $value)
{
    echo $value;
}

Comments

1

If you just want to serialize string[] (not clear but I think you wanted that, was the real question here), then just ...

string.Join(",", myData) 

should output e.g. 1, 2, 3...

Comments

1

Probably your best bet would be to encode it to JSON using JavaScriptSerializer and then you can decode it in PHP with json_decode()

Comments

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.