สิ่งที่ต้องเตรียมตัว
- สมัคร Traffy developer account
- สร้าง key สำหรับส่งข้อมูลโดยกรอก ip สำหรับ server ที่ใช้ส่งข้อมูล
- login ที่หน้า api.traffy.in.th/apis/ กรณีที่ยังไม่ได้ลงทะเบียนให้ลงทะเบียนก่อน
- เมื่อเข้าระบบให้กดปุ่ม “เพิ่ม”
- กรอก ip address server ที่ท่านต้องการ sync ข้อมูลซึ่งต้องเป็น fixed ip เท่านั้น
ข้อมูลที่ใช้ส่ง
สำหรับข้อมูลที่ใช้ส่งนั้นเป็นลักษณะ XML โดยมีลักษณะข้อมูลดังนี้
โดยที่สีแดงหมายถึงข้อมูลที่ต้องใส่
<?xml verion=”1.0″ encoding=”UTF-8″ ?>
<gps>
<record>
<vehicle_id>TEXT</vehicle_id>
<gps_timestamp>YYYY-MM-dd HH:mm:SS</gps_timestamp>
<latitude>Double</latitude>
<longitude>Double</longitude>
<speed>Double</speed> (หน่วยเป็น km/hr)
<heading>Double</heading> (หน่วยเป็น องศา โดยที่ 0 องศาแปลว่าทิศเหนือ)
<altitude>Double</altitude> (m)
<organization_source>Text</organization_source>
<vehecle_type>Text</vehecle_type> (กรุณาระบุถ้าระบุได้เช่น taxi, van, bus, feet, public)
<taxi_passenger_flag>0</taxi_passenger_flag> (ถ้าระบุได้กรุณาใส่ โดยที่ 0 = ไม่มีผู้โดยสาร, 1 = มีผู้โดยสาร)
<gpsdata_valid_flag>1</gpsdata_valid_flag> (ถ้าสามารถระบุได้ว่าข้อมูล GPS นี้ถูกต้อง 1 = gps ทำงานถูกต้อง, 0 = gps ทำงานไม่ถูกต้อง )
<number_of_satellites>3</number_of_satellites>(ระบุจำนวนดาวเทียม)
</record>
</gps>
ตัวอย่างข้อมูลที่ใช้ส่ง
<?xml verion=”1.0″ encoding=”UTF-8″ ?>
<gps>
<record>
<vehicle_id>00000001</vehicle_id>
<gps_timestamp>2010-12-27 17:03:57</gps_timestamp>
<latitude>13.740378</latitude>
<longitude>100.534919</longitude>
<speed>0.2</speed>
<heading>5</heading>
<altitude>-5</altitude>
<organization_source>tester</organization_source>
<vehecle_type>taxi</vehecle_type>
<taxi_passenger_flag>0</taxi_passenger_flag>
<gpsdata_valid_flag>1</gpsdata_valid_flag>
<number_of_satellites>3</number_of_satellites>
</record>
<record>
<vehicle_id>00000001</vehicle_id>
<gps_timestamp>2010-12-27 17:03:58</gps_timestamp>
<latitude>13.740378</latitude>
<longitude>100.534919</longitude>
<speed>0.2</speed>
<heading>5</heading>
<altitude>-5</altitude>
<organization_source>tester</organization_source>
<vehecle_type>taxi</vehecle_type>
<taxi_passenger_flag>0</taxi_passenger_flag>
<gpsdata_valid_flag>1</gpsdata_valid_flag>
<number_of_satellites>3</number_of_satellites>
</record>
</gps>
วิธีการเขียน code
- สร้างข้อมูล xml ตามรูปแบบที่กำหนดไว้
- เขียนโปรแกรมให้ส่งข้อมูลไปที่ url: http://api.traffy.in.th/apis/apitraffy.php?api=postGPSData&appid=[appid]&key=[key]&format=XML
- ในช่อง [appid], [key] ให้กรอก appid กับ key ที่ลงทะเบียนไว้
- กำหนดชนิดของ message ข้อมูลที่ส่งให้เป็น application/xml
- กำหนด message ให้เป็น xml ที่เราสร้างไว้แล้ว
- ส่งข้อมูลและรอรับ response
- หาก server ตอบกลับว่า response “OK” แปลว่าการส่งข้อมูลถูกต้อง
ตัวอย่าง code php
<?php //url ที่ใช้ส่งข้อมูล โดยใช้ appid และ key ที่ได้สร้างไว้แล้ว $weburl = http://api.traffy.in.th/apis/apitraffy.php?api=postGPSData&appid=[xxx]&key=[xxx]&format=XML; //สร้าง xml string $string = <<<XML <?xml version='1.0' encoding="UTF-8"?> <gps> <record> <vehicle_id>00000001</vehicle_id> <gps_timestamp>2010-12-27 17:04:54</gps_timestamp> <latitude>13.740378</latitude> <longitude>100.534919</longitude> <speed>0.2</speed> <heading>144.8</heading> <altitude>-5</altitude> <organization_source>tester</organization_source> <vehecle_type>taxi</vehecle_type> <taxi_passenger_flag>0</taxi_passenger_flag> <gpsdata_valid_flag>1</gpsdata_valid_flag> <number_of_satellites>3</number_of_satellites> </record> <record> <vehicle_id>00000001</vehicle_id> <gps_timestamp>2010-12-27 17:05:55</gps_timestamp> <latitude>13.740378</latitude> <longitude>100.534919</longitude> <speed>0.2</speed> <heading>144.8</heading> <altitude>-5</altitude> <organization_source>tester</organization_source> <vehecle_type>taxi</vehecle_type> <taxi_passenger_flag>0</taxi_passenger_flag> <gpsdata_valid_flag>1</gpsdata_valid_flag> <number_of_satellites>3</number_of_satellites> </record> <record> <vehicle_id>00000001</vehicle_id> <gps_timestamp>2010-12-27 17:06:57</gps_timestamp> <latitude>13.740378</latitude> <longitude>100.534919</longitude> <speed>0.2</speed> <heading></heading> <altitude>-5</altitude> <organization_source>tester</organization_source> <vehecle_type>taxi</vehecle_type> <taxi_passenger_flag>0</taxi_passenger_flag> <gpsdata_valid_flag>1</gpsdata_valid_flag> <number_of_satellites>3</number_of_satellites> </record> </gps> XML; //สร้างการเชื่อมต่อ $ch = curl_init("$weburl"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //กำหนดลักษณะของข้อมูลที่ส่ง curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); curl_setopt($ch, CURLOPT_POST, 1); //กำหนดส่วน message post เป็นข้อมูล xml ที่เราสร้างไว้ curl_setopt($ch, CURLOPT_POSTFIELDS, $string); //ส่งข้อมูลและรับ response $opt = curl_exec($ch); header("Content-Type:text/xml"); //แสดงผล response จาก server echo $opt; curl_close($ch); ?>