サンプルプログラム
サンプルプログラム
-
時間タイプ
-
スクールタイプ
-
会議室タイプ
-
イベントタイプ
予約データ一覧を取得するサンプルプログラム(PHP)
- <?php
-
- $handle = curl_init();
- $url = 'https://[予約サイトURL]/api/v2/reservations';
- $api_key = '[APIキー]';
-
- // API設定でレスポンスを
- // 「共通鍵方式で暗号化する」に設定している場合(復号する): true
- // 「暗号化しない」に設定している場合は: false
- $enabled_decrypt = true;
- // 「共通鍵方式で暗号化する」に設定している場合は共通鍵を設定
- $commonkey = hex2bin('[共通鍵]');
- // 暗号化アルゴリズムは「AES-256-GCM」を使用
- $chipher_algo = 'aes-256-gcm';
-
- // リクエストパラメータ
- $params = [
- 'offset' => 0,
- 'count' => 10,
- 'registered_datetime_from' => '2022/02/03 15:00:00',
- ];
-
- // URLを生成
- $url = $url . '?' . http_build_query($params);
-
- // cURLオプションを設定
- curl_setopt_array($handle, array(
- CURLOPT_URL => $url, // 取得するURL
- CURLOPT_RETURNTRANSFER => true, // curl_exec()の戻り値を文字列で取得する
- CURLOPT_TIMEOUT => 0, // cURL関数実行のタイムアウトを無制限にする
- CURLOPT_FOLLOWLOCATION => true, // Locationヘッダーの内容をたどる
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // HTTP/1.1 を使用する
- CURLOPT_CUSTOMREQUEST => 'GET', // HTTPリクエストメソッド
- CURLOPT_HTTPHEADER => array(
-
"Authorization: {$api_key}" // ヘッダーのAuthorizationにAPIキーを設定する
- ),
- ));
-
- // cURLを実行
- $response = curl_exec($handle);
- // HTTPステータスコードを取得
- $httpcode = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
- // レスポンスデータの処理
- $api_response = "";
-
if ($response) {
- // 受け取ったレスポンスを整形
- $api_response = json_decode($response, JSON_PRETTY_PRINT);
-
if ($httpcode === 200) {
- // リクエスト成功
- } else {
- // リクエスト失敗
- }
- } else {
- // リクエストエラー
- echo curl_error($handle);
- }
-
- // レスポンスを復号する
-
if ($enabled_decrypt && isset($api_response['encrypted_results'])) {
- // encrypted_results の形式
- // [IV(12バイト)] + [Tag(16バイト)] + [Ciphertext(可変長)] をbase64エンコードした文字列
- // 復号してresultsのJSONを取得
- $encrypted_data = base64_decode($api_response['encrypted_results']);
- $iv = substr($encrypted_data, 0, 12);
- $tag = substr($encrypted_data, 12, 16);
- $ciphertext = substr($encrypted_data, 28);
- $results_json = openssl_decrypt($ciphertext, $chipher_algo, $commonkey, OPENSSL_RAW_DATA, $iv, $tag);
-
if ($results_json === false) {
-
throw new RuntimeException('復号に失敗しました。共通鍵またはデータの整合性を確認してください。');
- }
-
- // 復号結果をJSONデコード
- $results = json_decode($results_json, true);
-
if ($results === null || json_last_error() !== JSON_ERROR_NONE) {
-
throw new RuntimeException('JSONデコードに失敗しました。レスポンスの形式を確認してください。');
- }
- $api_response['results'] = $results;
-
// encrypted_resultsを削除
- unset($api_response['encrypted_results']);
- }
-
- // 結果を表示
- header("Content-Type: application/json; charset=utf-8");
- print_r($api_response);