Design Simple Web API in PHP and Calling API using ASP .NET

1. API (in PHP at Linux CentOS Server):

<?php
// http://localhost:8080/api/index.php?FORM_NO=100001&PASSWORD=abcde&EMAIL=nazim@kuet.ac.bd&NAME=Md. Nazim Uddin
$FORM_NO = 0;
$PASSWORD = “”;
$EMAIL = “”;
$NAME = “”;
try{
$FORM_NO = $_GET[‘FORM_NO’];
$PASSWORD = $_GET[‘PASSWORD’];
$EMAIL = $_GET[‘EMAIL’];
$NAME = $_GET[‘NAME’];
}catch(Exception $ex){}

if($FORM_NO != 0 AND $PASSWORD != “”)
{
$link = mysql_connect(“localhost”, “root”,”pwd”);
$db_selected = mysql_select_db(‘api’, $link);
if (!$db_selected) {
die (‘Can\’t use api : ‘ . mysql_error());
}
$sql = “INSERT INTO CANDIDATE (FORM_NO, PASSWORD, EMAIL, NAME) VALUES (“.$FORM_NO.”, ‘”.$PASSWORD.”‘, ‘”.$EMAIL.”‘, ‘”.$NAME.”‘)”;
mysql_query($sql);
mysql_close($link);
}
?>

2. API Call (in ASP .NET at Windows Server):

Int32 IsFailed = 0;
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(“http://localhost:8080/“);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));
HttpResponseMessage response = client.GetAsync(“api/index.php?FORM_NO=100002&PASSWORD=csdeam2&EMAIL=nazim@kuet.ac.bd&NAME=Sanjida Rezwana”).Result;
if (response.IsSuccessStatusCode)
{
IsFailed = 0;
}
else
{
IsFailed = 1;
}
}
catch (Exception ex) {  }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s