Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

php

api

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 use that methods. How to do such thing?

like image 549
Rella Avatar asked Feb 03 '23 04:02

Rella


2 Answers

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.

like image 141
Darin Dimitrov Avatar answered Feb 05 '23 20:02

Darin Dimitrov


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

like image 21
Otávio Décio Avatar answered Feb 05 '23 19:02

Otávio Décio