Emulating PHP page to get response in C# - c#

I browsed internet but haven't found my answer... as I'm running out of ideas, I'd appreciate some help on this issue.
So here it is:
I have some PHP code that takes a path to a file and returns a bunch of information that i need.
I have the source code (it is open source), and i know my C#.
My problem is i want to be able to use this PHP code from a C# application, offline, emulating the PHP code.
Any ideas?
Thanks.

If you really must, you can always launch the PHP interpreter from C# by spawning a process. I'll leave the details to MSDN. Better, though, would be to just translate the PHP to C# by hand.

You could set up a PHP server on the local machine and invoke the file using an HTTP request created with c# (such as with the WebRequest object). This satisfies the requirement of working offline and since you have the PHP code, you can make minor modifications to accommodate whatever parameters you need to pass into it.
However unless this is for an isolated case in a controlled environment, I would not suggest it.
If this is for distribution to user machines it would be a) impractical to deploy and b) a maintenance nightmare. If you are running on a server and rewriting the PHP in c# is not an option, it should get the job done.

It sounds like to either wish to
Translate the PHP code to C#
Call the PHP code from .NET
???
I don't think emulation is the term you're looking for, exactly.
If you can link to the PHP code (if it's OSS, is there a site with more information?) or paste it here, we can probably let you know the best way to handle this.

If you don't want to translate the PHP code into C# then you must invoke the PHP interpreter.
First, look at the license on the PHP code you would be using. When I looked at the code earlier I noticed that all but one PHP file had a GNU GPL license on it. GNU GPL should work for what you want, but I recommend that you review the GNU GPL if you haven't done so already. You should also look into what license is supposed to be applied to the last file before continuing.
Second, rewrite the entry file. The file in question is upload_file.php. This file loads the other files as includes, parses the SC2 replay file, and generates an output for use by a web browser. You just need to rewrite this file to have it output a simplified version of its output to the console. You might have it receive the name of the replay file from command line arguments. The other files that are marked as GNU GPL can just be copied directly into your project. I should also say here that there may already be a console version. Take a minute to look around for one.
Third, write a C# application. Invoke the PHP interpreter telling it the what file to interpret, and the name of the replay file to parse. You will need to redirect the console output from your PHP instance into your C# code to be able to read the output.
Fourth, read the output.
Fifth, parse the output for use in your C# application.

Old thread but figured it could use an answer.
You could use the codeplex Phalanger. http://phalanger.codeplex.com/ It allows you to take existing php files and compile them to .net

Related

Is it possible/better to use PHP to execute C# code from the file or is it necessary to make it an executable?

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.
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
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

How to compile C# file without a project?

Is there a way in VS2013 to compile a C# file without a project or solution?
This is useful, e.g. in editing some C# code quickly while preparing a Stack Overflow answer while scrubbing corporate IP from the file (without risking any harm to the "real project").
The build toolbar is grayed-out.
I know one can occasionally see squiggles for errors.
Any other simple ideas?
You can use https://dotnetfiddle.net/
You can specify different things in it. It is best for writing fast answers, also you can post link with your saved work. It is similar to jsfiddle if you aware of it.
You can use Snippet Compiler. You can download it from below URL.
http://www.sliver.com/dotnet/SnippetCompiler/
Its a stand alone light weight small application.
If you have Mono C# installed then C# REPL is awesome tool to execute small chunk of c# code.
http://www.mono-project.com/docs/tools+libraries/tools/repl/

Check file creator before loading to prevent tampering/corrupted data

Informative
I'm working on a project that loads a binary file from the disk. This file has a specific structure to store data and I need to protect this file from being loaded if the data was generated/tampered by third parties.
Resume
I have a file that I must check if the "creator" is me before loading.
Project Specification
The application that loads the file is written in C++.
The application that generates the file is written in C#.
Question
What are my options to accomplish this?
What libraries/algorithms to use?
Can you provide me some steps or guides?
Notes
I have Crypto++ linked to the C++ project if it helps.
What you are looking for is a signing/certifying system wirth asymetric keys. The functionality is built in .Net. A Quick google search shows this example which looks legit.
Keep in mind though, that if you don't have the full control of the certification process (if your application is on a client's computer), someone might decompile the code to bypass the check.
quickly like that i would take a look at LZMA SDK since the SDK support c++ and C# and just compress/encrypt the file
maybe there is a better solution, but this would be my first try

Uploading a file from a C# desktop application to a PHP script?

I am looking to create a desktop application in C# which :
Allows the user to select a file / multiple files / folder containing files from his computer.
Upload the files selected to a PHP script (which is already equipped to handle file uploads using the $_FILES array.)
I'm a PHP developer and have never coded a single line of .NET before. So, you can assume I have no experience with .NET whatsoever.
I have looked this up online and all I seem to come up with are ASP.NET server side upload controls which i do not want. I'm looking for a client side solution. Also, will i have to make any changes in my PHP script ? The script already handles uploads from an HTML multipart form.
If anyone can help me point in the right direction of where to look, what C# controls are available which can help me create the application I need, I would really appreciate it.
The first, and simplest, way to go about this is to use any of the WebClient's UploadFile methods.
Here's some info an an example;
http://msdn.microsoft.com/en-us/library/36s52zhs.aspx
I have a feeling that this will not be enough for you, since you want to upload multiple files in a single request. The WebClient class can be used to manually build a http multipart request, which is probably your best bet.
It's a bit much to explain how to achieve this here on SO, but there are good guides out there.
Here are a couple of very to-the-point articles
http://www.codeproject.com/KB/cs/uploadfileex.aspx
http://www.codeproject.com/KB/IP/multipart_request_C_.aspx
And if you're interested in the details, or better OO design, here's an alternative (a bit harder to follow if you're not experienced with C#)
http://ferozedaud.blogspot.com/2010/03/multipart-form-upload-helper.html
I think both articles should give you enough info to get started.

C# plugin for Google Chrome

Can I communicate to Google Chrome in C#?
For writing a chrome plugin for example.
<Spoilers>
Quick short answer: No, because Extensions require JSON, JavaScript, and HTML.
</Spoilers>
Hi Tony,
There are multiple templates on the internet that you can download to build a chrome extension project using Visual Studio.
Downloading one of these templates just gives you the files and folders that you need which I call "the setup".
That won't let you build a Google extension using C#.
Andrey mentioned that there are libraries like Bridge.NET that will compile your code into HTML/JavaScript. That will help you write HTML and JavaScript using C#. You still need a manifest.json file.
I don't recommend that. That library is not designed for Chrome Extensions. Also, you will have to compile the code into JavaScript and store that JavaScript code in a JavaScript file. I recommend using HTML/JavaScript with no compilers when building your Chrome Extension.
You need to keep in mind that there are 3 main parts in a chrome extension. These are:
manifest.json
A JavaScript file
HTML file
There are multiple steps and it's not hard to build a google chrome extension. This link will give you the complete tutorial on developing a chrome extension with detailed explanation. This tutorial installs a template so that you can develop it in Visual Studio just like I mentioned before.
What I have done to address is use Simple Message Host, it will trigger an executable on the local machine that you code in c#, sending stdin messages and listening to stdout messages so you can build this host to use as a bridge, but like I said, it needs to be on your local network at least, and you have to do some editing in the windows registry, so it has its limitations.
But for the system I am working with, this solution worked perfectly because I have a controlled environment that I can set up all these prerequisites.
So, just to clarify, what I did here is:
Create a chrome extension with background.js opening up the listener to the website's javascript.
Add a registry in windows registry pointing to the path of the executable.
Create the executable in C# doing all your logic.
Send a response from the executable to the extension and then back to the website.
There are several guides on how to do this, so I won't detail these steps here so I don't replicate it.
But for the moment, it is the best way to do what you want, if you have control of your environment that is.
So, if your plugin (extension or chrome app) will work on a controlled environment, this is the way to go, otherwise, I would consider something related to ClickOnce perhaps or WebAssembly but that's not fully released yet.
Chrome own extension manager supports extensions written in js and html.
that said, to execute c# code within the extension you have two options:
Compile c# code to javascript code which then can be added as a normal javascript extension to chrome (take a look at scriptsharp)
Use c# as a back-end system. just like most of download managers:
for case 2 you need a c# application installed in client device(or in the cloud) listing to a specific port (using httplistener or self hosted webapi (you can use netcore) which do these steps
Listing to incoming requests
parse request data eg. json and do something with it
return the result to javascript extension which can display it to user or do other things with it.
The topic is quite old, but I'd like to share that sample:
https://github.com/Retyped/Demos/tree/master/ChromeDemo
In a few words, the sample is implemented in C#. The Retyped.chrome NuGet package provides bindings (Chrome API) for Bridge.NET projects. So yes, now you can implement your logic in C#, then C# code will be transparently compiled into JavaScript by Bridge.NET compiler.
With that approach you can build your Chrome extension on top of .NET Framework as well as utilize thousands of JavaScript libraries.

Categories