1) การสร้างเว็บเซอร์วิสแบบ REST ที่ใช้ JSON
ถ้าหากใช้ PHP 5.2.0 ขึ้นไป มี extension ที่ใช้ในการสร้างข้อมูลแบบ JSON โดยเราสามารถใช้ฟังก์ชัน json_encode
ตัวอย่าง
<?php
$arr = array();
$arr[0] = "Ta";
$arr[1] = "Tom";
echo '{"sample":' .json_encode($arr).'}';
?>
2) การเรียกใช้เว็บเซอร์วิสแบบ REST ที่ใช้ JSON
ถ้าหากใช้ PHP 5.2.0 ขึ้นไป มี extension ที่ใช้ในการสร้างข้อมูลแบบ JSON โดยเราสามารถใช้ฟังก์ชัน json_decode
ตัวอย่าง
<?php
$url = 'http://maps.googleapis.com/maps/api/directions/json?origin=Khon%20Kaen&destination=Bangkok&sensor=false';
$myFile = file_get_contents($url);
$obj = json_decode($myFile);
$routes = $obj->{'routes'};
$route = $routes[0];
$copyrights = $route->{'copyrights'};
echo $copyrights;
อ้างอิง
http://www.sencha.com/learn/legacy/Tutorial:Creating_JSON_Data_in_PHP
ไม่มีความเห็น