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#?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1662290
  • 466
  • 3
  • 10
  • 18
  • 1
    Try encoding the array using `json_encode`, then using a JSON parser class in #c parse the JSON to an array. – Dom Apr 30 '13 at 11:37
  • Could you post an example of what your result string looks like? Im not familiar with php – Robert J. Apr 30 '13 at 11:37

1 Answers1

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.

Robert
  • 19,800
  • 5
  • 55
  • 85
  • Hi, Thanks for your answer and the array is sent by PHP and recieved successfully by my application. My code: "String[] jsonObject = JsonReader.Deserialize(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? – user1662290 Apr 30 '13 at 12:19
  • Take a look here: http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object This code lets you work with JSON like you would in JavaScript at run-time. – Cameron Tinker Apr 30 '13 at 12:28
  • 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; – user1662290 Apr 30 '13 at 12:40
  • How would this be done via XML. I've had a look around at that too and I haven't found anything solid. – user1662290 Apr 30 '13 at 12:42
  • Hi, I was able to reslve my issues in the end. Thanks for your support – user1662290 Apr 30 '13 at 13:16