Skip to content

Instantly share code, notes, and snippets.

@banqhsia
Last active November 18, 2017 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banqhsia/e157a68f730785c0727481d57e5325e0 to your computer and use it in GitHub Desktop.
Save banqhsia/e157a68f730785c0727481d57e5325e0 to your computer and use it in GitHub Desktop.
交通部 PTX 串接範例 (PHP)
<?php
namespace App\Assist;
use Carbon\Carbon;
class MotcApiAuth {
/**
* Construct
*/
public function __construct()
{
$this->timestamp = $this->getTime();
$this->appId = env('MOTC_API_ID');
$this->appKey = env('MOTC_API_KEY');
}
/**
* Auth Header 資料
*
* 取得即將作為認證資訊的 header 資料
*/
public function getHeaders()
{
// 簽章
$signature = $this->getSignature();
// 認證欄位
$authorization = 'hmac username="'.$this->appId.'", algorithm="hmac-sha1", headers="x-date", signature="'.$signature.'"';
return [
'Authorization' => $authorization,
'x-date' => $this->timestamp,
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip, deflate'
];
}
/**
* 簽章
*
* 取得簽章資料,使用 hmac sha1,帶入時間與 app_key 以 hash
*/
private function getSignature()
{
return base64_encode(hash_hmac('sha1', 'x-date: '.$this->timestamp, $this->appKey, true )) ;
}
/**
* 時間
*
* 取得當下 UTC 時間,並手動設定為指定格式
*/
private function getTime()
{
// Mon, 23 Oct 2017 12:00:00 GMT';
return Carbon::now()->tz('UTC')->format('D, d M Y H:i:s \G\M\T');
}
}
<?php
namespace App\Assist;
class Request {
const MOTC_TRA_BASE = 'https://ptx.transportdata.tw/MOTC/v2/Rail/TRA';
const MOTC_TRA_FORMAT = '?$format=JSON';
/**
* 要進行請求的網址名稱
*/
private $url_set;
/**
* 傳入的參數 (起迄站、時間等)
*/
private $args;
/**
* 取代參數後,即將用來請求的網址
*/
private $translatedUrl;
/**
* Construct
*/
public function __construct ()
{
$this->MotcApiAuth = new MotcApiAuth;
}
/**
* Handler
**/
public function get ( $url_set = NULL, $args = [] )
{
$this->url_set = $url_set;
$this->args = $args;
$result = $this->sendRequest();
// 如果抓不到訊息
if ( empty ( $result ) || !$result ) {
return false;
}
return $result;
}
/**
* 送出請求
*
* 向臺鐵伺服器送出請求,傳回結果
*/
private function sendRequest()
{
// 取得網址
$this->getURL();
// 送出查詢
$response = \Guzzle::request('get', $this->translatedUrl, [
'headers' => $this->MotcApiAuth->getHeaders(),
'http_errors' => false,
'connect_timeout' => 15,
] );
// 如果請求不成功
if ( $response->getStatusCode() != 200 ) {
return false;
}
// Parse JSON
return json_decode( $response->getBody() );
}
/**
* 取得網址
*
* 按照給定的名稱、參數 取得網址
*/
private function getURL()
{
// 設定
$url = [
"DailyTimetable" => "/DailyTimetable/{0}/{1}",
"DailyTimetableOD" => "/DailyTimetable/OD/{0}/to/{1}/{2}",
"ODFare" => "/ODFare/{0}/to/{1}",
];
/**
* 即將用來請求的網址
*
* 將網址依照給定的參數,填入並組合成可供請求的網址
*/
$this->translatedUrl = (function() use ($url) {
$translatedUrl = $url[$this->url_set];
// 將變數填入預留的位置
foreach ($this->args as $key => $value){
$translatedUrl = str_replace("{".$key."}", $this->args[$key], $translatedUrl);
}
// 依照給給定的網址base與資料格式,組合網址
return self::MOTC_TRA_BASE.$translatedUrl.self::MOTC_TRA_FORMAT;
})();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment