DBに登録されているデータを登録フォームに表示
PHPとAPIを使用して、DBに登録されているデータを登録フォーム上に表示するサンプルプログラムです。
ページのPHP
<?php
//------------------------------------------------
//DBのデータを画面に表示
//------------------------------------------------
$docbody = "";//変数を初期化
$apikey = $SPIRAL->getEnvValue("apikey");//あらかじめPHP環境変数にAPIキーを登録しておきます。
$apiurl = $SPIRAL->getEnvValue("apiurl");//あらかじめPHP環境変数にAPIのURL「https://api.spiral-platform.com/v1/apps/」を登録しておきます。
$header = array(
"Authorization:Bearer " . $apikey,
"Content-Type:application/json",
);
//DBに登録されているデータを取得
$url = $apiurl . "{appid}/dbs/{dbid}/records/{recordid}";
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL , $url);
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , "GET");
$response = curl_exec($curl);
if (curl_errno($curl)) echo curl_error($curl);
curl_close($curl);
$data = json_decode($response , true);
if(array_key_exists('status', $data)){//APIのエラーが発生している場合
if($data["status"] != 200){
$SPIRAL->setTHValue("APIERROR", $data["message"]);//APIのエラーをThymeleafにセット
}
}else{
$docbody = $data["item"]["docbody"];//データを取得
$SPIRAL->setTHValue("docbody", $docbody);//データをThymeleafにセット
}
?>
ブロックの登録フォームソース
<div th:if="${cp.result.isSuccess}">
<p th:text="${cp.result.value['docbody']}"></p>
<p th:text="${cp.result.value['APIERROR']}"></p>
</div>
<div th:if="${!cp.result.isSuccess}">
<p th:text="${cp.result.errorMessage}">error message</p>
</div>
