C# PHP Communication ( Get / Send Data from C# to PHP )

This Piece of code will allow you, to Get information from Web page or Send Information to it, By Using POST method trough C# to a PHP Page.

C#🠖 Send Data 🠖 PHP Page 🠖 Processing Data 🠖 C# Get Information.

Let Show you the code:

Programm.cs

using System;
 
class Program
{
    static void Main(string[] args)
    {
        string Data = Web.GetPost("http://www.smart-arab.com/example.php", "UserName", "Me", "Password", "123");
        Console.WriteLine(Data);
    }
}

Take a look at this line of code

Web.GetPost("www.smart-arab.com/example.php", "UserName", "Me", "Password", "123");

it’s said like this
1-“WebSite URL”.
2-“Post Variable” , 3-“It’s Value” , 4-“Post Variable” , 5-“It’s Value”

The Post Variable Must be identical inside C# and PHP code.
For Example :

“UserName” in c# code match “Post Variable” in PHP code $_POST[‘UserName’]
(those must be changed together)
And The User Name Value which in our case is “Me” , could be anything “Admin” , “Sarah” … etc, This value will be send from C# to PHP.

Again “Password” in c# code match “Post Variable” in PHP code $_POST[‘Password’]
(those must be changed together)
And Again The Password Value which in our case “123” could be any thing “1904” , “SuperPass” , etc … This value will be send from C# to PHP.

if you need more explain please let me know …

PHP Code:

example.php

<?PHP
  
if (isset($_POST['UserName'])) {
    $UserName = $_POST['UserName'];
} else {
    $UserName = null;
}
  
if (isset($_POST['Password'])) {
    $Password = $_POST['Password'];
} else {
    $Password = null;
}
  
  
echo $UserName . " Is And Password Is " . $Password
  
?>

C# Class : ( You don’t need to go through it deeply, just add it to your Project as a Class file )

Web.cs

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
 
class Web
{
    public static string GetPost(string Url, params string[] postdata)
    {
        string result = string.Empty;
        string data = string.Empty;
 
        System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
 
        if (postdata.Length % 2 != 0)
        {
            MessageBox.Show("Parameters must be even , \"user\" , \"value\" , ... etc", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            return string.Empty;
        }
 
        for (int i = 0; i < postdata.Length; i += 2)
        {
            data += string.Format("&{0}={1}", postdata[i], postdata[i + 1]);
        }
 
        data = data.Remove(0, 1);
 
        byte[] bytesarr = ascii.GetBytes(data);
        try
        {
            WebRequest request = WebRequest.Create(Url);
 
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytesarr.Length;
 
            System.IO.Stream streamwriter = request.GetRequestStream();
            streamwriter.Write(bytesarr, 0, bytesarr.Length);
            streamwriter.Close();
 
            WebResponse response = request.GetResponse();
            streamwriter = response.GetResponseStream();
 
            System.IO.StreamReader streamread = new System.IO.StreamReader(streamwriter);
            result = streamread.ReadToEnd();
            streamread.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return result;
    }
}

I’m not going to spoon feed this old post but I figured I should maybe just help out a bit for those that are still trying to figure a way to send more than one type of data back to C#. Basically php and C# can not directly send data back and forth from a website to an application. The best approach for this is to use JSON. I would suggest reading up on PHP/JSON and C#/JSON … Taken from Wiki “JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs.” Using JSON will help you send multiple values in string based format that you can deserialize into a C# class.

Example: PHP JSON

echo"
{
'name' : 'ben',
'age' : 22
}
";

C# code will receive that as a string type which you can then deserialzie into a class.

Example: C# Class

public class UserData
{
public string Name { get; set; }
public int Age { get; set; }
}