I'm trying to do a simple program with a very simple UI.
Just a button in a c# windows form, when i press the button it encodes a simple array to Json and upload it to http://localhost, and then get the json data in PHP.
This is my code now in c#
private void button_Click(object sender, RoutedEventArgs e)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/SemesterProject/index.php");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
user = "Foo",
password = "Baz"
});
streamWriter.Write(json);
Console.WriteLine("Console.WriteLine(json);");
Console.WriteLine(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine("Console.WriteLine(result);");
Console.WriteLine(result);
}
}
And this is my code in PHP:
<?php
$post= file_get_contents("php://input");
$decodedjson=json_decode($post,true);
var_dump($decodedjson);
?>
at PHP output i get 'NULL' and in c# output i get
Console.WriteLine(json);
{"user":"Foo","password":"Baz"}
Console.WriteLine(result);
array(2) {
["user"]=>
string(3) "Foo"
["password"]=>
string(3) "Baz"
}
After this i need to post the data from PHP to phpMyAdmin database