ใช้งาน RESTful Services ใน CodeIgniter ด้วย Rest Server
เขียนเมื่อ 7 ปีก่อน โดย Ninenik Narkdeerestful codeigniter 3 phprequests codeigniter
คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ restful codeigniter 3 phprequests codeigniter
ไปที่
Copy
RESTful API เป็นรูปแบบหนึ่งของบริการ การส่งผ่านข้อมูลระหว่าง server
หรือ web application นิยมใช้เป็น web service เช่น
ให้บริการข้อมูลบางรายการที่มีการร้องขอด้วย HTTP GET
สร้างรายการข้อมูลใหม่ด้วย HTTP POST
อัพเดทรายการข้อมูลที่ร้องขอโดยส่งค่า parameter เข้ามาอัพเดท ด้วย HTTP PUT
หรือลบรายการข้อมูลด้วย HTTP DELETE
เนื่องจากเนื้อหาส่วนนี้ค่อนข้างยาว จะขอทำเป็นหลายบทความ
ก่อนอื่นตรวจสอบความต้องการของระบบก่อนว่า มีสองส่วนนี้รองรับหรือยัง
รันด้วย PHP 5.4 ขึ้นไป
ใช้งานกับ CodeIgniter 3.0 ขึ้นไป
การติดตั้ง Rest Server ใน codeigniter
แล้วกดปุ่มดาวน์โหลด Download ZIP
จากนั้นทำการแตกไฟล์ แล้ว copy ไฟล์ตามนี้
- application/libraries/Format.php
- application/libraries/REST_Controller.php
มาไว้ในโฟลเดอร์ apps > libraries ของ project codeigniter
- application/config/rest.php
มาไว้ในโฟลเดอร์ apps > config ของ project codeigniter
- application/language/enlisgh/rest_controller_lang.php
มาไว้ในโฟลเดอร์ apps > language > english ของ project codeigniter
ตัวอย่างและรูปแบบการใช้งาน
ในที่นี้เราจะสร้างโฟลเดอร์ชื่อ api ใน apps > controllers
และสร้างไฟล์ controller ด้านในชื่อ News.php
apps > controllers > api > News.php
สมมติว่าเราจะทำ REST API เกี่ยวกับข่าว จะได้ไฟล์ News.php ดังนี้
<?php defined('BASEPATH') OR exit('No direct script access allowed'); require(APPPATH.'libraries/REST_Controller.php'); class News extends REST_Controller { public function index_get() { // ดึงรายการข่าวทั้งหมด } public function index_post() { // สร้างรายการข่าวใหม่ } public function index_put() { // แก้ไขรายการข่าว } public function index_delete() { // ลบรายการข่าว } }
ลองสมมติเราทำส่วนของ GET กันดู ให้ข้อมูลข่าวเราเป็นดังนี้
<?php defined('BASEPATH') OR exit('No direct script access allowed'); require(APPPATH.'libraries/REST_Controller.php'); class News extends REST_Controller { public function index_get() { $data = array( array( "id"=>1, "topic"=>"หัวข้อข่าวที่ 1" ), array( "id"=>2, "topic"=>"หัวข้อข่าวที่ 2" ), array( "id"=>3, "topic"=>"หัวข้อข่าวที่ 3" ), ); $this->response($data, 200); // แสดงรายการข่าวทั้งหมด } }
เมื่อเราทดสอบเรียกดูข้อมูลแบบ GET โดยลองเรียกดูข้อมูลอย่างง่ายผ่าน url
http://localhost/learnci/api/news/
เราก็จะได้ข้อมูลออกมาในรูปแบบ json ดังนี้
[{"id":1,"topic":"\u0e2b\u0e31\u0e27\u0e02\u0e49\u0e2d\u0e02\u0e48\u0e32\u0e27\u0e17\u0e35\u0e48 1"}, {"id":2,"topic":"\u0e2b\u0e31\u0e27\u0e02\u0e49\u0e2d\u0e02\u0e48\u0e32\u0e27\u0e17\u0e35\u0e48 2"}, {"id":3,"topic":"\u0e2b\u0e31\u0e27\u0e02\u0e49\u0e2d\u0e02\u0e48\u0e32\u0e27\u0e17\u0e35\u0e48 3"}]
(รูปแบบ json เป็นค่าเริ่มตั้นเราสามารถกำหนด format
ที่ต้องการได้เป็น json , xml , csv , html , php และ serialize)
แต่ส่วนใหญ่แล้วเราจะใช้เป็น json และ xml
ตัวอย่าง url การเรียกแสดง format ตามตั้งการ จะเป็น
http://localhost/learnci/api/news/index/format/xml
http://localhost/learnci/api/news/index/format/json * ค่าเริ่มต้น ไม่ต้องกำหนด format ก็ได้
ตัวอย่างรูปแบบ xml เมื่อเรียกผ่าน url
http://localhost/learnci/api/news/index/format/xml
<? xml version = "1.0" encoding = "utf-8"?> <xml> <item> <id>1</id> <topic>หัวข้อข่าวที่ 1</topic> </item> <item> <id>2</id> <topic>หัวข้อข่าวที่ 2</topic> </item> <item> <id>3</id> <topic>หัวข้อข่าวที่ 3</topic> </item> </xml>
เราจะเห็นว่าตรง url จะมีคำว่า /index/ ตรงนี้คือชื่อฟังก์ชั่น จากตัวอย่างของเราเป็น index_get
เราจะใช้แค่ index หรือข้อความก่อน _get มาเป็นชื่อฟังก์ชั่น
ซึ่งปกติ ถ้าเป็นชื่อว่า index เราไม่จำเป็นต้องกำหนดก็ได้ กรณีที่ไม่มีค่าอื่นเพิ่มติม
นั่นก็คือ
http://localhost/learnci/api/news/ เท่ากับ http://localhost/learnci/api/news/index/
ทีนี้เราทดสอบเรียกดูข้อมูล โดยเราจะใช้ Requests for PHP จากเนื้อหาตอนที่แล้ว
เรียกใช้ Requests for PHP สำหรับใช้งาน HTTP library ใน codeigniter
http://www.ninenik.com/content.php?arti_id=701 via @ninenik
จะได้เป็นไฟล์ตัวอย่างการดึงข้อมูล ดังนี้ ไฟล์ Example.php ในโฟลเดอร์ apps > controllers
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Example extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('PHPRequests'); } public function index(){ $response = Requests::get('http://localhost/learnci/api/news/'); $responseData = $response->body; // ได้ข้อมูล json กลับมา // แปลงข้อมูลกลับ และให้เป็น array $arrData = json_decode($responseData,true); echo "<pre>"; print_r($arrData); // ทดสอบแสดงข้อมูลจากตัวแปร array echo "</pre>"; } }
เมื่อเราทดสอบรันไฟล์ผ่าน url
http://localhost/learnci/example/
เราก็จะได้ผลลัพธ์โครรงสร้างของ array ดังรูปด้านล่าง
Array ( [0] => Array ( [id] => 1 [topic] => หัวข้อข่าวที่ 1 ) [1] => Array ( [id] => 2 [topic] => หัวข้อข่าวที่ 2 ) [2] => Array ( [id] => 3 [topic] => หัวข้อข่าวที่ 3 ) )
เนื้อหาตอนหน้า จะต่อในเรื่องของการสร้าง RESTful API สำหรับการ POST
กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ

อ่านต่อที่บทความ
-
04 May2016ภาคต่อ ใช้งาน RESTful Services ฟังก์ชั่นสำหรับ POST ข้อมูล อ่าน 9,540
จากเนื้อหาตอนที่แล้วเราได้รู้จักการใช้งาน Rest Server ในการสร้าง RESTful
เนื้อหาที่เกี่ยวข้อง
-
02 May2016เรียกใช้ Requests for PHP สำหรับใช้งาน HTTP library ใน codeigniter อ่าน 7,043
เนื้อหาตอนนี้จะเป็นการแนะนำวิธีเรียกใช้งาน Requests for PHP สำหรับ ใช้งาน
-
กำลังอ่านเนื้อหานี้อยู่03 May2016ใช้งาน RESTful Services ใน CodeIgniter ด้วย Rest Server อ่าน 18,173
RESTful API เป็นรูปแบบหนึ่งของบริการ การส่งผ่านข้อมูลระหว่าง server
-
04 May2016ภาคต่อ ใช้งาน RESTful Services ฟังก์ชั่นสำหรับ POST ข้อมูล อ่าน 9,540
จากเนื้อหาตอนที่แล้วเราได้รู้จักการใช้งาน Rest Server ในการสร้าง RESTful
URL สำหรับอ้างอิง
Top
Copy
ขอบคุณทุกการสนับสนุน
![]()