How to make C# & PHP interact with each other

Hello everybody,

I am working on an C# application which would use the remote MySQL database located in my website hosted on a Linux server with PHP & MySQL support.

I tried to connect directly to the MySQL database using ODBC drivers, but was not able to connect due to restrictions  at my hoster side.

So the only option was to make C# send a request to PHP scripts running at the webserver and make PHP return data to C# application running on the desktop through internet.

Can anybody please tell me how to do this.

Please suggest any online links or tutorials or your own idea.

Please help me.

Kannan



Answer this question

How to make C# & PHP interact with each other

  • Gazeth

    Code Block

    try
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://remote/fromphp.php");

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader input = new StreamReader(response.GetResponseStream());

    DataSet dsTest = new DataSet();
    dsTest.ReadXml(input);

    int i,j, varTotCol = dsTest.Tables[0].Columns.Count, varTotRow = dsTest.Tables[0].Rows.Count;
    for (j = 0; j < varTotRow; j++)
    {

    for (i = 0; i < varTotCol; i++)
    {
    MessageBox.Show(dsTest.Tables[0].Rows[j].ToString());
    }
    }

    }
    catch (Exception Except)
    {
    MessageBox.Show(Except.ToString());
    }

    PHP Code:
    <
    echo "< xml version=\"1.0\" encoding=\"UTF-8\" >";

    echo "<MessageXML>";

    echo "<Message>";
    echo "<MsgID>Msg</MsgID>";
    echo "<From>Kannan</From>";
    echo "<Email>b_kannan@server.com</Email>";
    echo "<MsgDate>01/01/1001</MsgDate>";
    echo "<MsgTime>01:01:01</MsgTime>";
    echo "</Message>";

    echo "<Message>";
    echo "<MsgID>Msg</MsgID>";
    echo "<From>Kannan</From>";
    echo "<Email>b_kannan@server.com</Email>";
    echo "<MsgDate>01/01/1001</MsgDate>";
    echo "<MsgTime>01:01:01</MsgTime>";
    echo "</Message>";

    echo "</MessageXML>";
    >


    Now in PHP its simple to connect to MySQL and add the return data in between the tags.

    Usefull PHP Code returning data from MySQL Database:
    <

    $dbhost = 'localhost';
    $dbuser = 'user';
    $dbpass = 'password';
    $dbname = 'database;

    //Connecting to DB
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbname);

    $query = "SELECT * FROM users";
    $result = mysql_query($query);
    echo "< xml version=\"1.0\" encoding=\"UTF-8\" >";
    echo "<MessageXML>";
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    echo "<Data>"."<Email>{$row['email']}</Email>"."<Name>{$row['name']}</Name>"."<Password>{$row['password']}</Password>"."<Question>{$row['question']}</Question>"."<Answer>{$row['answer']}</Answer>"."</Data>";
    }
    echo "</MessageXML>";
    mysql_close($conn);
    >

    im also a newbie in c# and also in php... actually this maybe the first time that i will use xml.. is this code can trigger to php to do somethings for example what if i have a save,edit. or delete Button in my c# application then i will click one of them can this code of c# will trigger to php using xml ... because im also working now with c# and im wondering if it is possible but anyway thanks for this post, atleast i already have an idea in how to interact c# and php...


  • SQL2005usr

    Hi Kannan,
    The only solution that I can offer is to simply develop a web service on the PHP side and call it from C#. Unfortunately, PHP doesn't seem to provide you with a lot of options out of the box that can help you do that. So, one possible solution is to "render" xml as a result, then call the page from C#, get the xml, parse it and so on. We've done that a couple of years back when we had to integrate Bugzilla with Windows Sharepoint Services, and it did a good job back then. The bad thing about it is that in fact you have to write all that code that does the call (C #), render the output (PHP) and then parses the response (C #). So you might say that it's not a *real* web service call (although that’s exactly what you'll do) :)

    Cheers,

    Branimir



  • GoPrior

    The easiest way of sending parameters to the other side is to send them through a query string. So if the page you want to call (the one that will rend the xml) should recieve a param userID, the request URL should look like http://someaddress.com/page.php userID=1.

    Then you should clear the response from the PHP side, set the response type as xml and write the xml into the response. From the C# code, you can make the call with the WebClient class - check out the documentation along with some examples at http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemnetwebclientclasstopic.asp

     

    Cheers,
    Branimir



  • ThomasFreudenberg

    Ok I have done something and able to make C# & PHP interact with each other using XML.

    C# Code:
    try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://remote/fromphp.php");
                   
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    StreamReader input = new StreamReader(response.GetResponseStream());

                    DataSet dsTest = new DataSet();
                    dsTest.ReadXml(input);
                   

                    int i,j, varTotCol = dsTest.Tables[0].Columns.Count, varTotRow = dsTest.Tables[0].Rows.Count;
                    for (j = 0; j < varTotRow; j++)
                    {

                        for (i = 0; i < varTotCol; i++)
                        {
                            MessageBox.Show(dsTest.Tables[0].Rows[j]Idea.ToString());
                        }
                    }
               
                }
                catch (Exception Except)
                {
                    MessageBox.Show(Except.ToString());
                }

    PHP Code:
    <
    echo "< xml version=\"1.0\" encoding=\"UTF-8\" >";

    echo "<MessageXML>";

    echo "<Message>";
    echo "<MsgID>Msg</MsgID>";
    echo "<From>Kannan</From>";
    echo "<Email>b_kannan@server.com</Email>";
    echo "<MsgDate>01/01/1001</MsgDate>";
    echo "<MsgTime>01:01:01</MsgTime>";
    echo "</Message>";

    echo "<Message>";
    echo "<MsgID>Msg</MsgID>";
    echo "<From>Kannan</From>";
    echo "<Email>b_kannan@server.com</Email>";
    echo "<MsgDate>01/01/1001</MsgDate>";
    echo "<MsgTime>01:01:01</MsgTime>";
    echo "</Message>";

    echo "</MessageXML>";
    >


    Now in PHP its simple to connect to MySQL and add the return data in between the tags.

    Usefull PHP Code returning data from MySQL Database:
    <

    $dbhost = 'localhost';
    $dbuser = 'user';
    $dbpass = 'password';
    $dbname = 'database;

    //Connecting to DB
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbname);

    $query  = "SELECT * FROM users";
    $result = mysql_query($query);
    echo "< xml version=\"1.0\" encoding=\"UTF-8\" >";
    echo "<MessageXML>";
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
     echo "<Data>"."<Email>{$row['email']}</Email>"."<Name>{$row['name']}</Name>"."<Password>{$row['password']}</Password>"."<Question>{$row['question']}</Question>"."<Answer>{$row['answer']}</Answer>"."</Data>";
    }
    echo "</MessageXML>";
    mysql_close($conn);
    >

    Hope this helps somebody else also.

    Regards,
    Kannan.


  • Leonardo Carlos Prada

    Thanks for replying Branimir,

    I came to the same conclusion. But being a newbie I don't have a clue how to call the PHP service from C# application and read the XML.

    Can you please give me some idea how to call PHP and send a parameter (like a user id to be processed in database).

    How to again get the XML back from PHP to C#.

    Regards,

    Kannan.


  • How to make C# & PHP interact with each other