Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP to C# and Vice versa

Tags:

c#

php

I'm in this project:

A web page that's gonna be used by the front-end company people to query and update data from a SQL DB. I'm working with visual studio and the code behind (C#) is almost done, so the interactions between SQL and C# are ok. My original idea was to work with ASP.NET which is familiar to me, but that's not gonna be possible. I have to switch to PHP. So, today is my first day learning PHP, checking http://php.net/manual/en/index.php and a lot of things seem quite similar to ASP.NET so I guess it won't be that hard.

Anyways, some questions popped up quite fast as I wanted to script something else than a "hello world".

Is there an easy way to get/send C# variables from my class using a php page? I've read soemthing about using XML in order to do so, but still I'm scratching my head, is there another, easier, way to do this?

like image 313
Daniel Sh. Avatar asked Jun 26 '12 13:06

Daniel Sh.


1 Answers

You have options.

  1. direct integration. PHP can instantiate and use .NET objects . See the DOTNET library in PHP. So if you run PHP on Windows, and you expose your .NET logic according to the requirements of the PHP DOTNET infrastructure, then you can just call .NET classes directly from PHP. Some restrictions: PHP is built to integrate with the .NET 2.0 runtime. You can't build .NET 4.0 objects and connect to them from PHP.

  2. synchronous network protocols. As others have suggested you can expose your C# logic via aREST or web services interface, then invoke those services from PHP using the curl library or file_get_contents(). The C# logic could be, but need not be, publicly exposed. In other words, you could make it accessible only from within the firewall of your app, so that no anonymous public access is possible. on the other hand your architecture may call for access to the same API from 3rd-party or user apps. In that case it needs to be exposed publicly.

    in either case, public or private, you will want to use WCF or ASPNET MVC to expose these services implemented in C#.

  3. asynchronous mechanisms. PHP can connect to MSMQ. See Using PHP to Open MSMQ Queues . Of course C# can do likewise. You could use MSMQ as a buffering communication mechanism between the two worlds. To do this you'd need to come up with a data serialization protocol, for the messages you put and get on the queue. JSON or XML would be appropriate choices here.

  4. Database. If you are concerned about employing MSMQ as it is "one more piece of infrastructure to manage" you can also employ a database as a go-between. A shared database can be accessed by both PHP and C# and used as a message queue or communication conduit. PHP inserts messages in a MySQL Table, and the C# app could read and process them, then place reply messages in a different table. This would require some work by you to design the message formats, protocols, indexes, and request/reply correlation mechanism. But it relies on proven, existing technology that you already know how to use.

  5. Finally, there is Phalanger. This lets you compile PHP onto the .NET Framework. This means integration between C# and PHP will be simple. I haven't tried this but it might satisfy your requirements.

like image 99
Cheeso Avatar answered Oct 14 '22 14:10

Cheeso