今天會依序介紹 GOGOVAN 提供的API 功能:
都是使用一樣的 header
$header = array(
'GoGoVan-API-Key: b87d2da0-d333-4dbd-b984-b7d05d232fa3',
'GoGoVan-User-Language: zh-TW',
);
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"
}
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) }
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"}"
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