Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WP-API攻略

 WP-API攻略

PHPカンファレンス2017での資料です。

A0412ba4d1cc922f1950a46d8804a5f9?s=128

Takayuki Miyauchi

October 08, 2017
Tweet

Transcript

  1. 81"1*పఈ߈ུ PHPΧϯϑΝϨϯε 2017 ٶ಺ ོߦ @miya0001

  2. None
  3. None
  4. None
  5. None
  6. None
  7. ࠓ೔ͷΞδΣϯμ

  8. w 81"1*ͱ͸ʁ w άϩʔόϧύϥϝʔλ w ϖʔδωʔγϣϯ w ϑΟϧλʔ w ΧελϜ౤ߘλΠϓΛಠࣗΤϯυϙΠϯτͱͯ͠௥Ճ

    w ΧελϜϑΟʔϧυͷ௥Ճ w ೝূ w Ϣχοτςετ w ΫϥΠΞϯτϥΠϒϥϦʢ+BWB4DSJQUʣ
  9. 81"1*ͱ͸ʁ

  10. w 3&45GVM"1* w 8PSE1SFTTͷ(6*ͱͷ༥߹ʹΑΔͳΜͰ΋"1*؀ڥ w 8PSE1SFTTϓϥάΠϯʹΑΔ֦ுੑ w ΦʔτσΟεΧόϦ

  11. ΤϯυϙΠϯτ /wp-json/wp/v2 /wp-json/wp/v2/posts /wp-json/wp/v2/categories /wp-json/wp/v2/revisions /wp-json/wp/v2/tags /wp-json/wp/v2/pages /wp-json/wp/v2/comments /wp-json/wp/v2/taxonomies /wp-json/wp/v2/media

    /wp-json/wp/v2/users /wp-json/wp/v2/types /wp-json/wp/v2/statuses /wp-json/wp/v2/settings
  12. /wp-json/wp/v2 w ͜ͷΤϯυϙΠϯτͰ͢΂͕ͯΘ͔Γ·͢ɻ w αϙʔτͯ͠ΔΤϯυϙΠϯτ w ͦΕͧΕͷΤϯυϙΠϯτ͕ͲͷϝιουΛαϙʔτ͠ ͍ͯΔ͔ʁ w ͲΜͳύϥϝʔλΛαϙʔτ͍ͯ͠Δ͔ʁ

    υΩϡϝϯτͷҰ෦΋ ͜ͷΤϯυϙΠϯτ͔Βࣗಈੜ੒͍ͯ͠·͢ɻ
  13. ΦʔτσΟεΧόϦ $ http HEAD http://wp-api.test/wp-json/ HTTP/1.1 200 OK Access-Control-Allow-Headers: Authorization,

    Content-Type Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages Allow: GET Connection: close Content-Type: application/json; charset=UTF-8 Date: Sat, 07 Oct 2017 23:16:58 GMT Link: <http://wp-api.test/wp-json/>; rel="https://api.w.org/" Server: Apache/2.4.18 (Ubuntu) X-Content-Type-Options: nosniff X-Robots-Tag: noindex 8PSE1SFTTʹؔ࿈͢ΔͳΜΒ͔ͷαʔϏεΛ։ൃ͢Δ৔߹͸ɺ ϨεϙϯεϔομʔͷA-JOLAΛνΣοΫʂ
  14. άϩʔόϧύϥϝʔλ

  15. _jsonp /wp/v2/posts?_jsonp=hello ΫϩευϝΠϯͰίϯςϯπΛ࠶ར༻͢Δ͜ͱ͕Մೳʹɻ

  16. _method /wp-json/wp/v2/posts/42?_method=DELETE ͋ΔಛఆͷϝιουʹରԠ͍ͯ͠ͳ͍ΫϥΠΞϯτͱͷޓ׵ੑͷͨΊʹ ϦΫΤετϝιουΛ্ॻ͖͢Δ͜ͱ͕Մೳɻ ͨͩ͠ɺPOSTͰϦΫΤετ͢Δ͜ͱɺೝূ͕ඞཁͳͲͷ੍໿͕͋Γ·͢ɻ POST /wp-json/wp/v2/posts/42 HTTP/1.1 Host: example.com

    X-HTTP-Method-Override: DELETE X-HTTP-Method-Override ϔομʔͰ΋ಉ͜͡ͱ͕Մೳ
  17. _envelope /wp-json/wp/v2/posts/42?_envelope HTTPϨεϙϯεϔομΛਖ਼͘͠ղऍͰ͖ͳ͍ΫϥΠΞϯτͷͨΊʹ Ϩϯεϙϯεϔομʔͷ಺༰ΛJSONʹؚΊͯฦ͢ɻ HTTPεςʔλείʔυ͕200ʹͳΔ୅ΘΓʹ JSONʹΤϥʔ͕֨ೲ͞Εͯฦ٫͞Ε·͢ɻ $ curl --dump-header -

    http://.../posts/1111?_envelope HTTP/1.1 200 OK
  18. _embed /wp-json/wp/v2/posts/42?_embed ͦͷΦϒδΣΫτʹؔ࿈͢ΔΦϒδΣΫτ΋औಘ͢ΔͨΊͷύϥϝʔλɻ ͨͱ͑͹౤ߘͰ͋Ε͹ΞΠΩϟονը૾ɻ ݻఆϖʔδͰ͋Ε͹਌ϖʔδ΁ͷϦϯΫͳͲɻ

  19. ϖʔδωʔγϣϯ

  20. ύϥϝʔλ ༻్ ?page= ϖʔδͷ൪߸Λࢦఆ ?per_page= 1ϖʔδ͋ͨΓͷΦϒδΣΫτ਺Λࢦఆɻ
 ࠷େ஋͸100ɻ ?offset= ΦϒδΣΫτͷΦϑηοτΛࢦఆɻ +40/಺ͷA981505"-AٴͼA9815PUBM1BHFTAʹ

    ΦϒδΣΫτͷ૯਺ͱ૯ϖʔδ਺ؚ͕·Ε͍ͯ·͢ɻ APSEFSAٴͼAPSEFSCZAͰιʔτ΋Մೳɻ
  21. ϑΟϧλʔ

  22. ʁpMUFS<DBUFHPSZ@OBNF>VODBUFHPSJ[FE 81"1*ͷϕʔλςετͰఏڙ͞Ε͍ͯͨpMUFSύϥϝʔλ͸ɺ ๲େͳϢʔεέʔεʹԠ͑Δ͜ͱ͕ࠔ೉ͳ͜ͱɻ σϑΥϧτͰ༗ޮԽ͢Δʹ͸ηΩϡϦςΟ্ͷݕ౼ࣄ߲͕ଟ͍͜ͱɻ ͳͲΛཧ༝ʹίΞʹϚʔδ͢Δ࣌఺Ͱ࡟আ͞Ε·ͨ͠ɻ https://github.com/WP-API/rest-filter ݱࡏ͸ϓϥάΠϯͱͯ͠ఏڙ͞Ε͍ͯ·͢ɻ
 ͨͩ͠׬શͰ͸ͳ͍

  23. ಠࣗΤϯυϙΠϯτ

  24. wp scaffold post-type <slug> REST APIʹରԠͨ͠ΧελϜ౤ߘλΠϓΛ ίϚϯυҰൃͰੜ੒Ͱ͖·͢ɻ

  25. register_rest_route( 'rad/v1', 'site-info', array( array( 'methods' => 'GET', 'callback' =>

    function( $request ) { return array( 'phone_number' => get_option( 'phone_number' ), ); }, ), array( 'methods' => 'POST', 'callback' => function( $request ) { update_option( 'phone_number', $request['phone_number'] ); return array( 'phone_number' => get_option( 'phone_number' ), ); }, ), ) ); `register_rest_route()` Λ࢖ͬͯΦϦδφϧAPIΛ࡞Δྫ
  26. array( 'methods' => 'POST', 'callback' => function( $request ) {

    update_option( 'phone_number', $request['phone_number'] ); return array( 'phone_number' => get_option( 'phone_number' ), ); }, 'permission_callback' => function() { if ( current_user_can( 'manage_options' ) ) { return true; } return new WP_Error( 'rad_unauthorized', 'You do not have permission to update this resource.', array( 'status' => is_user_logged_in() ? 403 : 401 ) ); }, ), ೝূΛ͚ͭΔ৔߹͸ `permission_callback`
  27. ΧελϜϑΟʔϧυ

  28. add_action( 'rest_api_init', function() { register_rest_field( 'poi', 'poi', array( 'get_callback' =>

    function( $object ) { $meta = get_post_meta( $object['id'], '_addr', true ); return array( 'address' => $meta ); }, 'schema' => null, ) ); } ); ΧελϜϑΟʔϧυͷ஋͸σϑΥϧτͰ͸APIʹؚ·Ε·ͤΜɻ ʢηΩϡϦςΟ্ॏཁͳ৘ใؚ͕·ΕΔ͜ͱ͕૝ఆ͞ΕΔͨΊʣ ্ͷྫ͸ɺ`_addr` ͱ͍͏ΧελϜϑΟʔϧυͷ஋Λ `address` ͱ͍͏εΩʔϚͰAPIʹؚΊΔྫɻ
  29. ೝূ

  30. https://wordpress.org/plugins/rest-api-oauth1/

  31. Ϣχοτςετ

  32. ࣄނ๷ࢭͷͨΊʹ"1*ΛΧελϚΠζ͢Δͱ͖͸ ςετΛॻ͖·͠ΐ͏ʂ

  33. https://github.com/danielbachhuber/pantheon-rest-api-demo

  34. ࢀߟ

  35. https://developer.wordpress.org/rest-api/

  36. ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