0

I have already asked this question but need further help. c# is not sending json request to PHP

I am trying to send data from c# to PHP webpage using the JSON & REST API HTTP request.

On PHP page I see "String (0)"

c# Code

user user = new user();
    {
        user.firstname = "aaaa";
        user.secondname = "aaaaaaaaaaa";
        user.email = "aaa";
        user.phonenumber = "aaa";
    };
    string json = JsonConvert.SerializeObject(user);
    HttpWebRequest request = WebRequest.Create("https://scs.agsigns.co.uk/test.php") as HttpWebRequest;
    request.ContentType = "application/json";
    //request.Accept = "application/json, text/javascript, */*";
    request.Method = "POST";
    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(json);
    }
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    Stream stream = response.GetResponseStream();

    string json1 = "";

    using (StreamReader reader = new StreamReader(stream))
    {
        while (!reader.EndOfStream)
        {
            json1 += reader.ReadLine();
        }
    }
    DisplayAlert("Alert", json1, "OK");

PHP

$content = file_get_contents("php://input");

var_dump($content);

In c# I get this alert

c# display alert message

In the PHP webpage, I see following

PHP page shows string(0)

What I want to get data which app sendand save into MySql.

EDIT

I have ammended the PHP file code to save data in MySQL.

I am getting error

Notice: Trying to get property 'name' of non-object in C:\inetpub\scs\test.php on line 16

This is my PHP code.

//Receive the RAW post data.
$content = file_get_contents("php://input");

$obj = json_encode($content);

$insert_stmt = $mysqli->prepare("INSERT INTO test (name,address) VALUES (?,?)");
$name =$obj->{'name'};
$address = $obj->{'address'};


$insert_stmt->bind_param("ss", $name, $address);
//Execute the statement
$insert_stmt->execute();
  • The page will always show empty if you reload it because at the time of the page reload nothing has been sent. To see if it is working, return a response `exit(json_encode(['success' => true]));` and pick that up in your C#. – Script47 Mar 06 '19 at 11:53
  • So when app send data to PHP; How I can use that data in PHP because I was wanting to save that data in MySQL – James Hickling Mar 06 '19 at 11:55
  • When the request is sent from the app, if all is well, then you can use it in PHP. So just code the rest of your PHP code as though the data has reached fine and test to see if the rows appear in the db. – Script47 Mar 06 '19 at 11:56

1 Answers1

0

You should use HttpClient instead of HttpWebRequest

Your request would look like this with HttpClient

public async void SendUserDataToServer()
{
    user user = new user();
    {
        user.firstname = "aaaa";
        user.secondname = "aaaaaaaaaaa";
        user.email = "aaa";
        user.phonenumber = "aaa";
    };
    string json = JsonConvert.SerializeObject(user);
    using (var client = new HttpClient())
    {
        var response = await client.PostAsync(
            "https://scs.agsigns.co.uk/test.php", 
             new StringContent(json, Encoding.UTF8, "application/json"));
    }
    DisplayAlert("Alert", json, "OK");
}

Reference: this

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • Thanks, @Mihir Dave. My initial issue is still there. c# "DisplayAlert" shows the Json string which gets sent to PHP. I just tried to output the result in PHP but still says NULL. How to echo the data on a PHP page. – James Hickling Mar 06 '19 at 12:10
  • That's because both webpages have different sessions, in order to see those data on your php page you have to store data somewhere that is being received on https://scs.agsigns.co.uk/test.php and then grab it back on your php page – Mihir Dave Mar 06 '19 at 12:14
  • What would be the best way to store data. I tried storing the data in the local variable but I believe they get cleared on the page reload. – James Hickling Mar 06 '19 at 12:16
  • understand this, webpages are stateless and create a new instance with a new request, so the local variable is not a way to achieve what you want. on your https://scs.agsigns.co.uk/test.php page, you have to write logic that stores incoming post data to the database. and then pull it back wherever you want – Mihir Dave Mar 06 '19 at 12:19
  • I have added the SQL part to save data into MYSQL. Getting error cannot get property from an object. I have edited my question with the new code. – James Hickling Mar 06 '19 at 13:35
  • @JamesHickling i am no php expert from https://stackoverflow.com/questions/18866571/receive-json-post-with-php the link shouldn't be you using json_decode instead of json_encode? – Mihir Dave Mar 06 '19 at 14:22
  • @James have you got the chance to check it? – Mihir Dave Mar 07 '19 at 18:38
  • Thanks for your help. It works great. Thanks for your help. Thanks for asking. – James Hickling Mar 08 '19 at 09:16
  • Quick Question. Do you know how to print response? When the request is sent; the response is saved in a variable ( "var response " ). Do you know how to output/check response? Thanks – James Hickling Mar 08 '19 at 12:45
  • Sorry, buddy I am not sure about PHP. – Mihir Dave Mar 11 '19 at 14:14