Skip to content

Instantly share code, notes, and snippets.

@hideokamoto
Last active March 7, 2017 16:18
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 hideokamoto/f5e7d78155e828ec1125452e7d34d309 to your computer and use it in GitHub Desktop.
Save hideokamoto/f5e7d78155e828ec1125452e7d34d309 to your computer and use it in GitHub Desktop.
<?php
/**
* kintone API Shortcode example
*
* License: GNU General Public License v2 or later
**/
add_shortcode ( 'building-list' , 'kintone_building_list');
function kintone_building_list( $atts ) {
$atts = shortcode_atts(
array(
'api_token' => '',
'fields' => '物件名,物件所在地,オーナー電話番号,管理会社住所,オーナー企業名_氏名,管理会社電話番号,オーナー住所,管理会社名',
'kintone_api_base' => '',
'search' => '',
'app_id' => '',
),
$atts
);
$html = kintone_get_content( $atts['api_token'], $atts['app_id'], $atts['kintone_api_base'], $atts['fields'], $atts['search'] );
return $html;
}
function kintone_get_content( $api_token, $id, $kintone_api_base, $fields, $search ) {
$content = '';
$url = "{$kintone_api_base}/k/v1/records.json?app={$id}&fields={$fields}";
if ( $search ) {
$url .= '&query='. utf8_uri_encode( $search );
}
$args = array(
'headers' => array(
'X-Cybozu-API-Token' => $api_token
)
);
$kintone = wp_remote_get( $url, $args );
if ( is_wp_error( $kintone ) ) {
return $content;
}
$kintones = json_decode($kintone['body'],true);
if ( ! isset( $kintones['records'] ) ) {
return $content;
}
foreach( $kintones['records'] as $key => $kintone ) {
$content .= "<dl class='col-xs-4'>";
foreach( $kintone as $k => $v ) {
if ( is_array($v['value'] ) ) {
continue;
}
$t = esc_html( $k );
$val = esc_html( $v['value'] );
if ( $val ) {
$content .= "<dt>{$t}</dt><dd>{$val}</dd>";
}
}
$content .= '</dl>';
}
return $content;
}

ショートコードとして利用できます。

# APP id 2の不動産アプリから情報を取得する
[building-list api_token="YOUR_KINTONE_APP_API_TOKEN" app_id="2" kintone_api_base="https://xxxx.cybozu.com"]


# APP id 2の不動産アプリから物件名に「サンプル」が入っている物件を取得する
[building-list api_token="YOUR_KINTONE_APP_API_TOKEN" app_id="2" kintone_api_base="https://xxxx.cybozu.com" search="物件名 like \"サンプル\""]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment