6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WordPress で Kintone の API へアクセスする

Last updated at Posted at 2019-02-19

WordPress の関数で Kintone の API へアクセスするには以下のような感じ。

public function request( $url, $query )
{
    $args = array(
        'method' => 'POST',
        'blocking' => true,
        'headers' => array(
            'X-Cybozu-API-Token' => $this->token,
            'Authorization' => 'Basic ' . $this->auth,
            'Content-Type' => 'application/json',
            'X-HTTP-Method-Override' => 'GET',
        ),
        'body' => json_encode( $query )
    );

    $result =  wp_remote_post( $url, $args );

    if ( ! is_wp_error( $result ) ) {
        return json_decode( wp_remote_retrieve_body( $result ), true );
    } else {
        return $result;
    }
}

Kintone の API にアクセスする際に渡すパラメータは、クエリー文字列か以下のようなフォーマットの JSON を利用できますが、JSON のほうが余計なコードを書かなくてすみます。

{
    "app": 8,
    "query": "updated_time > \"2012-02-03T09:00:00+0900\" and updated_time < \"2012-02-03T10:00:00+0900\" order by $id asc limit 10 offset 20",
    "fields": ["$id", "created_time", "dropdown"]
}

でも、Kintone のレコードをとってくるには、リクエストメソッドが GET かつ body で JSON を放り投げるという仕様で、WordPress の wp_remote_get() とか wp_remote_request() ではそれができないんですよね。

なので、wp_remote_post() を使って、リクエストヘッダーで X-HTTP-Method-Override: GET を放り投げてあげる感じです。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?