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

WP-API攻略

 WP-API攻略

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

Takayuki Miyauchi

October 08, 2017
Tweet

More Decks by Takayuki Miyauchi

Other Decks in Technology

Transcript

  1. 81"1*పఈ߈ུ
    PHPΧϯϑΝϨϯε 2017

    ٶ಺ ོߦ

    @miya0001

    View full-size slide

  2. ࠓ೔ͷΞδΣϯμ

    View full-size slide

  3. w 81"1*ͱ͸ʁ
    w άϩʔόϧύϥϝʔλ
    w ϖʔδωʔγϣϯ
    w ϑΟϧλʔ
    w ΧελϜ౤ߘλΠϓΛಠࣗΤϯυϙΠϯτͱͯ͠௥Ճ
    w ΧελϜϑΟʔϧυͷ௥Ճ
    w ೝূ
    w Ϣχοτςετ
    w ΫϥΠΞϯτϥΠϒϥϦʢ+BWB4DSJQUʣ

    View full-size slide

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

    View full-size slide

  5. ΤϯυϙΠϯτ
    /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

    View full-size slide

  6. /wp-json/wp/v2
    w ͜ͷΤϯυϙΠϯτͰ͢΂͕ͯΘ͔Γ·͢ɻ
    w αϙʔτͯ͠ΔΤϯυϙΠϯτ
    w ͦΕͧΕͷΤϯυϙΠϯτ͕ͲͷϝιουΛαϙʔτ͠
    ͍ͯΔ͔ʁ
    w ͲΜͳύϥϝʔλΛαϙʔτ͍ͯ͠Δ͔ʁ
    υΩϡϝϯτͷҰ෦΋
    ͜ͷΤϯυϙΠϯτ͔Βࣗಈੜ੒͍ͯ͠·͢ɻ

    View full-size slide

  7. ΦʔτσΟεΧόϦ
    $ 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: ; rel="https://api.w.org/"
    Server: Apache/2.4.18 (Ubuntu)
    X-Content-Type-Options: nosniff
    X-Robots-Tag: noindex
    8PSE1SFTTʹؔ࿈͢ΔͳΜΒ͔ͷαʔϏεΛ։ൃ͢Δ৔߹͸ɺ
    ϨεϙϯεϔομʔͷA-JOLAΛνΣοΫʂ

    View full-size slide

  8. άϩʔόϧύϥϝʔλ

    View full-size slide

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

    View full-size slide

  10. _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 ϔομʔͰ΋ಉ͜͡ͱ͕Մೳ

    View full-size slide

  11. _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

    View full-size slide

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

    View full-size slide

  13. ϖʔδωʔγϣϯ

    View full-size slide

  14. ύϥϝʔλ ༻్
    ?page= ϖʔδͷ൪߸Λࢦఆ
    ?per_page=
    1ϖʔδ͋ͨΓͷΦϒδΣΫτ਺Λࢦఆɻ

    ࠷େ஋͸100ɻ
    ?offset= ΦϒδΣΫτͷΦϑηοτΛࢦఆɻ
    +40/಺ͷA981505"-AٴͼA9815PUBM1BHFTAʹ
    ΦϒδΣΫτͷ૯਺ͱ૯ϖʔδ਺ؚ͕·Ε͍ͯ·͢ɻ
    APSEFSAٴͼAPSEFSCZAͰιʔτ΋Մೳɻ

    View full-size slide

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

    ͨͩ͠׬શͰ͸ͳ͍

    View full-size slide

  16. ಠࣗΤϯυϙΠϯτ

    View full-size slide

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

    View full-size slide

  18. 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Λ࡞Δྫ

    View full-size slide

  19. 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`

    View full-size slide

  20. ΧελϜϑΟʔϧυ

    View full-size slide

  21. 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ʹؚΊΔྫɻ

    View full-size slide

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

    View full-size slide

  23. Ϣχοτςετ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide