แสดงบทความที่มีป้ายกำกับ สอน PHP แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ สอน PHP แสดงบทความทั้งหมด

วันพฤหัสบดีที่ 25 ตุลาคม พ.ศ. 2555


โดย ปกติแล้ว การที่เราจะให้คนอื่นสามารถที่จะเข้ามาดูเวบไซท์ของเราได้นั้น เราจะต้องนำเวบไซท์ไปฝากไว้กับผู้ให้บริการ Hosting หรือ Free Hosting ต่างๆ ซึ่งบางครั้งก็อาจจะเกิดความไม่สะดวกในการใช้งาน เช่น เครื่องมือที่ใช้ไม่ครบ หรือไม่มี Database ที่ต้องการ เป็นต้น
ปัจจุบัน นี้ เราได้มีการใช้ ADSL อย่างกว้างขวาง ซึ่งข้อดีประการหนึ่งของ ADSL คือ Always on นั่นหมายถึง เราสามารถเชื่อมต่อ ADSL wfhตลอดเวลาโดยไม่ต้องปิดโมเด็มซึ่งเหมือนกับเราเราต่ออยู่กับอินเตอร์เนทอ ยู่ตลอกเวลานั่นเอง
นั่น หมายความว่า ในเมื่อเรามีการเชื่อมต่ออยู่ตลอดเวลาอย่างนี้ และถ้าเราอยากจะสร้างเวบไซท์ให้คนอื่นสามารถเข้ามาดูเวบของเรา จะทำได้หรือไม่ ถ้าทำได้ จะทำอย่างไร คำตอบอยู่ด้านล่างนี้แล้วครับ
สิ่งที่ต้องเตรียมการ/อุปกรณ์
1. เครื่องคอมพิวเตอร์ 1 เครื่อง ลงระบบปฏิบัติการ Windows 2000,XP,2003 หรือ Linux ก็ได้ (แต่ในตัวอย่างจะเป็น Windows XP Professionl)
2. ADSL Router 1 ตัว Config ให้สามารถเชื่อมต่อกับ Internet เรียบร้อยแล้ว (จากตัวอย่างใช้เป็น ADSL Router Billion 711)
3. ไฟล์ HTML หรือเวบเพจที่เราต้องการจะนำขึ้นสู่สายตาประชาชน
ขั้นตอนการ Config
ไปที่ Start-->Setting -->Control Panel-->Add or remove program
เลือกไปที่ Add/Remove Windows Component
ทำเครื่องหมายที่ Internet Information Services(IIS) จากนั้นกดปุ่ม Next
จากนั้นระบบจะทำการติดตั้ง IIS ลงในเครื่อง ซึ่งจะมีการให้ใส่แผ่นติดตั้ง Windows เพื่อติดตั้งไฟล์บางไฟล์ลงในเครื่อง
เมื่อติดตั้งเรียบร้อยแล้ว ให้ลองไปดูที่ Drive C จะเห็นว่า มีโฟลเดอร์ที่ชื่อว่า Inetpub อยู่
เรียกเข้าไปที่ c:-->Inetpub-->wwwroot แล้วนำเวบเพจที่เราต้องการแสดงไปวาง
ทดลองเรียกเวบ โดยเรียกที่ http://localhost ดังรูป จะขี้นเวบที่เราเขียนไว้
ให้เราเปิดเวบไซท์เข้าไปที่ www.no-ip.comเพื่อขอใช้บริการ Dynamic DNS
เมื่อเข้าสู่หน้าเวบแล้วให้กดไปที่ Sign-up free
จะขึ้นหน้าจอให้เราใส่ข้อมูลต่าง ๆ ซึ่งข้อมูลที่สำคัญคือ e-mail adderess และ password จากนั้นให้กดปุ่ม Sign up now
ระบบจะทำการสร้าง Account และส่ง link สำหรับการ Activate ไปให้ทางเมล์
ให้เราเปิดเมล์ที่เราได้ลงทะเบียนไว้ แล้วทำการ Activation ตามเนื้อหาในเมล์ระบุ
เมื่อ Activate แล้ว ให้เรากลับมาที่ www.no-ip.com อีกครั้ง แล้วใส่ e-mail/password และทำการ Login
เลือกไปที่ Add
จะเข้าหน้า add host ให้ใส่ host name และเลือก domain จากนั้น set type ให้เป็น DNS Host (A) จากนั้นเลือก Create Host
จากนั้นจะขึ้นรายละเอียดตามที่เราได้เลือกไว้
จากนั้นให้เลือกไปที่ Download แล้วเลือกที่ windows จากนั้นเลือกไฟล์เพื่อ Download
เลือก Save File ไว้ที่ Desktop
เมื่อ Download เสร็จ จะปรากฏไฟล์บน Desktop
ให้เรียกโปรแกรม ducsetup ขึ้นมา จากนั้นให้เลือก next
เลือกที่จะติดตั้งแล้วเลือก next
เลือก next อีกครั้ง
Finish
จะขึ้นหน้าจอให้เราใส่ E-mail address/password สำหรับใช้งาน เมื่อใส่แล้วให้กด OK
โปรแกรมจะติดต่อกับ Server แล้วดึงชื่อที่เราได้ขอไว้ออกมา
ใส่เครื่องหมายหน้าโดเมนที่เราต้องการใช้งาน
ตั้งระบบเนทเวิร์คในเครื่องโดยไปที่ Start-->setting-->Control panel-->network connection-->local area connection
ไปที่ Internet protocol(TCP/IP)แล้วเลือก properties
ตั้ง IP ให้อยู่ในวงเดียวกับ Router ดังรูป
จากนั้นเปิด browser แล้วเข้าไปที่ router โดยพิมพ์ 192.168.1.254 ใส่ username/password
เลือกไปที่ virtual server
ใส่ค่าดังรูป จากนั้นกดปุ่ม Add this setting
จะปรากฏหน้าจอดังรูป ให้เลือก Save Config
เลือก Submit
จะปรากฏหน้าจอดังรูปให้รอ
จนกระทั่งขึ้นหน้าจอดังรูป เป็นอันเสร็จเรียบร้อย
สำหรับเครื่องที่มี personal firewall ให้ไปที่ Start-->Setting-->Control Panel-->Windows Firewall
เลือก Turn off
ให้ตรวจสอบค่า WAN ที่ได้จาก Router กับ โปรแกรม No-IP DUC จะต้องตรงกัน ถ้าไม่ตรงให้ปิดโปรแกรม No-IP แล้วเปิดใหม่
ทดลองเข้าจากเครื่องอื่นๆ บน internet จะเห็นว่า สามารภเข้าไปหน้าเวบเพจที่เราตั้งไว้ได้แล้ว
Posted by netdesign On 11:11 No comments READ FULL POST

วันอาทิตย์ที่ 15 กรกฎาคม พ.ศ. 2555

PHP Create - Call Web Service สร้างและเรียกเว็บเซอร์วิส ด้วย PHP (NuSoap and Soap) บทความการสร้าง Web Service และการเรียกใช้งาน Web Service ด้วย PHP แบบง่าย ๆ ผ่าน Library ของ NuSoap /Soap และฟังก์ชั่น soap_server ใน php การเรียกใช้งาน Web Service ฝั่ง Server หรือ Client จะต้องเรียกผ่าน wsdl ซึ่งเป็น XML Format ที่บ่งบอกและอธิบายเกี่ยวกับ Web Service ที่ใหบริการอยู่ว่ามีรูปแบบและการเรียกใช้งานอย่างไรบ้าง เช่น Service Name , Method หรือ ค่าParameters ต่าง ที่ให้บริการอยู่ในขณะนั้น และเมื่อเราสร้างไฟล์ Web Service ที่มีนามสกุล .php และเรียก URL ตามด้วย file.php?wsdl ก็จะได้ XML Format ออกมา ซึ่ง Web Service ที่ได้จะสามารถเรียกและใช้งานได้กับทุกภาษาที่มีมาตฐานการติดต่อผ่านรูปแบบ wsdl

ตัวอย่างที่ 1 ใช้ NuSoap

PHP Create - Calling Web Service

Download Library ของ NuSoap


หรือจะดาวน์โหลดได้จากในส่วนล่างของบทความนี้

สำหรับการสร้าง Web Service ในตัวอย่างนี้จะแบ่งออกเป็น 2 ประเภทคือ
- Web Service ฝั่ง Server ที่ให้บริการ 
- Web Service ฝั่ง Client ที่เรียกใช้ Web Service ฝั่ง Server

Web Service ฝั่ง Server

WebServiceServer.php
01.<?
02.require_once("lib/nusoap.php");
03. 
04.//Create a new soap server
05.$server new soap_server();
06. 
07.//Define our namespace
09.$server->wsdl->schemaTargetNamespace = $namespace;
10. 
11.//Configure our WSDL
12.$server->configureWSDL("HelloWorld");
13. 
14.// Register our method and argument parameters
15.$varname array(
16.'strName' => "xsd:string",
17.'strEmail' => "xsd:string"
18.);
19.$server->register('HelloWorld',$varnamearray('return' => 'xsd:string'));
20. 
21.function HelloWorld($strName,$strEmail)
22.{
23.return "Hello, World! Khun ($strName , Your email : $strEmail)";
24.}
25. 
26.// Get our posted data if the service is being consumed
27.// otherwise leave this data blank.
28.$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] :'';
29. 
30.// pass our posted data (or nothing) to the soap service
31.$server->service($POST_DATA);
32.exit();
33.?>


จาก Code มีการสร้าง Service ชื่อว่า HelloWorld และประกอบด้วย Method ชื่อว่า HelloWorld รับค่า argument 2 ตัวคือ $strName และ $strName และทำการ return return "Hello, World! Khun ($strName , Your email : $strEmail)";

ในไฟล์นี้จะจัดเก็บไว้ในโฟเดอร์ \nusoap\WebServiceServer.php

ทดสอบการรัน Web Service


PHP Create - Calling Web Service

ในตัวอย่างจะเห็น URL ของ Web Service ประกอบด้วย Method ชื่อว่า HellowWorld ให้คลิกที่ WSDL เพื่อดูรูปแบบของ XML

PHP Create - Calling Web Service



เมื่อรัน URL ของ Web Service ตามด้วย ?wsdl จะได้ XML ที่ประกอบด้วย Format ของ wsdl ดังรูป ซึ่งตอนที่นำไปใช้งานจริง ๆ เราจะใช้ URL นี้

รูปแบบ XML ที่ได้
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
03.<types>
04.<xsd:schema targetNamespace="http://localhost/soap/HelloWorld">
05.<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
06.<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
07.</xsd:schema>
08.</types>
09.<message name="HelloWorldRequest">
10.<part name="strName" type="xsd:string" />
11.<part name="strEmail" type="xsd:string" />
12.</message>
13.<message name="HelloWorldResponse">
14.<part name="return" type="xsd:string" />
15.</message>
16.<portType name="HelloWorldPortType">
17.<operation name="HelloWorld">
18.<input message="tns:HelloWorldRequest" />
19.<output message="tns:HelloWorldResponse" />
20.</operation>
21.</portType>
22.<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
23.<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
24.<operation name="HelloWorld">
25.<soap:operation soapAction="http://localhost/nusoap/WebServiceServer.php/HelloWorld" style="rpc"/>
26.<input>
27.<soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
28.</input>
29.<output>
30.<soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
31.</output>
32.</operation>
33.</binding>
34.<service name="HelloWorld">
35.<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
36.<soap:address location="http://localhost/nusoap/WebServiceServer.php" />
37.</port>
38.</service>
39.</definitions>





