0

I am making a PHP webpage that will retrieve data from a database and allow a user to send that data as an object to a database utilized by another application. Because of the API of the application, the object I need to send it as requires .NET.

The C# page takes in the necessary constructors, creates the object, and sends it. This part of my code functions perfectly when run within Visual Studio or when ran as a .exe from the command line. I can, presumably, run exec() on the executable and have it run that way within my PHP page.

However, I am hesitant to develop it this way. It seems like there should be a way to simply have a/some PHP file(s) within the same project and call the C# file directly.

I am very new to both PHP and C#. I have searched for both solutions, and I have only found that it is possible to run an executable that uses C# within PHP and seen examples of C# using PHP, but nothing that addresses using an executable over referencing the file directly, or even anything about just referencing a C# file within a Visual Studio project.

To clarify the question, I am wondering if it is possible to execute code from a C# file directly with PHP, and if so, is it a better practice than using exec() to run an executable version of the C# project.

EDIT: I want to clarify that I know PHP isn't naturally available within Visual Studio.

rrl
  • 21
  • 1
  • 4

2 Answers2

0

Make it executable than call it in php by

<?php 
exec('file.exe');
?>

You can also read this article: Calling .Net Framework and .Net Assemblies from PHP

Otávio Barreto
  • 1,348
  • 3
  • 10
  • 27
  • Actually, I already said that I was aware of this being an option. However, I was asking if it was possible to call the C# file itself and what is the best practice. This doesn't answer the question. – rrl Apr 19 '17 at 19:14
  • I edited the answer, it will guide you i the right direction. – Otávio Barreto Apr 20 '17 at 01:43
0

Yes, it's possible to execute C# code directly from PHP. The title of your question suggest you want to execute C# code that is compiled in .dll form not as an executable application .exe Excute C# Code From File

Using a DLL With PHP for Dummies

Call C-Sharp Using PHP

Community
  • 1
  • 1
RP Jr.
  • 3
  • 3
  • How about running your C# code an .exe application, that contacts PHP to retrive the database information it requires, than having PHP iniatiating the conversation with your C# code. – RP Jr. Apr 19 '17 at 19:49
  • I'm not sure if you noticed, but two of your links go to the same place. I found the part you were talking about regardless though. It's not possible for the project I'm apart of to run C$# as an application with PHP, unfortunately, but thank you for the suggestion. – rrl Apr 20 '17 at 18:52
  • In my opinion the best way to do this would be to send the required data to the PHP file on the server using POST [web requests](https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class) and process all data on server side. And only send the data in the right format to the server. Let the server handle the other things. That way you have less chance of people tampering with it – DarkEyeDragon Dec 22 '18 at 09:48