ver.2サポートサイト

HOMEAPIサンプルプログラムサンプルプログラム(API1.1)DB管理(作成/取得/変更/削除)

DB管理(作成/取得/変更/削除)

cURLライブラリを使用して、DBの作成、取得、変更、削除をするサンプルプログラムです。
PHPにcurlライブラリが組み込まれている必要があります。

 

<?php

 

//SPIRAL Ver2のAPI用のURL
$base_url = "https://api.spiral-platform.com/v1/";
$url = $base_url."apps/{app}/dbs";

 

//APIキー
$apikey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

 

//ヘッダ
$header = array(
"Authorization:Bearer ".$apikey,
"Content-Type:application/json",

"X-Spiral-App-Authority"."manage",

);

 

 

//------------------------------
//insert
//------------------------------

 

// 作成するDBを設定
//ボディ
$body = [
"name" => "employee_mst",
"displayName" => "社員マスタ",
"fields" => [
array(
"name" => "employeeId",
"displayName" => "社員番号",
"type" => "integer"
),
array(
"name" => "name",
"displayName" => "名前",
"type" => "text"
),
array(
"name" => "email",
"displayName" => "メールアドレス",
"type" => "email"
),
array(
"name" => "phone",
"displayName" => "電話番号",
"type" => "phone"
),
array(
"name" => "gender",
"displayName" => "性別",
"type" => "select",
"optionsTemplate" => "gender"
),
array(
"name" => "zipCode",
"displayName" => "郵便番号",
"type" => "text"
),
array(
"name" => "pref",
"displayName" => "都道府県",
"type" => "select",
"optionsTemplate" => "prefJp"
),
array(
"name" => "age",
"displayName" => "年齢",
"type" => "select" ,
"options" => array(
array("id" => 1 , "label" => "10代以下"),
array("id" => 2 , "label" => "20代"),
array("id" => 3 , "label" => "30代"),
array("id" => 4 , "label" => "40代"),
array("id" => 5 , "label" => "50代"),
array("id" => 6 , "label" => "60代"),
array("id" => 7 , "label" => "70代以上")
),
),
array(
"name" => "address",
"displayName" => "住所",
"type" => "text"
),
array(
"name" => "entryDays",
"displayName" => "入社日",
"type" => "date"
)
]

 

];

 

//配列をjson形式に変換
$json_body = json_encode($body);

 

// curlライブラリを使用して送信
//セッションを初期化する
$curl = curl_init();

 

//実行結果を文字列で取得できるように設定
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//urlを指定
curl_setopt($curl, CURLOPT_URL , $url);
//ヘッダを指定
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
//ボディを指定
curl_setopt($curl, CURLOPT_POSTFIELDS , $json_body);
//メソッドを指定
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , 'POST');

 

//セッションを実行し、その結果を取得
$response = curl_exec($curl);

 

//cURLのエラーメッセージを表示
if (curl_errno($curl)) echo curl_error($curl);

 

//セッションを閉じる
curl_close($curl);

 

//json形式を配列に変換して表示
print_r(json_decode($response , true));

 

 

//------------------------------
//select(List)
//------------------------------

 

// 条件検索する場合はurlの末尾にクエリパラメータを付加

//日本語などのマルチバイト文字を使用する場合は、urlencodeを使いエンコードする必要がある

$url = $url."?query=". urlencode("会員");

 

// curlライブラリを使用して送信
//セッションを初期化する
$curl = curl_init();
//実行結果を文字列で取得できるように設定
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//urlを指定
curl_setopt($curl, CURLOPT_URL , $url);
//ヘッダを指定
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);

 

//セッションを実行し、その結果を取得
$response = curl_exec($curl);

 

//cURLのエラーメッセージを表示
if (curl_errno($curl)) echo curl_error($curl);

 

//セッションを閉じる
curl_close($curl);

 

//json形式を配列に変換して表示
print_r(json_decode($response , true));

 

 

//------------------------------
//select
//------------------------------

 

//urlの末尾に取得したいdbIdを付加
$url = $url."/100";

 

// curlライブラリを使用して送信
//セッションを初期化する
$curl = curl_init();
//実行結果を文字列で取得できるように設定
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//urlを指定
curl_setopt($curl, CURLOPT_URL , $url);
//ヘッダを指定
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);

 

//セッションを実行し、その結果を取得
$response = curl_exec($curl);

 

//cURLのエラーメッセージを表示
if (curl_errno($curl)) echo curl_error($curl);

 

//セッションを閉じる
curl_close($curl);

 

//json形式を配列に変換して表示
print_r(json_decode($response , true));

 

 

//------------------------------
//update
//------------------------------

 

//urlの末尾に変更したいdbIdを付加
$url = $url."/100";

 

// 変更したいアプリ情報を設定
//ボディ
$body = [
"name" => "employee_master",
"displayName" => "社員DB",
"addFields" => [
array(
"name" => "sub_emali",
"displayName" => "サブメールアドレス",
"type" => "email"
),
array(
"name" => "birthday",
"displayName" => "誕生日",
"type" => "date"
)
],
"updateFields" => [
array(
"id" => "1",
"displayName" => "社員ID"
),
array(
"id" => "7",
"addOptions" => [
array(
"id" => "48",
"label" => "海外"
)
]
)
],
"deleteFields" => [
"gender"
]
];

 

//配列をjson形式に変換
$json_body = json_encode($body);

 

// curlライブラリを使用して送信
//セッションを初期化する
$curl = curl_init();

 

//実行結果を文字列で取得できるように設定
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//urlを指定
curl_setopt($curl, CURLOPT_URL , $url);
//ヘッダを指定
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
//ボディを指定
curl_setopt($curl, CURLOPT_POSTFIELDS , $json_body);
//メソッドを指定
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , 'PATCH');

 

//セッションを実行し、その結果を取得
$response = curl_exec($curl);

 

//cURLのエラーメッセージを表示
if (curl_errno($curl)) echo curl_error($curl);

 

//セッションを閉じる
curl_close($curl);

 

//json形式を配列に変換して表示
print_r(json_decode($response , true));

 

 

//------------------------------
//delete
//------------------------------

 

//urlの末尾に削除したいdbIdを付加
$url = $url."/100";

 

// curlライブラリを使用して送信
//セッションを初期化する
$curl = curl_init();

 

//実行結果を文字列で取得できるように設定
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//urlを指定
curl_setopt($curl, CURLOPT_URL , $url);
//ヘッダを指定
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
//メソッドを指定
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , 'DELETE');

 

//セッションを実行し、その結果を取得
$response = curl_exec($curl);

 

//cURLのエラーメッセージを表示
if (curl_errno($curl)) echo curl_error($curl);

 

//セッションを閉じる
curl_close($curl);

 

//json形式を配列に変換して表示
print_r(json_decode($response , true));

 

?>

※API1.0の記述についてはこちらをご覧ください。