LoginSignup
0
0

More than 3 years have passed since last update.

PHP出来るけど全然WordPressわからないので使ってみる(REST API編)

Posted at

前回環境準備が出来たので、WordPress REST APIを使ってみたいと思います。
ドキュメントは以下のURL↓
https://ja.wp-api.org/

投稿一覧取得

とりあえず投稿一覧を取得してみます。
https://ja.wp-api.org/reference/posts/
を見るとエンドポイントは
/wp-json/wp/v2/posts とのこと。
いろいろパラメーターありますが、これらは一旦無視。
GETメソッドでいけるので、ブラウザで
http://localhost:8000/wp-json/wp/v2/posts
にアクセス。。。
あれapacheのエラー画面が出るぞ。。。

パーマリンク設定

調べるとパーマリンク設定を変更しないとREST APIが使えないらしい。
これは罠ですね。
以下のように管理画面からパーマリンクを[投稿名]に変更しましょう。
wordpress.jpg

アクセス

http://localhost:8000/wp-json/wp/v2/posts
するとJSONが返ってきます。

[ 
   { 
      "id":1,
      "date":"2020-02-06T16:30:09",
      "date_gmt":"2020-02-06T07:30:09",
      "guid":{ 
         "rendered":"http://localhost:8000/?p=1"
      },
      "modified":"2020-02-06T16:30:09",
      "modified_gmt":"2020-02-06T07:30:09",
      "slug":"hello-world",
      "status":"publish",
      "type":"post",
      "link":"http://localhost:8000/hello-world/",
      "title":{ 
         "rendered":"Hello world!"
      },
      "content":{ 
         "rendered":"\n<p>WordPress へようこそ。こちらは最初の投稿です。編集または削除し、コンテンツ作成を始めてください。</p>\n",
         "protected":false
      },
      "excerpt":{ 
         "rendered":"<p>WordPress へようこそ。こちらは最初の投稿です。編集または削除し、コンテンツ作成を始めてください。</p>\n",
         "protected":false
      },
      "author":1,
      "featured_media":0,
      "comment_status":"open",
      "ping_status":"open",
      "sticky":false,
      "template":"",
      "format":"standard",
      "meta":[ 

      ],
      "categories":[ 
         1
      ],
      "tags":[ 

      ],
      "_links":{ 
         "self":[ 
            { 
               "href":"http://localhost:8000/wp-json/wp/v2/posts/1"
            }
         ],
         "collection":[ 
            { 
               "href":"http://localhost:8000/wp-json/wp/v2/posts"
            }
         ],
         "about":[ 
            { 
               "href":"http://localhost:8000/wp-json/wp/v2/types/post"
            }
         ],
         "author":[ 
            { 
               "embeddable":true,
               "href":"http://localhost:8000/wp-json/wp/v2/users/1"
            }
         ],
         "replies":[ 
            { 
               "embeddable":true,
               "href":"http://localhost:8000/wp-json/wp/v2/comments?post=1"
            }
         ],
         "version-history":[ 
            { 
               "count":0,
               "href":"http://localhost:8000/wp-json/wp/v2/posts/1/revisions"
            }
         ],
         "wp:attachment":[ 
            { 
               "href":"http://localhost:8000/wp-json/wp/v2/media?parent=1"
            }
         ],
         "wp:term":[ 
            { 
               "taxonomy":"category",
               "embeddable":true,
               "href":"http://localhost:8000/wp-json/wp/v2/categories?post=1"
            },
            { 
               "taxonomy":"post_tag",
               "embeddable":true,
               "href":"http://localhost:8000/wp-json/wp/v2/tags?post=1"
            }
         ],
         "curies":[ 
            { 
               "name":"wp",
               "href":"https://api.w.org/{rel}",
               "templated":true
            }
         ]
      }
   }
]

罠はありましたが、知っていると割と簡単にここまでこれると思います。
今回はこのへんで。

0
0
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
0
0