3

In my application, a resquest is sent to a PHP server from a C# application. The application sends details the author name via POST. I want the PHP application to query a database and return, ideally, an array of the authors details:

C#

        String result = "";
        string url = LINK_TO_SITE;
        using (WebClient client = new WebClient())
        {
            NameValueCollection postData = new NameValueCollection()
            {
                {"Author", Properties.Settings.Default.Author}               

            };
            result = Encoding.UTF8.GetString(client.UploadValues(url, postData));
            MessageBox.Show(result);

php

    $author=$_POST["author"];   
    $stmt = $mysqli->stmt_init();
    $stmt = $mysqli->prepare("SELECT name, date, code FROM Collab where Members=?");    
    $stmt->bind_param('s', $author);
    $stmt->execute();       
    $stmt->bind_result($name,$date, $code);

I can retrive the details fine. Now, how will I put the data into an array that can be sent back to C#?

So basically...How do I get a PHP array to work in C#?

2
  • 1
    Try encoding the array using json_encode, then using a JSON parser class in #c parse the JSON to an array. Commented Apr 30, 2013 at 11:37
  • Could you post an example of what your result string looks like? Im not familiar with php Commented Apr 30, 2013 at 11:37

1 Answer 1

3

There are many ways to do that it depends how you want to send it. The easiest way is to use separation character in C# for example ";" and then in php

$authors = explode(";", $_POST["author"]);  

you can also use XML or JSON, preg_match() it's up to you. You can format it before sending.

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

5 Comments

Hi, Thanks for your answer and the array is sent by PHP and recieved successfully by my application. My code: "String[] jsonObject = JsonReader.Deserialize<String[]>(result);" However, it says I'm missing JSONReader in C#. I've looked around but I've yet to find a copy of it. can you provide me with a link or an alternative code?
Take a look here: stackoverflow.com/questions/3142495/… This code lets you work with JSON like you would in JavaScript at run-time.
Unfortunately, That doesn't seem to work at all. It seems I'm missing the supporting libraries for that too. The following seem to be missing from my system using System.Dynamic; using System.Web.Script.Serialization;
How would this be done via XML. I've had a look around at that too and I haven't found anything solid.
Hi, I was able to reslve my issues in the end. Thanks for your support

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.