Skip to content

Instantly share code, notes, and snippets.

@fddcddhdd
Last active January 10, 2018 02:10
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 fddcddhdd/1da656ddbdaf0a8edd07 to your computer and use it in GitHub Desktop.
Save fddcddhdd/1da656ddbdaf0a8edd07 to your computer and use it in GitHub Desktop.
kintone REST APIのupsert用PHPスクリプト(数百~数千くらいなら実用的に使えそう)
<?php
/*
kintone REST APIのupsert自動化の処理時間テスト。
時間かかかるので、コマンドラインから実行する( php upsert.php )
100件で試した所、処理時間は33-38秒だった。単純計算すると1時間で1万レコード?
1レコード毎にselect → update/insertしているので遅い(オーバーヘッドがありすぎ)
フィールド数が50なのは、csv/excelからのアプリ生成の上限だから(フィールド数は、速度に無関係っぽい)
Web APIのエラーコード一覧みたいなのが無いので、エラー処理が出来ない。
とりあえず、ログファイル出力して、問題があったら再実行かな…。
*/
// 自分のkintoneの設定
define("SUB_DOMAIN", "");
define("APP_NO", "");
define("API_TOKEN", "");
// 追加・更新するループ回数(3レコードで1秒強)
define("LOOP_NUM", 100);
//開始時間
$time_start = microtime(true);
// テスト用に独自IDと
for($rec_no=0; $rec_no<LOOP_NUM; $rec_no++){
//最初に既存レコードがあるかチェック!
//サーバ送信するHTTPヘッダを設定
$options = array(
'http'=>array(
'method'=>'GET',
'header'=> "X-Cybozu-API-Token:". API_TOKEN ."\r\n"
)
);
//コンテキストを生成
$context = stream_context_create( $options );
// サーバに接続してデータを貰う。条件式の部分だけURLエンコードを行う(イコールを使っているので)
$query = urlencode(sprintf("数値=%d", $rec_no));
$url = 'https://'. SUB_DOMAIN .'.cybozu.com/k/v1/records.json?app='. APP_NO ."&query=". $query;
$contents = file_get_contents($url, FALSE, $context );
//JSON形式からArrayに変換
$data = json_decode($contents, true);
//レコードがあったらUPDATE
if(!empty($data['records'])){
echo "update";
//HTTPヘッダ(レコード更新はPUT)
$options = array(
'http'=>array(
'method'=> 'PUT',
"header" => "X-Cybozu-API-Token: ". API_TOKEN ."\r\n" . 'Content-Type: application/json',
'content' => json_encode(
array(
'app' => APP_NO,
'id' => $data['records'][0]['$id']['value'],
// フィールド数が50なのは、csv/excelからのアプリ生成の上限だから
'record' => array(
"数値_0" => array("value" =>0),
"数値_1" => array("value" =>1),
"数値_2" => array("value" =>2),
"数値_3" => array("value" =>3),
"数値_4" => array("value" =>4),
"数値_5" => array("value" =>5),
"数値_6" => array("value" =>6),
"数値_7" => array("value" =>7),
"数値_8" => array("value" =>8),
"数値_9" => array("value" =>9),
"数値_10" => array("value" =>10),
"数値_11" => array("value" =>11),
"数値_12" => array("value" =>12),
"数値_13" => array("value" =>13),
"数値_14" => array("value" =>14),
"数値_15" => array("value" =>15),
"数値_16" => array("value" =>16),
"数値_17" => array("value" =>17),
"数値_18" => array("value" =>18),
"数値_19" => array("value" =>19),
"数値_20" => array("value" =>20),
"数値_21" => array("value" =>21),
"数値_22" => array("value" =>22),
"数値_23" => array("value" =>23),
"数値_24" => array("value" =>24),
"数値_25" => array("value" =>25),
"数値_26" => array("value" =>26),
"数値_27" => array("value" =>27),
"数値_28" => array("value" =>28),
"数値_29" => array("value" =>29),
"数値_30" => array("value" =>30),
"数値_31" => array("value" =>31),
"数値_32" => array("value" =>32),
"数値_33" => array("value" =>33),
"数値_34" => array("value" =>34),
"数値_35" => array("value" =>35),
"数値_36" => array("value" =>36),
"数値_37" => array("value" =>37),
"数値_38" => array("value" =>38),
"数値_39" => array("value" =>39),
"数値_40" => array("value" =>40),
"数値_41" => array("value" =>41),
"数値_42" => array("value" =>42),
"数値_43" => array("value" =>43),
"数値_44" => array("value" =>44),
"数値_45" => array("value" =>45),
"数値_46" => array("value" =>46),
"数値_47" => array("value" =>47),
"数値_48" => array("value" =>48)
)
)
)
)
);
$context = stream_context_create( $options );
$url = "https://4qn7v.cybozu.com/k/v1/record.json";
$contents = file_get_contents($url, FALSE, $context );
echo $contents;
// 無かったら、INSERT
}else{
echo "insert";
//HTTPヘッダ(新規レコードはPOST)
$options = array(
'http'=>array(
'method'=> 'POST',
"header" => "X-Cybozu-API-Token: ". API_TOKEN ."\r\n" . 'Content-Type: application/json',
'content' => json_encode(
array(
'app' => APP_NO,
// フィールド数が50なのは、csv/excelからのアプリ生成の上限だから
'record' => array(
"数値" => array("value" => $rec_no), //このフィールドが主キー扱い
"数値_0" => array("value" =>0),
"数値_1" => array("value" =>1),
"数値_2" => array("value" =>2),
"数値_3" => array("value" =>3),
"数値_4" => array("value" =>4),
"数値_5" => array("value" =>5),
"数値_6" => array("value" =>6),
"数値_7" => array("value" =>7),
"数値_8" => array("value" =>8),
"数値_9" => array("value" =>9),
"数値_10" => array("value" =>10),
"数値_11" => array("value" =>11),
"数値_12" => array("value" =>12),
"数値_13" => array("value" =>13),
"数値_14" => array("value" =>14),
"数値_15" => array("value" =>15),
"数値_16" => array("value" =>16),
"数値_17" => array("value" =>17),
"数値_18" => array("value" =>18),
"数値_19" => array("value" =>19),
"数値_20" => array("value" =>20),
"数値_21" => array("value" =>21),
"数値_22" => array("value" =>22),
"数値_23" => array("value" =>23),
"数値_24" => array("value" =>24),
"数値_25" => array("value" =>25),
"数値_26" => array("value" =>26),
"数値_27" => array("value" =>27),
"数値_28" => array("value" =>28),
"数値_29" => array("value" =>29),
"数値_30" => array("value" =>30),
"数値_31" => array("value" =>31),
"数値_32" => array("value" =>32),
"数値_33" => array("value" =>33),
"数値_34" => array("value" =>34),
"数値_35" => array("value" =>35),
"数値_36" => array("value" =>36),
"数値_37" => array("value" =>37),
"数値_38" => array("value" =>38),
"数値_39" => array("value" =>39),
"数値_40" => array("value" =>40),
"数値_41" => array("value" =>41),
"数値_42" => array("value" =>42),
"数値_43" => array("value" =>43),
"数値_44" => array("value" =>44),
"数値_45" => array("value" =>45),
"数値_46" => array("value" =>46),
"数値_47" => array("value" =>47),
"数値_48" => array("value" =>48)
)
)
)
)
);
$context = stream_context_create( $options );
$url = "https://4qn7v.cybozu.com/k/v1/record.json";
$contents = file_get_contents($url, FALSE, $context );
echo $contents;
}
}
// 処理にかかった時間を表示する
$time_end = microtime(true);
$time = $time_end - $time_start;
$min_sec_time = sprintf("%02d:%02d",$time / 60, $time % 60);
echo "\n\n exec_time = ". $min_sec_time."\n\n";
@alysbarzaga
Copy link

alysbarzaga commented Jan 10, 2018

Hi. How can I attached file to one record?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment