0

My Class in C# which should be send to PHP:

MyData data = new MyData();
data.Name = "MyName";
data.Type = "MyType";
data.ID = 1;
data.Description = "MyLongDescription...";

How do I send it to PHP via C# in JSON format? And how do I retrieve it in PHP? The next step should be to insert the submitted data/values into a MySQL DB.

C# -> Send some JSON Data to PHP -> Write JSON Data to MYSQL DB via PHP

Sample Database: MyData(varchar Name, varchar Type, int ID, varchar Description)

My current PHP Code:

$json = json_decode($input,true);

$connection = mysql_connect("localhost","root","");
if (!$connection)
{
     die(mysql_error());
}

mysql_select_db("MyDataDB", $con);

mysql_query("INSERT INTO MyData (Name, Type, OtherID, Description) 
VALUES('$json[Name]','$json[Type]','$json[ID]','$json[Description]')");

Any ideas?

Thanks for your efforts.

3
  • serialize data which should be send over the wire (create wcf) and use php to consume this service. weblogs.asp.net/gunnarpeipman/archive/2007/09/17/… Commented May 4, 2012 at 12:55
  • 1
    This question is overly broad. You've got several pretty big and very distinct tasks here (generating JSON, sending JSON over HTTP, receiving JSON over HTTP, parsing JSON, inserting data into a database). Put some effort into it, and then ask a focused question if you get stuck. Commented May 4, 2012 at 12:55
  • My problem is the C# part. I have to post the JSON data and a PHP parameter called "key" to make sure the user is authorized. Commented May 4, 2012 at 13:00

2 Answers 2

2

C# code to encode:

var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jsonSerializer.Serialize(yourCustomObject);

PHP code to decode:

$decoded = json_decode($received_json_string);

What have you tried, by the way?

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

2 Comments

I've tried the PHP part. But I don't know the best way to do it in C#.
At first encode your object as in given example and send using HttpWebRequest
0

I'll answer this, but to Quentin's point, you've got a lot going on.

There is no direct interop between C# and PHP so you'd need to expose a web service from PHP and consume it in your C#. Better yet, have the C# save directly to mysql and don't worry about interop.

Comments

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.