WordPress

WordPress4.4コアのWP REST APIを叩いてみる(ルート編)

ドキュメント読むより実際にAPI叩いた方がイメージつくんじゃないかと思ったので、とりあえず叩いてみました。 使えるエンドポイント [bash] % curl -L https://wp-kyoto.cdn.rabify. […]

広告ここから
広告ここまで

ドキュメント読むより実際にAPI叩いた方がイメージつくんじゃないかと思ったので、とりあえず叩いてみました。

使えるエンドポイント

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".routes"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1138 0 1138 0 0 9843 0 –:–:– –:–:– –:–:– 9895
{
"/": {
"namespace": "",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "https://wp-kyoto.cdn.rabify.me/wp-json/"
}
},
"/oembed/1.0": {
"namespace": "oembed/1.0",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "oembed/1.0"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "https://wp-kyoto.cdn.rabify.me/wp-json/oembed/1.0"
}
},
"/oembed/1.0/embed": {
"namespace": "oembed/1.0",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"url": {
"required": true
},
"format": {
"required": false,
"default": "json"
},
"maxwidth": {
"required": false,
"default": 600
}
}
}
],
"_links": {
"self": "https://wp-kyoto.cdn.rabify.me/wp-json/oembed/1.0/embed"
}
}
}
[/bash]
わかりにくいですね。jqで小分けしていきましょう。

キーを見る

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq "keys"
[
"_links",
"authentication",
"description",
"name",
"namespaces",
"routes",
"url"
]
[/bash]

authentication

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".authentication"
[]
[/bash]
認証なにもやってないからでしょうね。

description

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".description"
"WordPress公式テーマの紹介とカスタマイズ、その他Web制作に関するTipsを紹介します。"
[/bash]
サイトの説明文が出てきます。

name

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".name"
"WP-kyoto"
[/bash]
ここはサイトタイトルですね。

namespaces

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".namespaces"
[
"oembed/1.0"
]
[/bash]
WP REST APIで使用する名前空間が定義されている様子です。

routes

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".routes"
{
"/": {
"namespace": "",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "https://wp-kyoto.cdn.rabify.me/wp-json/"
}
},
"/oembed/1.0": {
"namespace": "oembed/1.0",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "oembed/1.0"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "https://wp-kyoto.cdn.rabify.me/wp-json/oembed/1.0"
}
},
"/oembed/1.0/embed": {
"namespace": "oembed/1.0",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"url": {
"required": true
},
"format": {
"required": false,
"default": "json"
},
"maxwidth": {
"required": false,
"default": 600
}
}
}
],
"_links": {
"self": "https://wp-kyoto.cdn.rabify.me/wp-json/oembed/1.0/embed"
}
}
}
[/bash]
使用できるエンドポイントやリクエストタイプなどが入っている様子です。
postやpage / attachementなどは取れないようになってますね。

url

[bash]
% curl -L https://wp-kyoto.cdn.rabify.me/wp-json/ | jq ".url"
"https://wp-kyoto.cdn.rabify.me"
[/bash]
サイトURLですね。

curl -L + jqでいろいろ調べてみよう

こんな感じで、curl -LでREST APIを叩きつつ、jqで整形してみるといろいろ発見があってなかなか面白いです。

そこ、ドキュメント読めとか言わない。

oembedのAPIが直接叩けそうな気配なので、今度はそこを叩いてみようかと思います。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts