简体   繁体   English

PHP到C#,反之亦然

[英]PHP to C# and Vice versa

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. 前端公司人员将使用的一个网页,用于查询和更新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. 我正在使用visual studio,后面的代码(C#)差不多完成了,所以SQL和C#之间的交互都没问题。 My original idea was to work with ASP.NET which is familiar to me, but that's not gonna be possible. 我最初的想法是使用我熟悉的ASP.NET,但这是不可能的。 I have to switch to PHP. 我必须切换到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. 所以,今天是我学习PHP的第一天,检查http://php.net/manual/en/index.php ,很多东西看起来和ASP.NET非常相似所以我想它不会那么难。

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? 有没有一种简单的方法可以使用php页面从我的类中获取/发送C#变量? 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? 我已经阅读了关于使用XML的内容,但是我仍然摸不着头脑,还有另一种更简单的方法吗?

You have options. 你有选择。

  1. direct integration. 直接整合。 PHP can instantiate and use .NET objects . PHP可以实例化和使用.NET对象。 See the DOTNET library in PHP. 请参阅PHP中的DOTNET库。 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. 因此,如果您在Windows上运行PHP,并根据PHP DOTNET基础结构的要求公开.NET逻辑,那么您可以直接从PHP调用.NET类。 Some restrictions: PHP is built to integrate with the .NET 2.0 runtime. 一些限制:PHP构建为与.NET 2.0运行时集成。 You can't build .NET 4.0 objects and connect to them from PHP. 您无法构建.NET 4.0对象并从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() . 正如其他人建议你可以通过一个REST或Web服务接口公开你的C#逻辑,然后使用curl库或file_get_contents()从PHP调用这些服务。 The C# logic could be, but need not be, publicly exposed. C#逻辑可以(但不一定)公开暴露。 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. 另一方面,您的架构可能要求从第三方或用户应用程序访问相同的API。 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#. 无论是公共还是私有,您都希望使用WCF或ASPNET MVC来公开在C#中实现的这些服务。

  3. asynchronous mechanisms. 异步机制。 PHP can connect to MSMQ. PHP可以连接到MSMQ。 See Using PHP to Open MSMQ Queues . 请参阅使用PHP打开MSMQ队列 Of course C# can do likewise. 当然C#也可以这样做。 You could use MSMQ as a buffering communication mechanism between the two worlds. 您可以使用MSMQ作为两个世界之间的缓冲通信机制。 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. JSON或XML在这里是合适的选择。

  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. 如果您担心使用MSMQ,因为它是“需要管理的另一个基础架构”,您还可以使用数据库作为中间人。 A shared database can be accessed by both PHP and C# and used as a message queue or communication conduit. PHP和C#都可以访问共享数据库,并将其用作消息队列或通信管道。 PHP inserts messages in a MySQL Table, and the C# app could read and process them, then place reply messages in a different table. PHP在MySQL表中插入消息,C#应用程序可以读取和处理它们,然后将回复消息放在不同的表中。 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. 最后,还有Phalanger。 This lets you compile PHP onto the .NET Framework. 这使您可以将PHP编译到.NET Framework上。 This means integration between C# and PHP will be simple. 这意味着C#和PHP之间的集成将很简单。 I haven't tried this but it might satisfy your requirements. 我没试过,但它可能满足你的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM