Pages

Saturday, 8 February 2020

Post data over with API using PHP

This post will help you to understand. How can you call an API created from ASP.NET MVC and calling from PHP?  API was created for the notification purpose for school.API Database is SQL server and server-side language is C#. You will see in this post how can you consume a post API created from other language and consume by PHP

Data Model

Field
Description
ID
Row ID (Auto Increment)
NotesText
Text of notification
NotesType
Attachment Type (Text,Link,File)
Path
Location of Attachemntq
ClassID
Class Name
NewGIF
Description: http://ankuram.probuzzmedia.com/Image/Page/new.gif
CreateDate
Created Date

API Code (C#)

public IHttpActionResult Postdata([FromBody] NotificationModel data)
        {
            UserRepository obj = new UserRepository();
            string ErrMsg="";
            obj.AddNotesDB(data, ref ErrMsg);
            return Ok();
        }public IHttpActionResult Postdata([FromBody] NotificationModel data)
        {
            UserRepository obj = new UserRepository();
            string ErrMsg="";
            obj.AddNotesDB(data, ref ErrMsg);
            return Ok();
        }



Database Repository C#

public bool AddNotesDB(NotificationModel ob, ref string ErrMsg)
        {
            String result = "";
            try
            {
                using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand("[dbo].[AddNotification]", dbcon))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.AddWithValue("@NotesText", ob.NotesText);
                        cmd.Parameters.AddWithValue("@NotesType",Convert.ToInt32(ob.NotesType));
                        cmd.Parameters.AddWithValue("@ClassID",Convert.ToInt32(ob.ClassID));
                        cmd.Parameters.AddWithValue("@NewGIF", Convert.ToInt32(ob.NewGIF));
                        cmd.Parameters.AddWithValue("@CreatedBy", Convert.ToInt32(ob.CreatedBy));
                        cmd.Parameters.AddWithValue("@Path", ob.Path);
                        if (dbcon.State == ConnectionState.Closed)
                            dbcon.Open();
                        cmd.ExecuteNonQuery();
                        result = "1";
                    }
                }
            }
            catch (Exception ex)
            {
                ErrMsg = ex.Message;
            }
            if (result == "1")
                return true;
            else
                return false;
        }

PHP Code


$msg="";
if(isset($_POST["submit"]))
{
$chk="0";
 
    if(isset($_POST['chkNewGIF'])) 
   { 
        $chk ="1"; 
    } 
//API URL
//$url = 'http://localhost:56585/api/myapi/Postdata/';
$url='http://ankuram.probuzzmedia.com/api/myapi/Postdata/';
//create a new cURL resource
$ch = curl_init($url);
//setup request to send json via POST
$data = array(
'NotesText' => $_POST["txtNotofaction"],
'NotesType' => '1',
'Path'  =>'gurl',
'ClassID'  => $_POST["ddlClass"],
'NewGIF'  => $chk,
'CreatedBy'  => '1'
);
$payload = json_encode($data);
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
//close cURL resource
curl_close($ch);
$msg="Saved";
}

No comments:

Post a Comment