开发者

How to call PHP api from C# ?(simple example)

开发者 https://www.devze.com 2022-12-27 19:20 出处:网络
So I need a very basic interaction example of C# client using some PHP API (A remote service being called from a C# app). I want to see a simple php API containing 2 methods sum(a, b):c and echo(strin

So I need a very basic interaction example of C# client using some PHP API (A remote service being called from a C# app). I want to see a simple php API containing 2 methods sum(a, b):c and echo(string):string and a simple C# client able to u开发者_Go百科se that methods. How to do such thing?


I don't know what you mean by a PHP server but can't you write a script that will add two numbers:

<?php echo (int)$_GET["a"] + (int)$_GET["b"]; ?>

And in C#:

using (var client = new WebClient())
{
    var a = 50;
    var b = 100;
    var result = client.DownloadString(string.Format("http://example.com/add.php?a={0}&b={1}", a, b));
    Console.WriteLine(result);
}

Another and a better option is PHP SOAP.


Create a PHP web service and a C# client defining a proxy to call it.


Have a look at the WebClient Class

0

精彩评论

暂无评论...
验证码 换一张
取 消