blog ตอนที่แล้ว ดูภาพจากกล้อง CCTV ทั่วกรุงเทพ ด้วย TraffyAPI (และ Drupal7) ได้ทำ drupal7 module เพื่อทดสอบ Traffy API ก็ได้พบกับปัญหาจุกจิกเล็กๆน้อย ปัญหาหลักๆที่กวนใจก็คือเรื่อง http referer เนี่ยแหละ แต่จริงๆผมอาจจะติดกับพวก Public Facebook API หรือ Twitter API มากเกินไปที่ไม่ต้องใช้ http referrer
หลังจากที่ลองเล่น Traffy API มานิดนึง เพื่อหวังหาไอเดียทำอะไรซักอย่าง ก็เจอกับปัญหา referrer เข้าอย่างจัง มีวันนึงผมจะเขียน google chrome extension แต่ว่าเจอปัญหา Request ไปขอข้อมูลอะไรไม่ได้เลย เพราะมึนเรื่อง http referrer เนี่ยแหละ
- หลักการตั้งชื่อไม่เหมือนกัน มีหลายแบบมากดังข้างล่าง * getLinkALLInfo * getCCTVimg * getCCTV
- api ในหัวเอกสารไม่ตรงกับที่ต้องเรียกจริงๆ * เอกสาร: getTrafficCongestion แต่จริงๆแล้วต้องเรียก getCL ดูหัวเอกสารที่ info.traffy * เอกสาร: getCCTVimg แต่จริงๆ ต้องเรียก getcctvimg ดูหัวเอกสารที่ info.traffy
- case sensitive ที่ชื่อ api และ format ถ้าไม่ใช่ XML/JSON/TABLE มันเจ๊งงง
- output จาก api มีไม่เหมือนกันเลย เช่นบางอัน มี JSON บางอันไม่มี เป็นต้น
ผมเลยทำ Traffy API Wrapper ซะเลย แต่ไม่รู้ว่าจะเรียกว่า Wrapper หรือ Proxy ดี แต่ชอบคำว่า Wrapper มากกว่า มันดูเท่ดี (ว่าไปนั่น)
หน้าที่ของ module Traffy API Wrapper มีหน้าที่แค่ รับ request จากทางบ้าน แล้ว request ต่อไปที่ Server Traffy API
อ้าว!! แล้วมันแตกต่างกับการเรียกใช้ Traffy API ของ Nectec ยังไงล่ะ?
- สามารถใช้ได้เลย ไม่ต้องใช้ apikey และ appid (เฉพาะ api ที่ผมคิดว่า ไม่จำเป็นต้อง authenticate)
- (อาจจะ) ไม่ต้องปวดหัวกับ ปัญหา referrer
- map api ให้ตรงกับหัวเอกสาร (เช่น 'getTrafficCongestion' => 'getCL', 'getCCTVimg' => 'getcctvimg') แก้ไขปัญหาหัวเอกสารไม่ตรงกับ api
- ปรับให้ใช้เรียก api โดยไม่สนใจอักขระตัวเล็กตัวใหญ่ (case insensitive) ทำให้สามารถเรียก getcctv, getcctvimg, gettrafficcongestion ได้เลย
- cache traffy api ดังนี้ cache api เป็นเวลา 5 นาที ดังนี้ 'getcctv' => array('cid' => 'traffy_'.$api_to_call, 'type' => 'cache_page', 'expire' => 560), 'getcctvimg' => array('cid' => 'traffy_'.$api_to_call, 'type' => 'cache_page', 'expire' => 560), );
- ผมมีแผนที่จะแคชข้อมูลบางตัวให้ เพื่อแก้ไขปัญหาความช้าของ Server ที่ส่งผลลัพธ์
- ลืมแล้วว่าคิดอะไรไว้ !!
- เลือก api ที่ต้องการใช้งานจาก Traffy Info แต่แนะนำให้ดู API จากเมนูด้านบน ข้างๆเมนู "หน้าแรก" หรือ ตาม ข้างล่าง
$map = array( 'getcctvimg' => 'getcctvimg',
'getcctv' => 'getCCTV',
'getvms' => 'getVMS',
'getvmsimg' => 'getvmsimg',
'getincident', => 'getIncident',
'getlinkallinfo' => 'getLinkALLInfo',
'getrainforecast' => 'getRainForecast',
'gettrafficcongestion' => 'getCL',
'getlinkinfo' => 'getLinkInfo',
'gettraveltime' => 'getTravelTime',
//postAPI
'postgpsdata' => 'postGPSData',
'postincident' => 'postIncident',
);
-
ผมไม่ได้ handle เวลาที่ api error
-
request api ไปที่ path http://www.together.in.th/drupal/traffy/wrapper/(api-to-call) ตัวอย่างเช่น
หรือ request แบบพิเศษได้ประมาณนี้ http://www.together.in.th/drupal/traffy/wrapper/getcctv/[HEADER]
หรือ reques แบบส่ง [HEADER] ไปทาง query string ($_GET['header']) ก็ได้ อย่างเช่น
รายชื่อ header ที่ทั้งหมดที่ใช้ได้
$map = array(
'img' => 'image/jpeg',
'image' => 'image/jpeg',
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'html' => 'text/html',
'xml' => 'text/xml',
'javascript' => 'application/javascript',
'js' => 'application/javascript',
'json' => 'application/json',
);
//bad
gettrafficcongestion มีปัญหาเพราะ parameter $_GET['q'] มันซ้ำกัน
getlinkallinfo ต้องระบุ format = csv/table
- ไม่ handle เหตุการณ์เมื่อมี error
-
http://www.together.in.th/drupal/traffy/wrapper/getcctvimg?id=255
request แบบพิเศษ http://www.together.in.th/drupal/traffy/wrapper/getcctv/[HEADER]
-
http://www.together.in.th/drupal/traffy/wrapper/getcctv/json
-
http://www.together.in.th/drupal/traffy/wrapper/getcctvimg/JPEG/?id=255
request แบบส่ง [HEADER] ไปทาง query string ($_GET['header'])
-
http://www.together.in.th/drupal/traffy/wrapper/getcctvimg/?id=255&header=jpeg
อื่นๆ
-
http://www.together.in.th/drupal/traffy/wrapper/getlinkallinfo?format=csv
-
http://www.together.in.th/drupal/traffy/wrapper/getvms?format=json&header=json
-
http://www.together.in.th/drupal/traffy/wrapper/getvmsimg/png/?id=3
-
http://www.together.in.th/drupal/traffy/wrapper/getvmsimg/?id=3&header=png