‏הצגת רשומות עם תוויות WebService. הצג את כל הרשומות
‏הצגת רשומות עם תוויות WebService. הצג את כל הרשומות

יום רביעי, 24 באוקטובר 2012

REST Web Service Asp.Net



עד עכשיו כתבתי על SOAP והגיע הזמן להכיר את השיטה המתחרה REST - representational state transfer , ההבדל העיקרי הוא ש SOAP הוא פרוטוקול, יש בו חוקים ברורים שבעזרתם עוטפים את המידע שברצוננו לשלוח, ב REST העסק שונה, REST היא ארכיטקטורה שמתבססת על HTTP Protocol וכל אחד יכול לממש אותה אחרת.

ב SOAP אנחנו עובדים מול קובץ WSDL שמכיל בתוכו את המעטפת (אלמנטים וקריאות לפונקציות) שמאוד קשה לתחזק  וכשמדובר על Service מורכב העסק עלול להפוך למסורבל, REST נותן פתרון פשוט שמתבסס על HTTP ,בעזרת פונקציות שקיימות בפרוטוקול (Get,Put,Post,Delete) ננתב את הבקשות.

שימו לב! המאמר מציג שיטה למימוש Rest ע"ג IIS 7.

שלב א - צד השרת:

תחילה נבנה פרויקט חדש של Web Site ב Visual Studio.

ניצור Class חדש שיורש את ה Interface של IHttpHandler , עלינו לממש את המאפיין IsReusable ופונקציית ProcessRequest כפי שניתן לראות בדוגמה הבאה:


public class Service: IHttpHandler
    {
        bool IHttpHandler.IsReusable
        {
            get { return false; }
        }

        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            //implementation of request by method...
        }

}

CURD (create , update, read, delete) Method

פרוטוקל HTTP מכיל בתוכו פונקציות מובנות, מי שבונה אתרי אינטרנט מכיר בעיקר את ה GET ו POST המפורסמים שמאפשרים לנו להעביר מידע בין טפסים, אבל קיימות פונקציות נוספות כמו PUT ו DELETE שבעזרתם ניתן לנתב את הבקשות ל Service שלנו.

GET
בקשות למידע, לדוגמה קריאות מה Database.

POST
הוספת אובייקט ל Database בעזרת המידע שנשלח ב Body.

PUT
דומה ל POST , עדכון אובייקט קיים בעזרת המידע שנשלח ב Body.

DELETE
מחיקת אובייקט

כשנשלחת בקשת GET אנחנו פונים ל Database ומחזירים את הנתונים ללקוח על בסיס פרמטרים שעוברים ב Url, כאשר הלקוח שולח בקשת Post יוצרים אובייקט חדש ב Database שאת המידע אנחנו אוספים מה Body של החבילה.

כאשר מתבצעת קריאה ל Service הפונקציה Process Request היא בעצם ה Entry Point וכל בקשה תפעיל את הפונקציה הזאת ודרכה ננתב את הבקשות של המשתמש למשאבים המתאימים כפי שניתן לראות בדוגמה הבאה:

  void IHttpHandler.ProcessRequest(HttpContext _context)
        {
            try
            {
                string url = Convert.ToString(_context.Request.Url);
                //handling CURD (CREATE UPDATE READ DELETE)
                switch (_context.Request.HttpMethod)
                {
                    case "GET":
                        //return data from database             
                        READ(_context);
                        break;
                    case "POST":
                        //create new element in database
                        CREATE(_context);
                        break;
                    case "PUT":
                        //update element in database
                        UPDATE(_context);
                        break;
                    case "DELETE":
                        //delete element from the database
                        DELETE(_context);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            { _context.Response.Write(ex.Message);}
        }

אחרי שלכדנו את השם של הפונקציה שנשלחה מהמשתמש , עלינו לנתב את הבקשה לפונקציה המתאימה, נבנה 4 פונקציות נוספות ב Class:


//getting object from request using GET method
        private void READ(HttpContext _context)
        {
            string serialized = "";
         
            //getting guides parameters from request
            if (_context.Request["type"].ToString() == "person")
            {
                //properly the point to call database
                //get data and parse to xml back to client
                person _person = new person();
                _person.FirstName = "HELLO";
                _person.LastName = "WORLD";
                serialized = Serialize(_person);
            }

            //depend what language you want to use
            _context.Response.ContentType = "text/xml";
            HttpContext.Current.Response.Write(serialized);
        }

        //insert new object using the POST method
        private void CREATE(HttpContext _context)
        {
            //reading the body of the message
            byte[] bodybytes = _context.Request.BinaryRead(_context.Request.ContentLength);
            //convert the bytes array to string as utf8
            string str = Encoding.UTF8.GetString(bodybytes);
            // deserialize xml into person
            person _person = person.deSerialize(bodybytes);
            //insert database...
        }

        //update object using the POST method
        private void UPDATE(HttpContext _context)
        {
            //reading the body of the message
            byte[] bodybytes = _context.Request.BinaryRead(_context.Request.ContentLength);
            //convert the bytes array to string as utf8
            string str = Encoding.UTF8.GetString(bodybytes);
            // deserialize xml into person
            person _person = person.deSerialize(bodybytes);
            //update database...
        }

        //delete element using the DELETE method
        private void DELETE(HttpContext _context)
        {

            if (_context.Request["type"].ToString() == "person")
            {
                if (_context.Request["id"] != null)
                {
                    int i = 0;
                    if (int.TryParse(_context.Request["id"].ToString(), out i))
                    {
                        //continue to database...
                    }
                }
            }
        }

אחד היתרונות ב REST שניתן לעבוד עם סוגי מבנים שונים, בניגוד ל SOAP שמבוסס על XML ב REST אפשר לבחור מה שבא לנו XML , JSON או להמציא בעצמנו את המבנה, במאמר זה נעבוד עם XML , לכן חשוב לייצר 2 פונקציות נוספות שימירו לנו את המידע מ XML לאובייקט .Net ומ .Net ל XML.

קוד מלא:

using System;
using System.Collections.Generic;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Xml;

namespace restLand
{
    public class rest : IHttpHandler
    {
        bool IHttpHandler.IsReusable
        {
            get { return false; }
        }

        void IHttpHandler.ProcessRequest(HttpContext _context)
        {
            try
            {
                string url = Convert.ToString(_context.Request.Url);

                //handling CURD (CREATE UPDATE READ DELETE)
                switch (_context.Request.HttpMethod)
                {
                    case "GET":
                        //return data from database          
                        READ(_context);
                        break;
                    case "POST":
                        //create new element in database
                        CREATE(_context);
                        break;
                    case "PUT":
                        //update element in database
                        UPDATE(_context);
                        break;
                    case "DELETE":
                        //delete element from the database
                        DELETE(_context);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            { _context.Response.Write(ex.Message);}
        }

        //getting object from request using GET method
        private void READ(HttpContext _context)
        {
            string serialized = "";
         
            //getting guides parameters from request
            if (_context.Request["type"].ToString() == "person")
            {
                //properly the point to call database
                //get data and parse to xml back to client
                person _person = new person();
                _person.FirstName = "HELLO";
                _person.LastName = "WORLD";
                serialized = Serialize(_person);
            }

            //depend what language you want to use
            _context.Response.ContentType = "text/xml";
            HttpContext.Current.Response.Write(serialized );
        }

        //insert new object using the POST method
        private void CREATE(HttpContext _context)
        {
            //reading the body of the message
            byte[] bodybytes = _context.Request.BinaryRead(_context.Request.ContentLength);
            //convert the bytes array to string as utf8
            string str = Encoding.UTF8.GetString(bodybytes);
            // deserialize xml into person
            person _person = person.deSerialize(bodybytes);
            //insert database...
        }

        //update object using the POST method
        private void UPDATE(HttpContext _context)
        {
            //reading the body of the message
            byte[] bodybytes = _context.Request.BinaryRead(_context.Request.ContentLength);
            //convert the bytes array to string as utf8
            string str = Encoding.UTF8.GetString(bodybytes);
            //deserialize xml into person
            person _person = person.deSerialize(bodybytes);
            //update database...
        }

        //delete element using the DELETE method
        private void DELETE(HttpContext _context)
        {

            if (_context.Request["type"].ToString() == "person")
            {
                if (_context.Request["id"] != null)
                {
                    int i = 0;
                    if (int.TryParse(_context.Request["id"].ToString(), out i))
                    {
                        //continue to database...
                    }
                }
            }
        }

        private String Serialize(object _class)
        {
            try
            {
                //output string
                String xmlstring = null;

                //parser to xml from object using polymorphism
                XmlSerializer seriailizor = null;
                if (_class.GetType().ToString() == "restLand.person")
                    seriailizor = new XmlSerializer(typeof(person));
             
                //memory buffer
                MemoryStream stream = new MemoryStream();
             
                //xml writer be sure using utf8 encoding
                XmlTextWriter textwriter = new XmlTextWriter(stream, Encoding.UTF8);

                //the moment of true, parsing object
                seriailizor.Serialize(textwriter, _class);
             
                //fill the buffer
                stream = (MemoryStream)textwriter.BaseStream;
             
                //Convert to array to string 
                xmlstring = UTFbytesToString(stream.ToArray());
                return xmlstring;
            }
            catch (Exception ex)
            {
                return "";
            }

        }

        //convert utf8 bytes array to string
        private string UTFbytesToString(Byte[] array)
        {
            UTF8Encoding utf8encoder = new UTF8Encoding();
            string convertedString = utf8encoder.GetString(array);
            return convertedString;
        }

    }


    public class person
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        //create deserialize function for each class
        public static person deSerialize(byte[] xmlbyte)
        {
           try
           {
               XmlSerializer seriailizor = new XmlSerializer(typeof(person));
               MemoryStream stream = new MemoryStream(xmlbyte);
               person _person = new person();
               //parsing the bytes to person
               _person = (person)seriailizor.Deserialize(stream);
               return _person;
           }
           catch (Exception ex)
           {throw;}
        }
    }
}

חשוב מאוד! להגדיר את ה Handler ב Web.config:

</system.web>
 <httpHandlers>
        <add type="restLand.rest" verb="*" path="service" />
  </httpHandlers>
 </system.web>


  <system.webServer>
     <handlers accessPolicy="Read, Script">
            <add name="service" path="service" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
     </handlers>
   </system.webServer>




שלב ב - צד הלקוח

החלק הזה מאוד פשוט, ניצור פרוייקט חדש מסוג Console Application, ונבצע בקשות ל Url של ה Service בעזרת אובייקט HttpRequest, ונקבל את התשובה בעזרת HttpResponse.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace resttest
{
    class Program
    {
        //service url
        static string url = "http://192.168.1.8:443/service?type=person";
        
        static void Main(string[] args)
        {
            GET();
            POST();
        }

        //GET method
        private static void GET()
        {
           
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            
            //selected method
            request.Method = "GET";

            Console.WriteLine("send GET request");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            Console.WriteLine("get response");
            Console.WriteLine(reader.ReadToEnd());
            Console.WriteLine("end GET request");
        }

        //create xml stream of person
        private static byte[] create_person(string firstname, string lastname)
        {
            //create person xml 
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("person");
            writer.WriteStartElement("FirstName");
            writer.WriteString(firstname);
            writer.WriteEndElement();
            writer.WriteStartElement("LastName");
            writer.WriteString(lastname);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();

            return stream.ToArray();
        }

        //POST method, PUT and DELETE like this method
        private static void POST()
        {

            byte[] _arr = create_person("WORLD", "HELLO");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            Console.WriteLine("send POST request");
            
            //selected method
            request.Method = "POST";
            request.ContentType = "text/xml";
            request.KeepAlive = false;
            request.Timeout = 5000;
            request.ContentLength = _arr.Length;

            Stream stream = request.GetRequestStream();
            // write xml to stream
            stream.Write(_arr, 0, _arr.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //write the response from server
            Console.WriteLine(reader.ReadToEnd().ToString());
            Console.WriteLine("end POST request");
        }
       
    }
}

סיכום:
קשה מאוד להגיד איזו שיטה טובה יותר, אבל האמת היא ש REST מבוססת על תקשורת בסיסית של HTTP בניגוד ל SOAP שלא תמיד נתמך ולפעמים יש להשתמש בספריות צד שלישי בשביל לעבוד איתו, אבל חשוב מאוד לזכור שאחריות רבה מונחת על המפתח בזמן ההתעסקות עם REST, בגלל הגמישות שלו אנחנו חסרי הגנה וחשוב לבצע בדיקות רבות לפני שאנחנו משחררים אותו לעולם.

כולל שירות...

יום שבת, 29 בספטמבר 2012

Web Service Authentication ASP.NET




יש לי חיבה רבה ל Web Services במיוחד של Soap והחלטתי לכתוב מאמר שיסביר כיצד ניתן להגן עליו בסביבה כל כך עויינת שנקראת אינטרנט, אבל לפני שנקפוץ למים העמוקים נעבור בבריכת הילדים על מנת להבין מדוע אנחנו צריכים לשמור על ה Web Services שלנו.

אז כמו שאתם יודעים (בתקווה...) Web Service מכיל בתוכו פונקציות מרוחקות שממקומות בשרת שנותן שירותים לתוכניות שמבצעות קריאות לאותן פונקציות (API) ובעצם מחלקים את תחומי האחריות של התוכנית ובונים מרכז לוגי משותף לכל התוכניות שצריכות שירות מה Service ללא תלות בארכיטקטורה.

נבנה פרויקט חדש ב Visual Studio.



מבנה הפרוייקט


נערוך את קובץ Service1.asmx ונוסיף לתוכו 3 פונקציות:

  public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string restricted_functionA()
        {
            return "restricted_functionA";
        }

        [WebMethod]
        public string restricted_functionB()
        {
            return "restricted_functionA";
        }

        [WebMethod]
        public string restricted_functionC()
        {
            return "restricted_functionA";
        }
    }


נריץ את ה Web Service בדפדפן ונגיע לדף Webservice1.asmx  שחושף לנו את הפונקציות של ה Web Service , כאן בעצם יש בעייה , אנחנו ממש לא מעוניינים לחשוף את הפונקציות של ה Service שלנו לכל אחד ובמיוחד לא לחשוף אותו בחיפושים ב Google כבר היום ניתן למצוא מליונים Web Services חשופים.


רק כדי להמחיש את הסכנה אני אשתמש ב Google ככלי פריצה וסריקה ( Google Hack) בעזרת החיפוש המתקדם במנוע ניתן לחפש חשיפות לדוגמה, אם נרשום בחיפוש filetype:asmx נקבל את כל הדפים ש Google מצא בסיומת asmx.


מכאן אנחנו מדרדרים וה Web Service שלנו חשוף מתמיד לכל אחד בכל מקום, הגיע הזמן לצאת מהבריכה של הילדים ושאריות ד.נ.א לא רצוי ולעבור לבריכה של הגדולים, קיימות מספר דרכים להגן על ה Web Service משלב ההעלמה מגוגל ותהליך Authentication מול ה Service שלנו.

חשוב לציין שאם ה Web Service שלנו משרת תוכניות שנגישות אליו מכתובות קבועות ניתן למנוע את כל הבלגן בעזרת חוק ב Firewall שיאפשר גישה ל Service רק מכתובות מוכרות, במקרה שה Service ניתן למשתמשים שלא ניתן לדעת את הכתובות שמהן הם יגיעו ל Service נצטרך לנקוט בשיטה אחרת.

קודם כל העלמת השירות מגוגל, נכתוב קובץ robots.txt ונשמור אותו בתיקייה של הפרויקט, נרשום את השורות הבאות:

User-agent: *
Disallow: /

ונמנע מ Google לסרוק את ה Service שלנו, עד כאן היה החלק הפשוט בכל הסיפור ועכשיו נתחיל את תהליך Authentication.

 Forms Authentication

הדרך הפשוטה ביותר לאבטח את ה Web Service שלנו, על מנת לגשת ל Web Service יש להפעיל את פונקצית Login בשביל להתחבר ל Service, מה שקורה בפועל שהשם משתמש והסיסמא נבדקים מול ה DB ובמקרה שהם נכונים ה Web Service רושם Cookie עם Ticket אצל ה Client.

Webservice

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Security;

namespace WebService1
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        private string Authenticate(string user, string pass)
        {
            //check the credential from DB or hard coded.
            if (user == "proxytype" && pass == "password")
                return "Proxytype connect";
            else
                return null;
        }

        [WebMethod]
        public bool login(string user, string pass)
        {
            string isLog = Authenticate(user, pass);

            if (isLog != null)
            {
                //return authentication cookie
                FormsAuthentication.SetAuthCookie(user, false);
                return true;
            }
            else
                return false;
        }

        [WebMethod]
        public void logout()
        {
            FormsAuthentication.SignOut();
        }

        [WebMethod]
        public string restricted_functionA()
        {
            //check if user authentication before execute
            if (Context.User.Identity.IsAuthenticated)
               return "Access Granted Restricted FunctionA";
            return "Access Denied Restricted FunctionA";
        }

        [WebMethod]
        public string restricted_functionB()
        {
            //check if user authentication before execute
            if (Context.User.Identity.IsAuthenticated)
                return "Access Granted Restricted FunctionB";
            return "Access Denied Restricted FunctionB";
        }

        [WebMethod]
        public string restricted_functionC()
        {
            //check if user authentication before execute
            if (Context.User.Identity.IsAuthenticated)
                return "Access Granted Restricted FunctionC";
            return "Access Denied Restricted FunctionC";
        }
    }
}


Client

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using example1.localhost;
namespace example1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //create new instance for the webservice
            Service1 myservice = new Service1();
            //create a cookie container for the return 
            //cookie from the web service
            myservice.CookieContainer = new System.Net.CookieContainer();
            
            myservice.login("proxytype", "password");
            Response.Write(myservice.restricted_functionA());

        }
    }
}