Web Service ฝั่ง Client

สร้างไฟล์สำหรับ Client เพื่อทดสอบเรียก Web Sevice 

WebServiceClient.php
01.<html>
02.<head>
03.<title>ThaiCreate.Com</title>
04.</head>
05.<body>
06.<?
07.require_once("lib/nusoap.php");
08.$client new nusoap_client("http://localhost/nusoap/WebServiceServer.php?wsdl",true);
09.$params array(
10.'strName' => "Weerachai Nukitram",
11.'strEmail' => "is_php@hotmail.com"
12.);
13.$data $client->call("HelloWorld",$params);
14.echo $data;
15.?>
16.</body>
17.</html>


ในตัวอย่าง มีการระบุ URL ของ Web Service ในส่วนของ new nusoap_client

$client->call("HelloWorld",$params);
// HelloWorld คือ Method ที่อยู่ใน Web Service
// $params คือ argument หรือ parameter ที่จะต้องส่งไป


เมื่อลอง echo $data; ก็จะได้สิ่งที่ Web Service ตอบกลับมา ซึ่งอาจจะอยู่ในรูปแบบของ XML หรือ String

Screenshot

PHP Create - Calling Web Service

Download Code !! 






ตัวอย่างที่ 2 ใช้ Soap

PHP Create - Calling Web Service

วิธีนี้จะใช้ Class ที่ชื่อว่า SoapServer ที่อยู่ใน php_soap.dll วิธีใช้งานจะต้องดาวน์โหลด extension ตัวนี้มาติดตั้งก่อน 

Download php_soap.dll


หลังจากดาวน์โหลดแล้วให้แตกไฟล์และ Copy ไปไว้ในโฟเดอร์ของ extension ถ้าอยากรู้ว่าอยู่ที่ไหน ก็ให้เปิดไฟล์ php.ini

C:\Windows\php.ini
; Directory in which the loadable extensions (modules) reside.
extension_dir = "D:/AppServ\php5\ext"


หาบรรทัดที่มีคำว่า extension_dir ซึ่งจะบอกว่า Path ของ extension ถูกจัดเก็บไว้ที่ไหน

หลังจาก Copy เรียบร้อยแล้วให้เพิ่มบรรทัดนี้ใน php.ini

C:\Windows\php.ini
extension=php_soap.dll


php_soap.dll and php.ini

หรือดูภาพประกอบ

หลังจากแก้ไขเรียบร้อยแล้วให้ Restart Web Server หรือ Apache ซะ 1 ครั้ง

กรณี Error Class ของ SoapClient
Fatal error: Class 'SoapServer' not found in C:\AppServ\www\SoapServer\WebServiceServer.php on line 2


เริ่มต้นการสร้าง Web Service ด้วย Soap

ขั้นตอนแรกให้สร้างไฟล์ helloworld.wsdl

helloworld.wsdl
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02.<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
10.targetNamespace="http://localhost/soap/HelloWorld">
11.<types>
12.<xsd:schema targetNamespace="http://localhost/soap/HelloWorld">
13.<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
14.<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
15.</xsd:schema>
16.</types>
17. 
18.<message name="HelloWorldRequest">
19.<part name="strName" type="xsd:string" />
20.<part name="strEmail" type="xsd:string" />
21.</message>
22. 
23.<message name="HelloWorldResponse">
24.<part name="return" type="xsd:string" />
25.</message>
26. 
27.<portType name="HelloWorldPortType">
28.<operation name="HelloWorld">
29.<input message="tns:HelloWorldRequest" />
30.<output message="tns:HelloWorldResponse" />
31.</operation>
32.</portType>
33. 
34.<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
35.<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
36.<operation name="HelloWorld">
37.<soap:operation soapAction="http://localhost/SoapServer/WebServiceServer.php/HelloWorld"style="rpc" />
38.<input>
39.<soap:body use="encoded" namespace=""encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
40.</input>
41.<output>
42.<soap:body use="encoded" namespace=""encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
43.</output>
44.</operation>
45.</binding>
46. 
47.<service name="HelloWorld">
48.<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
49.<soap:address location="http://localhost/SoapServer/WebServiceServer.php" />
50.</port>
51.</service>
52.</definitions>


สำหรับไฟล์ wsdl อ่านรายละเอียดได้ที่
http://wiki.nectec.or.th/setec/Knowledge/WSDL
http://oreilly.com/catalog/webservess/chapter/ch06.html
http://www.basic-skill.com/content.php?cont_id=74&sec_id=3


สร้างไฟล์ Web Service ฝั่ง Server

WebServiceServer.php
01.<?php
02.$server new SoapServer("helloworld.wsdl");
03. 
04.function HelloWorld($strName,$strEmail){
05.return "Hello, World! Khun ($strName , Your email : $strEmail) ";
06.}
07. 
08.$server->AddFunction("HelloWorld");
09.$server->handle();
10.?>


Save ไฟล์ไว้ที่ \SoapServer\WebServiceServer.php อยู่ภายใต้ root ของ Server

ทดสอบรัน URL ของ Web Service



PHP Create - Calling Web Service

ผลลพัทธ์ที่ได้เมื่อทอสอบรัน URL ของ Web Service


Web Service ฝั่ง Client

WebServiceClient.php
01.<html>
02.<head>
03.<title>ThaiCreate.Com</title>
04.</head>
05.<body>
06.<?
08.array(
09."trace"      => 1,     // enable trace to view what is happening
10."exceptions" => 0,     // disable exceptions
11."cache_wsdl" => 0)         // disable any caching on the wsdl, encase you alter the wsdl server
12.);
13. 
14.$params array(
15.'strName' => "Weerachai Nukitram",
16.'strEmail' => "is_php@hotmail.com"
17.);
18. 
19.$data $client->HelloWorld($params);
20. 
21.print_r($data);
22.?>
23.</body>
24.</html>


ทดสอบการเรียก URL ของ Web Service ฝั่ง Client 

Screenshot

PHP Create - Calling Web Service


Download Code !!


บทความนี้เป็นเพียงพื้นฐานการสร้าง Web Service แบบง่าย ๆ แต่ทั้งนี้สามารถนำไปประยุกต์กับการใช้งานที่ซับซ้อน เช่นการใช้งานร่วมกับ MySQL ซึ่งไว้มีโอกาสจะลองเขียนบทความให้หลากหลายเพื่อเป็นตัวอย่างไว้ให้ศึกษา 


สรุปถ้าจะให้เลือกใช้ระหว่าง NuSoap และ Soap ผมแนะนำให้ใช้ NuSoap ดีกว่า เพราะใช้งานและเข้าใจง่ายกกว่าครับ

และผิดพลาดประการใดขออภัยมา ณ ที่นี้ด้วยครับ
Posted by netdesign On 08:57 No comments READ FULL POST

Onsite computer repair Ramkhamhaeng.














รับซ่อมคอมพิวเตอร์นอกสถานที่ รามคำแหง บางกะปิ นวมินทร์ เสรีไทย


ค่าแรง+ค่ารถ+ลงwinodws+โปรแกรมดูหนังฟังเพลง+office+antivirus 400 บาท
ปล. ให้บริการ เฉพาะเขตพื้นที่ รามคำแหง บางกะปิ นวมินทร์ เสรีไทย ลาดพร้าวเฉพาะ บริเวณ จากเดอะมอลบางกะปิถึงโชคไชย4


สนใจติต่อ : Tel. 083-792-5426





ดูแผนที่ขนาดใหญ่ขึ้น


  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

ป้ายกำกับ

คลังบทความของบล็อก

Advertisement

Unordered List