iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 20
1
自我挑戰組

使用PHP串接金流相關API系列 第 20

Day 19 - GOGOVAN (2) API 使用

GOGOVAN API

今天會依序介紹 GOGOVAN 提供的API 功能:

  • 費用試算
  • 送出訂單
  • 取消訂單
  • 訂單狀態查詢

都是使用一樣的 header

$header = array(
  'GoGoVan-API-Key: b87d2da0-d333-4dbd-b984-b7d05d232fa3',
  'GoGoVan-User-Language: zh-TW',
);

費用試算 API

GET https://gogovan-staging-tw.herokuapp.com/api/v0/orders/price.json

$postData = array(
  'order' => array(
	'name' => 'Eddie'
    , 'phone_number' => '0912345678'
	, 'pickup_time' => '2017-12-31T18:00:00Z'//可預約司機取件時間
	, 'vehicle' => "motorcycle"
	, 'title_prefix' => 'Corporate Order'
  )				
);

$locations = array(
  array(25.0552809,121.544679, '台北市松山區興安街174巷6號1樓')
  , array(25.1344639,121.5069234, '台北市北投區溫泉路68巷24號1樓')
);

參數拆開的原因可以參考昨天的文章

//curl setting
$ch = curl_init("https://gogovan-staging-tw.herokuapp.com/api/v0/orders/price.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlQuery);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);

得到完整 response

array(6) {
  ["breakdown"]=>
  array(2) {
    ["fee"]=>
    array(2) {
      ["title"]=>
      string(6) "運費"
      ["value"]=>
      int(209)
    }
    ["night_extra_charge"]=>
    array(2) {
      ["title"]=>
      string(15) "夜間附加費"
      ["value"]=>
      int(104)
    }
  }
  ["distance_in_kms"]=>
  string(6) "12.917"
  ["travel_time"]=>
  string(4) "1971"
  ["base"]=>
  int(209)
  ["total"]=>
  int(313)
  ["payment_method"]=>
  string(4) "cash"
}

送出訂單 API

POST https://gogovan-staging-tw.herokuapp.com/api/v0/orders.json

送出的參數和 費用試算 API 一樣,差別是要改成 POST 送出

$ch = curl_init("https://gogovan-staging-tw.herokuapp.com/api/v0/orders.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlQuery);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
//response order id
array(1) { ["id"]=> int(6932) }

取消訂單 API

POST 'https://gogovan-staging-tw.herokuapp.com/api/v0/orders/{order_id}/cancel.json'

將 order id 放在 url 上

curl_setopt($ch, CURLOPT_URL, "https://gogovan-staging-tw.herokuapp.com/api/v0/orders/6934/cancel.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
//刪除成功
string(0) ""

//刪除失敗
string(34) "{"status":404,"error":"Not Found"}"

訂單狀態查詢 API

GET 'https://gogovan-staging-tw.herokuapp.com/api/v0/orders/{order_id}.json''

一樣只需要將 order id 放在 url 上

curl_setopt($ch, CURLOPT_URL, "https://gogovan-staging-tw.herokuapp.com/api/v0/orders/6932.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close ($ch);
// 待取件
string(87) "
{"id":6934,"status":"pending","name":"Eddie","phone_number":"0912345678","price":313.0}"

//已取消
string(89) "{"id":6932,"status":"cancelled","name":"Eddie","phone_number":"0912345678","price":313.0}"

得到的 'status' 會有這四種狀況:

Pending - The order is not picked up by any driver yet
assigned - A driver has been assigned to the order
completed - The order has been marked "completed" by the driver
canelled - The order has been marked "cancelled" by the driver


上一篇
Day 18 - GOGOVAN (1) 介紹
下一篇
Day 20 - 電子發票(1) API申請
系列文
使用PHP串接金流相關API30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言