Ryu Yamashita様
お世話になります。
アドバイスいただきありがとうございます。
RequestBinはphpでも使用できることを失念しておりました。
RequestBinでecho出力内容の確認とステータス更新APIのドキュメント再確認し、無事にステータス更新することができました。
結果的に以下3点がエラーの原因でした。
①追加処理の記載場所の間違い(GET・PUTリクエストがスルーされていた)
②PUTリクエストボディの形式の間違い
③phpファイル保存時の文字コードの間違い
①の追記場所についてですが、 if ( is_wp_error( $res_put ) ) { の下ではなく、} else { の下に記載が正しかったです。
②PUTリクエストについてはステータス更新APIのドキュメントの通り修正しました。
③phpファイルの文字コード違いについては、ステータスが日本語だったのでphpファイルの保存時に、文字コードをUTF-8と指定して保存しました。
最終的には、以下のようなコードになりました。
==========================================================================================-
/*
* Send form data to Kintone API
* @param string $kintone Shortcode attributes it need for auth.
* @param string $data $_POST data
* @return array true or WP_Error object
* @since 0.1
*/
private static function save_data( $kintone, $data )
{
$url = sprintf(
‘https://%s.cybozu.com/k/v1/record.json’,
$kintone[‘sub_domain’]
);
if ( isset( $kintone[‘basic_auth_user’] ) && isset( $kintone[‘basic_auth_pass’] ) ) {
$headers = Kintone_API::get_request_headers( $kintone[‘token’], $kintone[‘basic_auth_user’], $kintone[‘basic_auth_pass’] );
} else {
$headers = Kintone_API::get_request_headers( $kintone[‘token’] );
}
if ( is_wp_error( $headers ) ) {
return $headers;
}
$headers[‘Content-Type’] = ‘application/json’;
$record = self::parse_record( $data );
$body = array(
‘app’ => $kintone[‘app’],
‘record’ => $record,
);
$res = wp_remote_post(
$url,
array(
‘method’ => ‘POST’,
‘headers’ => $headers,
‘body’ => json_encode( $body ),
)
);
if ( is_wp_error( $res ) ) {
return $res;
} elseif ( $res[‘response’][‘code’] !== 200 ) {
$message = json_decode( $res[‘body’], true );
$e = new WP_Error();
$e->add( ‘validation-error’, $message[‘message’], $message );
return $e;
} else {
/*###############################
* 追加処理 開始
*/###############################
$url_get = ‘https://<サブドメイン名>.cybozu.com/k/v1/records.json?app=382’;
$headers_get = array( ‘X-Cybozu-API-Token’ => ‘<APIトークン>’ );
$res_get = wp_remote_get(
$url_get,
array(
‘headers’ => $headers_get
)
);
$return_value = json_decode( $res_get[‘body’], true );
$recno = $return_value[‘records’][0][‘record_no’][‘value’];
$url_put = sprintf(
‘https://%s.cybozu.com/k/v1/record/status.json’,
$kintone[‘sub_domain’]
);
$body_put = array(
‘app’ => $kintone[‘app’],
‘id’ => $recno,
‘action’ => ‘申請済み’,
);
$res_put = wp_remote_request(
$url_put,
array(
‘method’ => ‘PUT’,
‘headers’ => $headers,
‘body’ => json_encode( $body_put ),
)
);
if ( is_wp_error( $res_put ) ) {
return $res_put;
} elseif ( $res_put[‘response’][‘code’] !== 200 ) {
$message = json_decode( $res_put[‘body’], true );
$e = new WP_Error();
$e->add( ‘validation-error’, $message[‘message’], $message );
return $e;
} else {
return true;
}
/*###############################
* 追加処理 終了
*/###############################
}
}
/*
* Prase $_POST to Kintone’s record format.
* @param string $data $_POST data
* @return array Kintone’s formated array.
* @since 0.1
*/
public static function parse_record( $data )
{
$record = array();
foreach ( $data as $key => $value ) {
$key = preg_replace(“/^_kintone_control_/”, “”, $key);
$record[$key] = array( ‘value’ => $value );
}
return $record;
}
public static function send( $data )
{
$kintone = get_option( $data[‘_kintone_key’], false );
if ( ! $kintone ) {
return new WP_Error( __LINE__ );
}
$result = self::save_data( $kintone, $data );
if ( is_wp_error( $result ) ) {
return $result;
} else {
update_option( ‘kintone-success’, true );
return true;
}
}
/*
* Save shortcode atts and token as option
* @param string $data shortcode atts
* @return array options key
* @since 0.1
*/
public static function save_option_key( $atts ) {
$option_key = ‘kintone-’. sha1( $atts[‘sub_domain’] . $atts[‘app’] ); update_option( $option_key, $atts );
return $option_key;
}
}
// EOF
==========================================================================================-
RequestBinでecho出力していなければリクエストがスルーされていることに気づかず右往左往していたと思います。
非常に参考になりました!ありがとうございます。