call php function in c#

Asked

Viewed 1,302 times

2

As by c# in Windowsformsapplication call a function from a php page and take the value from it, example:
php page:

function retornar()
{
  $valor = "aa";
  return $valor;
}

In case I would like to call this page by c# and assign to a variable of c# the value that will come, in case "aa".

  • 1

    Is php your code? can you change it?

  • Yes, I can, it’s mine

  • Yes it is possible! you need to make a request with C# and in php code you should make one echo with the desired format be it a json or plain text, then just take the.

  • have an example request link for me to read? please

2 answers

7


If it is a "php page" then I suppose you are talking about a web page yourself, then in practice you should call the function on your web page, for example teste.php:

<?php

function retornar() {
  $valor = "aa";
  return $valor;
}

echo retornar();

Or it might even just be this:

<?php

echo "aa";

And then call the address http://seusite/teste.php (your site is just an example) on its own c#, you can use Webclient

Call in your C file#

WebClient client = new WebClient();
string downloadString = client.DownloadString("http://seusite/teste.php");

Or save content to a document:

WebClient client = new WebClient ();
client.DownloadFile("http://seusite/teste.php", " C:\conteudo.txt");
  • then in case for me to "access" the return function I would need to put in the link? I was doubtful about this, if you can explain me

  • Yes, you need the @Rods link, but you can bind if the php script is on the same machine as the c#script. But if the . php is a web page even, as I mentioned it works. The WebClient must be in your C#

  • i put as in the link then to get to the "return" function taking as example that the page is called test.php and from whatever link you showed

  • @Rods in fact you call the return in the PHP page, for C# this is irrelevant, since the download is done after processed, the <?php echo retornar();, it was just an example, you can do so <?php echo 'rods'; that will arrive like this rods in C# after the DownloadString ;)

  • 1

    Ahhh, I download the whole file and pick up which part I want in the case by own c#?

  • That’s right @Rods ;)

  • Valeuzão had not understood

  • THANK YOU VERY MUCH!

Show 3 more comments

0

According to what I searched, in C# you can execute whatever you want on the command line as follows:

string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

source: https://stackoverflow.com/questions/1469764/run-command-prompt-commands

Since you can run commands on the PC, instead of accessing a URL you can run the PHP code through the interpreter that comes along with PHP5+. It would only be the case if you run your . php file straight from the command line, then you won’t need a server running the desired program. You can even pass arguments directly on this command line, and reference inside PHP through the $argc (number of arguments) and $argv (the arguments themselves, in string).

php nome_do_arquivo.php argumento1 argumento2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.