0

I am attempting to send data from c# to PHP using the JSON.

I see 'NULL' on the PHP page.

Here is my c# HTTP request which should send json request to PHP webpage.

        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://localhost/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");

I have PHP "test.php" which has the following code.

<?php

echo json_encode($_POST);


?>

When the c# function run; I don't get any errors.

Then I refresh webpage and it says "NUll"

What I would like to achieve is c# to send data using JSON using REST API (HTTP Request).

Then using PHP; I will get the data and save in MySql Table.

But PHP just shows 'Null'

What I am doing wrong.

Thank you

EDIT

The DisplayAlert which I see in c# shows the string which it passes to PHP.

c# alert of string which it passes to PHP

PHP webpage shows string (0)

PHP Code

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

$obj = json_encode($content);



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


$insert_stmt->bind_param("ss", $name, $address);
//Execute the statement
$insert_stmt->execute();
  • 1
    Possible duplicate of [file\_get\_contents("php://input") or $HTTP\_RAW\_POST\_DATA, which one is better to get the body of JSON request?](https://stackoverflow.com/questions/2731297/file-get-contentsphp-input-or-http-raw-post-data-which-one-is-better-to) – Jonnix Mar 06 '19 at 10:01
  • Please at least google before posting questions - http://thisinterestsme.com/receiving-json-post-data-via-php/ would give you a walk through on one option.. Why is this tagged c# the problem is purely php end – BugFinder Mar 06 '19 at 10:02
  • I can see the c# showing me the string which it passes to PHP. But on the webpage, I see "string (0)". I have added pictures in the original question. – James Hickling Mar 06 '19 at 10:40

1 Answers1

0

You have to look what C# call returns to you right ?

I don't know C# but I don't think the json must be send in the $_POST variable but in the body part of the request. In vanilla PHP you can get it with :

file_get_contents('php://input');

Bacteries
  • 593
  • 4
  • 13
  • Thanks, @Bacteries. I can see the alert in c# show me the string which I pass. But on the webpage, I still see "string(0)". I was wanting to send data to PHP so that I can save data in MySQL – James Hickling Mar 06 '19 at 10:39
  • So : don't use $_POST but file_get_contents('php://input'); instead ;) – Bacteries Mar 06 '19 at 13:50
  • I am using file_get_contents('php://input') but still not working. I have amended the code in my original question. See under Edit – James Hickling Mar 06 '19 at 13:56
  • You tried var_dump($content) ? (in C# : does ReadToEnd is not better ?) – Bacteries Mar 07 '19 at 14:12