למרות שהמשתמש לא יכול להשתמש בפונקציות הן עדיין חשופות והמשתמש עדיין יכול לראות אותם אם הוא יכניס את הכתובת של ה Web Service בדפדפן שלו וזה עדיין לא מספיק מאובטח, המטרה שכל גישה לא מורשה תקבל הודעת שגיאה 401.1 Unauthorized, תחילה יש לשנות הגדרה ב IIS עצמו, בתגית Authentication במצב Feature View יש לבטל את ה Anonymous Authentication , ולהפעיל את ה Basic Authentication:


כשהמשתמש ינסה להכניס את הכתובת של ה Service בדפדפן הוא יצטרך להכניס שם משתמש וסיסמא , במקרה שהם לא נכונים הוא יקבל שגיאה 401.1 Unauthorized.



בשביל להעביר את ה Credentials יש לבצע מספר שינויים ב Client:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using example1.localhost;
using System.Net;
namespace example1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //create new instance for the webservice
            Service1 myservice = new Service1();
           
            //create net credentials
            CredentialCache myCredentials = new System.Net.CredentialCache();
            NetworkCredential netCred = new NetworkCredential("proxytype", "password");

            //sign the credentials as basic authentication 
            myCredentials.Add(new Uri(myservice.Url), "Basic", netCred);
            myservice.Credentials = myCredentials;

            //create a cookie container for the return 
            //cookie from the web service
            myservice.CookieContainer = new System.Net.CookieContainer();
            
            myservice.login("proxytype", "password");
            Response.Write(myservice.restricted_functionA());
           

        }
    }
}

חשוב מאוד!  

Basic Authentication מתבסס על שמות והסיסמאות של המשתמשים שרשומים במערכת ההפעלה של ה Service, מומלץ ליצור משתמש חדש עם הרשאות מוגבלות , במקרה שאין SSL השם משתמש והסיסמא עוברים כ Clear Text והם חשופים לכל מי שמאזין על הרשת.

סיכום:

משחקי הרשאות עלולות להפוך את העסק למאוד מסורבל וחשוב מאוד לפשט כמה שאפשר את פריסת האבטחה סביב התוכנית שלנו, ניתן להשתמש בצורות Authentication נוספות כמו Windows ו Digest , כמובן לא לשכוח להשתמש ב SSL.

לא לחשוף סתם...

יום שני, 12 בספטמבר 2011

Php Soap Server and .NET Client

Webservices תמיד עניינו אותי ולכן חיפשתי משהו נחמד על מנת שתוכלו להכיר אותם טוב יותר, קיימת תחרות רבה בין 2 השיטות המובילות בתחום, שיטת ה REST הנפוצה במערכות Linux , כמו שראינו במאמרים בעבר , שם מרבית העבודה מתבצעת עם REST , ולכן העבודה היא הרבה יותר קשה לניתוח, כל חברה יכולה לממש את התתמשקות בצורה אישית ללא סטנדרט מסויים ובאחריות המשתמש לנתח את הנתונים בעצמו.
 השיטה השנייה Simple Object Access  Protocol - Soap , מוגדרת כסטנדרט ועל מנת להשתמש בו יש לעמוד בחוקים שלה, מיקרוספט אימצו את השיטה ומבחינתה זאת השיטה הטובה ביותר לפיתוח בפלטפורמות Windows , אני באמת לא יודע מה יותר טוב, עדיין REST פופלרי מאוד אבל Soap מומלץ לעבודה ע"י W3C העולמית שפיתחה עבורו תקן WSDL - Web Services Description Language  , לכן זה נראה לי ויכוח שכנראה אין בו מנצחים ומפסדים וצריכים להכיר את 2 השיטות.

  חשוב מאוד! המאמר הזה מתייחס לעבודה עם Soap בסביבת Linux ונוסה על מערכת הפעלה Fedora. 

 תשתית:

