PHP code example of tkc49 / kintone-sdk-for-wordpress
1. Go to this page and download the library: Download tkc49/kintone-sdk-for-wordpress library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tkc49 / kintone-sdk-for-wordpress example snippets
// Composerのオートローダーを読み込み
ntone接続情報
$kintone = array(
'domain' => 'your-subdomain.cybozu.com', // 末尾のスラッシュは不要
'app' => 123, // アプリID(数値)
'token' => 'your-api-token', // APIトークン
'basic_auth_user' => '', // Basic認証ユーザー(必要な場合)
'basic_auth_pass' => '', // Basic認証パスワード(必要な場合)
);
// 単一条件での取得
$records = Kintone_API::getRecords(
$kintone,
'ステータス in ("未処理", "処理中")', // クエリ
100, // 取得件数上限
0, // オフセット
array('レコード番号', '件名', 'ステータス') // 取得フィールド
);
if ( is_wp_error( $records ) ) {
// エラー処理
echo $records->get_error_message();
} else {
foreach ( $records as $record ) {
echo $record['件名']['value'] . PHP_EOL;
}
}
// 全レコード取得(500件を超える場合も自動的に取得)
$all_records = Kintone_API::getAllRecordsSortById(
$kintone,
'', // クエリ(空の場合は全件)
array('レコード番号', '件名') // 取得フィールド
);
// 1件のレコードを作成
$data = array(
'件名' => array(
'value' => '新しいタスク'
),
'ステータス' => array(
'value' => '未処理'
),
'詳細' => array(
'value' => 'タスクの詳細説明'
)
);
$result = Kintone_API::post( $kintone, $data );
if ( is_wp_error( $result ) ) {
// エラー処理
echo $result->get_error_message();
} else {
// 成功時:作成されたレコードのIDとリビジョン番号が返される
echo '作成されたレコードID: ' . $result['id'];
}
// 複数レコードの一括作成
$records = array(
array(
'件名' => array('value' => 'タスク1'),
'ステータス' => array('value' => '未処理')
),
array(
'件名' => array('value' => 'タスク2'),
'ステータス' => array('value' => '未処理')
)
);
$result = Kintone_API::posts( $kintone, $records );
// 1件のレコードを更新
$kintone['id'] = 100; // 更新対象のレコードID
$update_data = array(
'ステータス' => array(
'value' => '完了'
)
);
$result = Kintone_API::put( $kintone, $update_data );
if ( is_wp_error( $result ) ) {
echo $result->get_error_message();
} else {
echo '更新後のリビジョン: ' . $result['revision'];
}
// 複数レコードの一括更新
$update_records = array(
array(
'id' => 100,
'record' => array(
'ステータス' => array('value' => '完了')
)
),
array(
'id' => 101,
'record' => array(
'ステータス' => array('value' => '完了')
)
)
);
$result = Kintone_API::puts( $kintone, $update_records );
// 複数レコードの削除
$ids = array(100, 101, 102);
$result = Kintone_API::delete( $kintone, $ids );
if ( is_wp_error( $result ) ) {
echo $result->get_error_message();
} else {
echo 'レコードを削除しました';
}
// フォームフィールド情報の取得
$fields = Kintone_API::get_field_json( $kintone );
if ( is_wp_error( $fields ) ) {
echo $fields->get_error_message();
} else {
foreach ( $fields as $field_code => $field_info ) {
echo $field_info['label'] . ' (' . $field_info['type'] . ')' . PHP_EOL;
}
}
// ファイルのアップロード(フォームからのアップロード)
// $_FILES['file'] が存在する前提
$file_key = Kintone_API::get_attachement_file_key( $kintone );
$file_data = json_decode( $file_key['body'], true );
// レコード作成時にファイルを添付
$data = array(
'件名' => array('value' => 'ファイル付きレコード'),
'添付ファイル' => array(
'value' => array(
array('fileKey' => $file_data['fileKey'])
)
)
);
$result = Kintone_API::post( $kintone, $data );
// URLからファイルをアップロード
$file_key = Kintone_API::get_attachement_file_key_from_url(
$kintone,
'https://example.com/image.jpg',
'image.jpg'
);
$result = Kintone_API::post( $kintone, $data );
if ( is_wp_error( $result ) ) {
$error_code = $result->get_error_code();
$error_message = $result->get_error_message();
$error_data = $result->get_error_data();
// エラーログに記録
error_log( sprintf(
'Kintone API Error [%s]: %s',
$error_code,
$error_message
));
// 詳細なエラー情報がある場合
if ( $error_data ) {
error_log( print_r( $error_data, true ) );
}
}
$limit = 100;
$offset = 0;
$all_records = array();
do {
$result = Kintone_API::getRecords(
$kintone,
'',
$limit,
$offset
);
if ( is_wp_error( $result ) ) {
break;
}
$all_records = array_merge( $all_records, $result );
$offset += $limit;
} while ( count( $result ) === $limit );
// 現在のレコードを取得
$current = Kintone_API::getRecords(
$kintone,
'$id = "100"',
1
);
if ( ! is_wp_error( $current ) && ! empty( $current ) ) {
$update_key = array(
'field' => '更新日時',
'value' => $current[0]['更新日時']['value']
);
$update_data = array(
'ステータス' => array('value' => '完了')
);
$result = Kintone_API::put( $kintone, $update_data, $update_key );
}
define( 'WP_HTTP_BLOCK_EXTERNAL', false );
// Load Composer autoloader
ess\Kintone_API;
// Kintone connection settings
$kintone = array(
'domain' => 'your-subdomain.cybozu.com', // No trailing slash
'app' => 123, // App ID (numeric)
'token' => 'your-api-token', // API token
'basic_auth_user' => '', // Basic auth user (if needed)
'basic_auth_pass' => '', // Basic auth password (if needed)
);
// Get records with conditions
$records = Kintone_API::getRecords(
$kintone,
'Status in ("Pending", "In Progress")', // Query
100, // Limit
0, // Offset
array('Record_number', 'Title', 'Status') // Fields to retrieve
);
if ( is_wp_error( $records ) ) {
// Error handling
echo $records->get_error_message();
} else {
foreach ( $records as $record ) {
echo $record['Title']['value'] . PHP_EOL;
}
}
// Create a single record
$data = array(
'Title' => array(
'value' => 'New Task'
),
'Status' => array(
'value' => 'Pending'
)
);
$result = Kintone_API::post( $kintone, $data );
if ( is_wp_error( $result ) ) {
echo $result->get_error_message();
} else {
// Returns created record ID and revision
echo 'Created record ID: ' . $result['id'];
}
// Update a single record
$kintone['id'] = 100; // Record ID to update
$update_data = array(
'Status' => array(
'value' => 'Completed'
)
);
$result = Kintone_API::put( $kintone, $update_data );
// Delete multiple records
$ids = array(100, 101, 102);
$result = Kintone_API::delete( $kintone, $ids );
$result = Kintone_API::post( $kintone, $data );
if ( is_wp_error( $result ) ) {
$error_code = $result->get_error_code();
$error_message = $result->get_error_message();
error_log( sprintf(
'Kintone API Error [%s]: %s',
$error_code,
$error_message
));
}