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

概念から学ぶ正しくRESTfulなAPIとWP REST APIでの実例 #wb_rest

概念から学ぶ正しくRESTfulなAPIとWP REST APIでの実例 #wb_rest

2017.09.03 #wb_rest HANATANI Takuma #wbosaka #wbkyoto #wbnagano
9月のWordBenchの登壇スライドです。

potato4d(Takuma HANATANI)

September 03, 2017
Tweet

More Decks by potato4d(Takuma HANATANI)

Other Decks in Programming

Transcript

  1. RESTͷجຊݪଇʹ͍ͭͯ HTTPϝιουϕʔεͰͷΞΫηε • RESTͷϦιʔε΁ͷૢ࡞͸ϙϐϡϥʔ͔ͭ࠷খݶͷ໋ྩηοτʹ Αͬͯදݱ͞ΕΔ • ΑΓ࣮૷ʹ͍ۙݺশͰHTTPͷ GET , POST

    , PUT , DELETE ͷ4छΛ ར༻͢Δ • ͜Ε͸ඞͣ͠΋CRUDͱରԠ͢Δ΋ͷͰ͸ͳ͍ • ͱ͸͍͑WordPressͷΑ͏ͳൺֱతϦϨʔγϣϯ͕؆ܿͳγεςϜʹ ͓͍ͯ͸CRUDͱͷϚοϐϯάͱͳΓ͕ͪ
  2. REST͡Όͳ͍ϧʔϧ Bearerೝূ΋ඞਢͰ͸ͳ͍ • OAuth / BearerͰͷೝূʹΑͬͯRESTful APIͷೝূ͸ߦΘΕ ͕ͪͰ͸͋Δ͕ɺBearerࣗମ͸ඞਢͰ͸ͳ͍ • ͦ΋ͦ΋

    Authorization ϔομʔΛ෇༩͍ͯ͠Δ͜ͱ͸εςʔ τϨεͷ৚͔݅Βҳ୤͍ͯ͠ΔͷͰ͸ʁ • ͜Ε΋ϓϥάϚςΟοΫͳ࿩ͱͯ͠͸ਖ਼͍͠
  3. WP REST APIʹ͓͚ΔRESTͱͷ෇͖߹͍ํ WP REST API͸ՄೳͳݶΓRESTͷ֓೦Λ९क͍ͯ͠Δ • WP REST API͸RESTfulͰ͋Δ͜ͱΛࢸ্ͱͯ͠࡞੒͞Ε͍ͯ

    Δ • WP REST API͸ɺRESTͰ͸ରॲ͖͠Εͳ͍໰୊ʹ͍ͭͯɺՄ ೳͳݶΓRESTΛ९क͢ΔΑ͏ਐΊ͍ͯΔ • ͱ͸͍࣮͑ࡍ໰୊ͱͯ͠ෆՄೳͳͱ͜Ζʹଥڠ͸͋Δ
  4. WP REST APIʹ͓͚ΔRESTͱͷ෇͖߹͍ํ جຊతͳར༻ʹ͍ͭͯ • WP REST API͸ɺWordPress͕΋ͭ๲େͳϦιʔεʹ͍ͭͯ جຊతͳૢ࡞ΛRESTʹԊͬͨܗͰఏڙ͍ͯ͠Δ •

    ྫ͑͹ɺ [GET] /posts ʹͯ ౤ߘͷҰཡͷऔಘΛɺ [GET] / posts/<id> ʹͯ୯Ұͷ౤ߘͷऔಘΛɺ [POST] /posts ʹͯ ౤ߘͷ࡞੒Λߦ͏͜ͱ͕Մೳɻ • ͜ΕΒ͕ΧςΰϦɾϢʔβʔɾݻఆϖʔδͳͲͦΕͧΕʹର ͯ͠ఏڙ͞Ε͍ͯΔ
  5. WP REST APIʹ͓͚ΔRESTͱͷ෇͖߹͍ํ Searchʹ͍ͭͯ • WordPressͰ͸RESTʹ͙ͦΘͳ͍ /search ܥͷΤϯυϙΠ ϯτͰ͸ͳ͘ɺ /posts

    ΁ͷ֦ுͱ͍͏ܗͰߦ͍ͬͯΔ • /posts?search= ͱ͍͏ܗࣜʹͯݕࡧ͕Մೳͱͳ͍ͬͯΔ • ͜Εࣗମ͸Ϧιʔεͱਖ਼͘͠Ϛον͓ͯ͠Γɺ͔ͭGETʹͯ ϦιʔεͷมԽ͕ߦΘΕͳ͍ͷͰਖ਼͍͠ͱ͍͑Δ
  6. WP REST APIʹ͓͚ΔRESTͱͷ෇͖߹͍ํ ೝূʹ͍ͭͯ • WP REST APIͰ͸ɺೝূΛ OAuth, Basic,

    ͦͯ͠CookieͰߦͬ ͍ͯΔ • ໪࿦Cookieʹ͍ͭͯ͸εςʔτϨεͱ͸ݴ͍೉্͍ɺଞͷೝ ূ΋௨৴ʹίϯςΩετΛඞཁͱ͢Δ • WordPressͱ͍͏Ϧιʔεͷ࡞੒ʹ੍ݶΛ՝͢ͱ͍͏Ϟνϕʔ γϣϯʹ͓͍ͯؒҧ͍Ͱ͸ͳ͍ʢؾ͕͢Δʣ • ݱ୅ʹ͓͍ͯ͸͜͜ʹର͢Δਖ਼͍͠ιϦϡʔγϣϯ͸ͳ͍
  7. ·ͱΊ ᶃ RESTͷجຊ • REST͸͍·͔Β15೥Ҏ্લʹఏএ͞ΕͨWeb։ൃͷઃܭελΠϧ Ͱ͋Δ • RESTͷେ͖ͳಛ௃ͱͯ͠ɺҎԼͷ༷ͳ΋ͷ͕͋Δ • GET,

    POST, PUT, DELETEΛར༻ͨ͠Ϧιʔεૢ࡞͕ఏڙ͞Ε͍ͯΔ • εςʔτϨεͰ͋Δ͜ͱʹΑΔѻ͍΍͍͢I/F͕ఏڙ͞Ε͍ͯΔ • ϦιʔεओٛͰ͋Δ͜ͱʹΑΓURIʹͯશ͕ͯ໌ࣔ͞Ε͍ͯΔ
  8. ࣗݾ঺հ Ֆ୩୓ຏ a.k.a @potato4d • དྷ೥੒ਓ͠·͢ • େࡕੜ·Εେࡕҭͪौ୩ࡏॅ • ීஈ͸ϑϩϯτΤϯυΤϯδχΞ

    • ͱ͖Ͳ͖PHPͱ͔ॻ͍ͯ·͢ • PHPΧϯϑΝϨϯε2017Ͱొஃ͢Δͷ ͰԠԉ͍ͯͩ͘͠͞