ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: rest-api

rest-api

๊ณต๊ฐœ์ ์œผ๋กœ WordPress REST API v2 ์—”๋“œ ํฌ์ธํŠธ ์ˆจ๊ธฐ๊ธฐ ๋“ฑ๋ก ๋œ ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ๋žŒ๊ณผ

๋‚ด๊ฐ€ ์‚ฌ์šฉ์„ ์‹œ์ž‘ํ•˜๊ณ ์žํ•˜๋Š” ์›Œ๋“œ ํ”„๋ ˆ์Šค์˜ REST API v2๊ฐ€ ๋‚ด ์‚ฌ์ดํŠธ์—์„œ ์ฟผ๋ฆฌ ์ •๋ณด. ์—”๋“œ ํฌ์ธํŠธ URL์„ ์ง์ ‘ ๋ฐฉ๋ฌธํ•˜๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต๊ฐœ์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŽ์€ ์ž์Šต์„œ์—์„œ ๋ผ์ด๋ธŒ ์‚ฌ์ดํŠธ๊ฐ€ ์•„๋‹Œ ํ…Œ์ŠคํŠธ ๋˜๋Š” ๋กœ์ปฌ ์„œ๋ฒ„ ์‚ฌ์šฉ์— ๋Œ€ํ•ด ์–ธ๊ธ‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚ด ์งˆ๋ฌธ์€ :

  • ์ด๊ฒƒ์€ ์ƒ์‚ฐ ํ˜„์žฅ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๊นŒ?
  • /wp-json/wp/v2/users/์‚ฌ์ดํŠธ์— ๋“ฑ๋ก ๋œ ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ๋žŒ๊ณผ ๊ฐ™์ด ์—”๋“œ ํฌ์ธํŠธ๋ฅผ ๋ณผ ์ˆ˜์žˆ๊ฒŒํ•˜๋Š” ๋ณด์•ˆ ์œ„ํ—˜์ด
    ์žˆ์Šต๋‹ˆ๊นŒ?
  • ์ธ์ฆ ๋œ ์‚ฌ์šฉ์ž ๋งŒ ์—”๋“œ ํฌ์ธํŠธ์— ์•ก์„ธ์Šคํ•˜๋„๋ก ํ—ˆ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

๋ณด์•ˆ๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋ฒ” ์‚ฌ๋ก€๋ฅผ ๋”ฐ๋ฅด๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋“  ํŒ์ด ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. API ๋ฌธ์„œ๋Š” ์ธ์ฆ์„ ์–ธ๊ธ‰,ํ•˜์ง€๋งŒ ๋‚œ ์ง์ ‘ ์•ก์„ธ์Šค๋˜๋Š” URL์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๊ฒ ์–ด์š”. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋„ˆ๋ฌด ๋งŽ์€ ์ •๋ณด๋ฅผ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ  ์™ธ๋ถ€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ์ด ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šคํ•˜๋„๋ก ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•ฉ๋‹ˆ๊นŒ?



๋‹ต๋ณ€

์ด๊ฒƒ์€ ์ƒ์‚ฐ ํ˜„์žฅ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๊นŒ?

์˜ˆ. ๋งŽ์€ ์‚ฌ์ดํŠธ์—์„œ ์ด๋ฏธ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์‚ฌ์ดํŠธ์— ๋“ฑ๋ก ๋œ ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ํ‘œ์‹œํ•˜๋Š” / wp-json / wp / v2 / users /์™€ ๊ฐ™์€ ์‚ฌ๋žŒ์ด ์—”๋“œ ํฌ์ธํŠธ๋ฅผ ๋ณผ ์ˆ˜์žˆ๊ฒŒํ•˜๋Š” ๋ณด์•ˆ ์œ„ํ—˜์ด ์žˆ์Šต๋‹ˆ๊นŒ?

์•„๋‹ˆ์š”. ์„œ๋ฒ„์˜ ์‘๋‹ต์€ ๋ณด์•ˆ๊ณผ ๊ด€๋ จ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋นˆ ํ™”๋ฉด / ์ฝ๊ธฐ ์ „์šฉ ์•ก์„ธ์Šค๋กœ ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ? ์•„๋ฌด๊ฒƒ๋„!

๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์ดํŠธ์—์„œ ์ทจ์•ฝํ•œ ์•”ํ˜ธ๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ๊ฒƒ์€ ๊ท€ํ•˜์˜ ์‚ฌ์ดํŠธ ์ •์ฑ…์ด๋ฏ€๋กœ REST API๋Š” ๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค.

์ธ์ฆ ๋œ ์‚ฌ์šฉ์ž ๋งŒ ์—”๋“œ ํฌ์ธํŠธ์— ์•ก์„ธ์Šคํ•˜๋„๋ก ํ—ˆ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

์˜ˆ. ๊ถŒํ•œ ์ฝœ๋ฐฑ ์„ ์‚ฌ์šฉํ•˜์—ฌ์ด๋ฅผ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
    return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you cannot view this resource with edit context.' ), array( 'status' => rest_authorization_required_code() ) );
}

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋„ˆ๋ฌด ๋งŽ์€ ์ •๋ณด๋ฅผ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ  ์™ธ๋ถ€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ์ด ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šคํ•˜๋„๋ก ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•ฉ๋‹ˆ๊นŒ?

์ •๋ณด ๊ฐ€ ๋ฌด์—‡์ธ์ง€ / ์–ธ์ œ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—์ด ์งˆ๋ฌธ์— ๋Œ€๋‹ตํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค . ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ๋ชจ๋‘ ์ฐธ์กฐ ์™€ ์น˜ํŠธ ์‹œํŠธ๋ฅผ ์‚ฌ์šฉ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

์ธ์ฆ ๋œ ์‚ฌ์šฉ์ž ๋งŒ ์—”๋“œ ํฌ์ธํŠธ์— ์•ก์„ธ์Šคํ•˜๋„๋ก ํ—ˆ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

์ปจํ…์ธ ๋ฅผ ๋ณด๋ ค๋ฉด ์ธ์ฆ์ด ํ•„์š”ํ•œ API ์—”๋“œ ํฌ์ธํŠธ์— ์‚ฌ์šฉ์ž ์ •์˜ ๊ถŒํ•œ ์ฝœ๋ฐฑ์„ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ถŒํ•œ์ด์—†๋Š” ์‚ฌ์šฉ์ž๋Š” ์˜ค๋ฅ˜ ์‘๋‹ต์„๋ฐ›์Šต๋‹ˆ๋‹ค"code": "rest_forbidden"

์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ WP_REST_Posts_Controller๋ฅผ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๊ทธ ๊ฐ„๋‹จํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค.

class My_Private_Posts_Controller extends WP_REST_Posts_Controller {

   /**
   * The namespace.
   *
   * @var string
   */
   protected $namespace;

   /**
   * The post type for the current object.
   *
   * @var string
   */
   protected $post_type;

   /**
   * Rest base for the current object.
   *
   * @var string
   */
   protected $rest_base;

  /**
   * Register the routes for the objects of the controller.
   * Nearly the same as WP_REST_Posts_Controller::register_routes(), but with a
   * custom permission callback.
   */
  public function register_routes() {
    register_rest_route( $this->namespace, '/' . $this->rest_base, array(
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => array( $this, 'get_items' ),
            'permission_callback' => array( $this, 'get_items_permissions_check' ),
            'args'                => $this->get_collection_params(),
            'show_in_index'       => true,
        ),
        array(
            'methods'             => WP_REST_Server::CREATABLE,
            'callback'            => array( $this, 'create_item' ),
            'permission_callback' => array( $this, 'create_item_permissions_check' ),
            'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
            'show_in_index'       => true,
        ),
        'schema' => array( $this, 'get_public_item_schema' ),
    ) );

    register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => array( $this, 'get_item' ),
            'permission_callback' => array( $this, 'get_item_permissions_check' ),
            'args'                => array(
                'context' => $this->get_context_param( array( 'default' => 'view' ) ),
            ),
            'show_in_index'       => true,
        ),
        array(
            'methods'             => WP_REST_Server::EDITABLE,
            'callback'            => array( $this, 'update_item' ),
            'permission_callback' => array( $this, 'update_item_permissions_check' ),
            'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
            'show_in_index'       => true,
        ),
        array(
            'methods'             => WP_REST_Server::DELETABLE,
            'callback'            => array( $this, 'delete_item' ),
            'permission_callback' => array( $this, 'delete_item_permissions_check' ),
            'args'                => array(
                'force' => array(
                    'default'     => true,
                    'description' => __( 'Whether to bypass trash and force deletion.' ),
                ),
            ),
            'show_in_index'       => false,
        ),
        'schema' => array( $this, 'get_public_item_schema' ),
    ) );
  }

  /**
   * Check if a given request has access to get items
   *
   * @param WP_REST_Request $request Full data about the request.
   * @return WP_Error|bool
   */
  public function get_items_permissions_check( $request ) {
    return current_user_can( 'edit_posts' );
  }

}

์ฝœ๋ฐฑ์ด ์•ก์„ธ์Šค ํ—ˆ์šฉ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ function get_items_permissions_check์‚ฌ์šฉ ํ•˜๋Š” ๊ถŒํ•œ current_user_can์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ REST API ์ง€์›์œผ๋กœ ์‚ฌ์šฉ์ž ์ •์˜ ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์„ ๋“ฑ๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. register_post_type

  /**
   * Register a book post type, with REST API support
   *
   * Based on example at: http://codex.wordpress.org/Function_Reference/register_post_type
   */
  add_action( 'init', 'my_book_cpt' );
  function my_book_cpt() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest'       => true,
        'rest_base'          => 'books-api',
        'rest_controller_class' => 'My_Private_Posts_Controller',
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'book', $args );
}

๋‹น์‹ ์€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค rest_controller_class์‚ฌ์šฉํ•˜๋Š” My_Private_Posts_Controller๋Œ€์‹  ๊ธฐ๋ณธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ.

์„ค๋ช…์„œ ์™ธ๋ถ€์—์„œ REST API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋Œ€ํ•œ ์ข‹์€ ์˜ˆ์™€ ์„ค๋ช…์„ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค . ๊ธฐ๋ณธ ์ปจํŠธ๋กค๋Ÿฌ ํ™•์žฅ์— ๋Œ€ํ•œ ํ›Œ๋ฅญํ•œ ์„ค๋ช…์„ ์ฐพ์•˜ ์œผ๋ฉฐ ์—ฌ๊ธฐ ์— ์—”๋“œ ํฌ์ธํŠธ ์ถ”๊ฐ€์— ๋Œ€ํ•œ ์ฒ ์ €ํ•œ ์•ˆ๋‚ด์„œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์€ ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ REST API๋ฅผ ์ „ํ˜€ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ฐจ๋‹จํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

add_filter( 'rest_api_init', 'rest_only_for_authorized_users', 99 );
function rest_only_for_authorized_users($wp_rest_server){
    if ( !is_user_logged_in() ) {
        wp_die('sorry you are not allowed to access this data','cheatin eh?',403);
    }
}

๋‹ต๋ณ€

add_filter( 'rest_api_init', 'rest_only_for_authorized_users', 99 );
function rest_only_for_authorized_users($wp_rest_server)
{
if( !is_user_logged_in() )

    wp_die('sorry you are not allowed to access this data','Require Authentication',403);
} }
function json_authenticate_handler( $user ) {

global $wp_json_basic_auth_error;

$wp_json_basic_auth_error = null;

// Don't authenticate twice
if ( ! empty( $user ) ) {
    return $user;
}

if ( !isset( $_SERVER['PHP_AUTH_USER'] ) ) {
    return $user;
}

$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];


remove_filter( 'determine_current_user', 'json_authenticate_handler', 20 );

$user = wp_authenticate( $username, $password );

add_filter( 'determine_current_user', 'json_authenticate_handler', 20 );

if ( is_wp_error( $user ) ) {
    $wp_json_basic_auth_error = $user;
    return null;
}

$wp_json_basic_auth_error = true;

return $user->ID;}add_filter( 'determine_current_user', 'json_authenticate_handler', 20 );

๋‹ต๋ณ€


์ด ๊ธ€์€ wordpress ์นดํ…Œ๊ณ ๋ฆฌ์— ๋ถ„๋ฅ˜๋˜์—ˆ๊ณ  , , ํƒœ๊ทธ๊ฐ€ ์žˆ์œผ๋ฉฐ ๋‹˜์— ์˜ํ•ด ์— ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.