Cosa sono le rest API? Come iniziare a utilizzarle velocemente? Come estenderne le funzionalità? Quali sono i vantaggi per design, blogger, developer? In questo talk ti fornirò tutte le risposte e ti mostrerò perché è fondamentale conoscerle
Speech tenuto al WordCamp Roma 2017
2. CHI SONO
• DEVELOPER & SEO @ T.C. INFORMATICA
• LAVORO CON WP DAL 2009
• CO-ORGANIZZATORE DEL WP ROMAGNA MEETUP
• I <3 TALK ABOUT WORDPRESS
• https://wordpress.tv/?s=andrea+cardinali
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
3. I <3 TALK ABOUT WORDPRESS
• INDICIZZAZIONE & CRAWL BUDGET OPTIMIZATION
(seoCMS ‘16)
• VAGRANT E SVILUPPO (WCMIL ‘16)
• WP-CLI (WCTRN ‘17)
• SEO, HTTPS e HTTP/2 (seoCMS 17)
• WEB PERFOMANCE E HTTP/2 (WMF ‘17)
• REALIZZARE SITI CHE CARICANO IN 1s (WCMIL ‘17)
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
4. DI COSA PARLERÒ
• COSA SONO LE REST API
• VANTAGGI
• COME FUNZIONANO
• PLUGINS
• DOMANDE
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
10. PLUGIN CHE UTILIZZANO LE
REST API
• https://wordpress.org/plugins/wp-front-end-editor/
• https://it.wordpress.org/plugins/woocommerce/
• https://it.wordpress.org/plugins/contact-form-7/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
11. LA STORIA IN BREVE
• NASCONO NEL 2012 da
RACHEL BAKER & RYAN MC CUE
• INCLUSE NEL CORE DALLA v.4.4
(8 Dicembre 2015)
• ATTIVE DI DEFAULT DALLA v.4.7
(6 Dicembre 2016)
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
14. LE REST API SONO
UN’INTERFACCIA DI
PROGRAMMAZIONE
APPLICATIVA RESTFUL HTTP
JSON […] EVOLUZIONE
DELLA PRECEDENTE API
BASATA SU PROTOCOLLO
XML-RPC
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
16. «Non hai veramente
capito qualcosa fino a
quando non sei in
grado di spiegarlo a tua
nonna»
Albert Einstein
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
31. REST È UN TIPO DI
ARCHITETTURA
SOFTWARE
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
32. REST DEFINISCE UN
INSIEME DI REGOLE
(PRINCIPI ARCHITETTURALI)
PER LA
PROGETTAZIONE DI
UN SISTEMA
https://it.wikipedia.org/wiki/Representational_State_Transfer
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
34. CARATTERISTICHE REST
• Identificazione delle risorse
• Utilizzo esplicito dei metodi HTTP
• Comunicazione senza stato
• Risorse autodescrittive
• Collegamenti tra risorse
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
35. CARATTERISTICHE REST
• Identificazione delle risorse
• Utilizzo esplicito dei metodi HTTP
• Comunicazione senza stato
• Risorse autodescrittive
• Collegamenti tra risorse
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
36. CARATTERISTICHE REST
• Identificazione delle risorse
• Utilizzo esplicito dei metodi HTTP
• Comunicazione senza stato
• Risorse autodescrittive
• Collegamenti tra risorse
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
42. CARATTERISTICHE REST
• Identificazione delle risorse
• Utilizzo esplicito dei metodi HTTP
• Risorse autodescrittive
• Collegamenti tra risorse
• Comunicazione senza stato
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
52. AZIONI CRUD E METODI HTTP
AZIONE METODO HTTP RISORSA
CREATE POST /posts/
READ GET /posts/123
UPDATE PUT /posts/123
DELETE DELETE /posts/123
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
53. CARATTERISTICHE REST
• Identificazione delle risorse
• Utilizzo esplicito dei metodi HTTP
• Comunicazione senza stato
• Risorse autodescrittive
• Collegamenti tra risorse
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
56. OGNI RICHIESTA DEL
CLIENT DEVE
CONTENERE TUTTE LE
INFO NECESSARIE AL
SERVER A
COMPRENDERLA […]
https://stackoverflow.com/a/34131829
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
71. LE REST API
FUNZIONANO
SOLO CON I COOKIES
DI AUTENTICAZIONE
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
73. HTTP authentication is
presumed to be stateless:
all of the information
necessary to authenticate a
request MUST be provided
in the request, rather than
be dependent on the server
remembering prior requests.
https://tools.ietf.org/html/rfc7235
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
74. PER SFRUTTARE LE
API IN SCRITTURA
(CRUD) È NECESSARIO
IMPLEMENTARE UN
MECCANISMO DI AUTH
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
80. ENDPOINT =
ROUTE + METODO HTTP
https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
81. 1 ROUTE 3 ENDPOINT
GET /wp-
json/wp/v2/posts/123
PUT/wp-
json/wp/v2/posts/123
DELETE /wp-
json/wp/v2/posts/123
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
82. AD OGNI ENDPOINT
È ASSOCIATA UNA
FUNZIONE DI
CALLBACK
https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
83. ROTTE PREDEFINITE
Resource Base Route
Posts /wp/v2/posts
Post Revisions /wp/v2/revisions
Categories /wp/v2/categories
Tags /wp/v2/tags
Pages /wp/v2/pages
Comments /wp/v2/comments
Taxonomies /wp/v2/taxonomies
Media /wp/v2/media
Users /wp/v2/users
Post Types /wp/v2/types
Post Statuses /wp/v2/statuses
Settings /wp/v2/settings
https://developer.wordpress.org/rest-api/reference/
84. REST API &
TIPI DI POST
PERSONALIZZATI
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
85. DISATTIVATE DI
DEFAULT PER I
CUSTOM POST TYPE
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
86. ATTIVARE LE REST API PER I
CPT
$args = array(
'public' => true,
'show_in_rest' => true,
'label' => 'Books'
);
register_post_type( 'book',
$args );
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
89. È POSSIBILE
MOSTRARE I CAMPI
PERSONALIZZATI
NELLA RISPOSTA
UTILIZZANDO
register_meta()
https://developer.wordpress.org/reference/functions/register_meta/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
90. È POSSIBILE
AGGIUNGERE
REST FIELDS
PERSONALIZZATI CON
register_rest_field()
https://developer.wordpress.org/reference/functions/register_rest_field/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
91. CMB2 SUPPORTA LE
REST API
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
92. CMB2 SUPPORTA LE REST API
function yourprefix_register_demo_metabox() {
$cmb = new_cmb2_box( array(
'id' =>
'cmb2_info_metabox',
'title' => 'Information',
'object_types' => array( 'post' ),
'show_in_rest' =>
WP_REST_Server::READABLE
) );
}
add_action( 'cmb2_init',
'yourprefix_register_demo_metabox' );
https://github.com/CMB2/CMB2/wiki/REST-API
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
93. ACF SUPPORTA LE
REST API
(TRAMITE PLUGIN)
https://it.wordpress.org/plugins/acf-to-rest-api/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
111. RICHIAMARE INTERNAMENTE
$request = new WP_REST_Request( 'GET',
'/wp/v2/posts' );
// Set one or more request query
parameters
$request->set_param( 'per_page', 20 );
$response = rest_do_request( $request );
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
112. RICAPITOLANDO
• Le REST API permettono a un qualsiasi client
che «parla» http e json di interagire con
WordPress
• Le REST API richiedono un meccanismo di
autenticazione
• È possibile estendere la risposta delle api
• È possibile creare nuovi endpoint
• Ogni volta che provi disattivare le REST API
muoiono delle fatine
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
114. DEV TOOLS
CLIENT HTTP PER TESTARE LE API
https://www.getpostman.com/
BASIC AUTH (SOLO PER I TEST)
https://github.com/WP-API/Basic-Auth
Network inspector (Chrome e Firefox)
LOGGER PER API
https://it.wordpress.org/plugins/wp-rest-api-
log/
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
116. PLUGIN UTILI
DISABILITARE LE API PER GLI UTENTI NON
LOGGATI
https://it.wordpress.org/plugins/disable-json-api/
ACF TO REST API
https://it.wordpress.org/plugins/acf-to-rest-api/
REST MANAGER - DISABILITARE PLUGIN PER
ROUTE
https://it.wordpress.org/plugins/rest-manager/
WP-CLI RESTFUL
https://github.com/wp-cli/restful
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
117. PLUGIN PER AUTENTICAZIONE
COOKIELESS
OAUTH 1
https://wordpress.org/plugins/rest-api-
oauth1/
JWT AUTH (JSON WEB TOKEN AUTH)
https://wordpress.org/plugins/jwt-
authentication-for-wp-rest-api/
BASIC AUTH (SOLO PER I TEST)
https://github.com/WP-API/Basic-Auth
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017
119. WORDPRESS MEETUP ROMAGNA
QUANDO:
ogni 1°Martedì del mese (9 Gennaio 2018)
DOVE:
Dinamo Coworking Space Cesena
PERCHÉ:
Per parlare di WordPress, conoscere bella
gente e condividere le proprie esperienze
wpromagna.com
@romagnawp
@andreacardinali - #WCRM - WordCamp Roma 15/16 Dicembre 2017