תחילה יש להבין שעבודה עם Soap בסביבת Linux מצריכה Php אבל לא סתם אלא Php 5.0 ומעלה , התמיכה המלאה ב Soap קיימת בגרסה 5 בלבד, גרסאות נמוכות יותר מצריכות עבודה מול כלים של צד שלישי , בקיצור סיפור מהתחת לכן לא מומלץ לשלב Soap במערכות קיימות עם Php נמוך מגרסה 5, וכמובן שרת Apache. יש להתקין את המחלקות של ה Soap ב Php

#: yum install php-soap

* יש להפעיל את ה Apache מחדש,  ולגשת לדף הבדיקות  phpinfo ולראות אם נוספו השורות הבאות:
soap
Soap Client enabled
Soap Server enabled
DirectiveLocal ValueMaster Value
soap.wsdl_cache11
soap.wsdl_cache_dir/tmp/tmp
soap.wsdl_cache_enabled11
soap.wsdl_cache_limit55
soap.wsdl_cache_ttl8640086400

אחרי שהכנו את ה Server נתחיל לרשום את הקוד, כאן העסק מתחלק ל 2 חלקים, חלק הראשון הוא המימוש של הפונקציות והפרמטרים, במקרה הזה נכתוב קובץ עם סיומת Php , בו נכתוב את הקוד עם כמה תוספות, החלק השני הוא החשיפה של ה Webservice בעזרת קובץ WSDL בפורמט Xml.

 נעבור בקצרה על מבנה הקובץ ה Wsdl , ניתן לקבל מידע רחב בנושא בלינק הבא:


  • לפי ה Element אפשר לראות שהפונקציה שחשופה לנו היא HelloWorld , מתחת שם הפונקציה ניתן לראות שיש לה "ילדים" שהם הפרמטרים שהיא מקבלת ומחזירה ומה הסוג שלהם.
  • תגיות PortType פעולות מופשטות להודעות נכנסות ויוצאות.
  • תגיות ה Message מייצגות מידע שמועבר, מאוד חשובים לשלבים הלוגיים של ה Webservice
  • תגיות ה Binding בעצם מייצגות את הפרוטוקול את מבנה ה Data שהוגדר ע"י PortType ספציפי.
  • תגית ה Port מייצגת איזה Port קצה.
  • תגית ה Service מייצגת סדרה של Ports שקשורים ל Service.


מבנה קובץ ה Php הוא מאוד פשוט, יש הצהרה תחילה על אובייקט Server$ שמסוג SoapServer , בשלב ההצהרה נעמיס את שם הקובץ ה WSDL לכן ממולץ שהקבצים ישבו באותה תקייה, לאחר מכן נוסיף לאובייקט את הפונקציה אותה אנו מעוניינים לחשוף ולאחר מכן נחבר את ה Handle על מנת שהיא תופעל. אין יותר מידי מה לפרט על הפונקציה בגדול אנחנו צריכים להוציא את הפרמטר שנשלח ע"י שימוש במערך וע"פ השם שניתן בקובץ ה Wsdl.

 

טיפ להתחלה:
 מומלץ לבנות את ה WSDL ע"ג ה Visual Studio בעזרת יצירת פרויקט Webservice , יש ליצור פונקציות פקטיביות ולאחר מכן לראות את ה WSDL שנוצר ובכך לחסוך את כתיבת ה WSDL , המנגנון האוטמטי ב VS בהחלט עושה את העבודה.

בהצלחה.