WordPress REST-API: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 86: Regel 86:
 
* ''Endpoints'' are combinations of ''verbs'' and ''routes''
 
* ''Endpoints'' are combinations of ''verbs'' and ''routes''
  
E.g., in
+
E.g.:
  
 
  GET wp/v2/posts
 
  GET wp/v2/posts

Versie van 9 apr 2019 13:40

De WordPress REST-API is bedoeld voor taal-onafhankelijke interactie over HTTP tussen een site en een computer. Vermoedelijk is REST-API completer en actueler dan de WP-CLI, omdat deze laatste soms gebaseerd is op deze eerste [1]

Voordelen REST-API

  • Vermoedelijk completer & actueler dan WP-CLI
  • Taal-onafhankelijk. Ik kan het dus ook gebruiken vanuit Python
  • Compleet? Ik hoop dat de REST-API alle functionaliteit van een WordPress-site dekt, zodat ik alles via één interface kan regelen. Dit is overigens nog maar de vraag: Kan ik de taal van objecten instellen tijdens import?

Nadelen REST-API

  • Weer een nieuwe techniek om te leren
  • Ik betwijfel of ik zit te wachten om JSON te leren
  • Ik heb de indruk dat de documentatie tav. REST-API+Python beperkt is
  • Onduidelijk hoe je data importeert die op het snijvlak van meerdere plugins ligt, zoals de taal van producten tijdens import

Voorbeelden

  • Mobiele apps die data uitwisselen met een server (bv. een winkel-app). JSON is compacter dan XML, en dat speelt hierbij mogelijk een rol
  • Data importeren in een site.

Hoe compleet is deze interface?

De WP-CLI heeft beperkingen: Je moet maar mazzel hebben dat datgene wat je wilt, is opgenomen in de WP-CLI. Ik vermoed dat hetzelfde geldt voor de REST-API, al is de situatie hier vermoedelijk beter

  • Sommige sites (wo. [2]) lijken te suggeren dat deze api compleet is
  • Polylang ondersteunt REST-API alleen voor betaalde versies. Het is dus niet vanzelfsprekend dat een REST-API-interface werkt.

REST

[3]:

REST is an architectural style that helps create and organize a distributed system. It describes the web as a distributed hypermedia application whose linked resources communicate by exchanging representations of resource state.

Resources are the main building blocks of the REST architecture. In fact, they are the main building blocks of the web itself to the extent that the web is sometimes referred to as “resource-oriented”.

When talking about WordPress, these resources are discrete entities like posts, pages, comments, users, and custom post types etc. To interact with resources, URIs (Uniform Resource Identifier) are used, and as the name suggests, it’s an identifier for a resource.

A RESTful service treats URIs as the primary way to address an underlying resource. These resource may have several representations. For example, an image file might be available in .JPG, .GIF, or .PNG formats. The relationship between resources and URIs is one-to-many. A URI can only point to one specific resource but a resource may have more than one URIs.

Some resources currently supported by the WP REST API:

  • Posts
  • Pages
  • Media
  • Custom Post Types
  • Post Meta
  • Revisions
  • Comments
  • Terms
  • Users

We can perform different actions on these resources using HTTP verbs.

Verbs

[4]:

The REST API offers several HTTP requests, known as HTTP verbs or verbs, the first four of which are CRUD actions (create, read, update & delete). The last two verbs assist a client in determining whether a resource exists and what HTTP verbs are available for it to perform further operations. This makes the RESTful service a self-documenting system:

  1. GET: Read or retrieve a resource (cRud). A GET request is idempotent i.e. a client can call it many times but it will not affect the state of a resource.
  2. POST: Create a new resource (Crud)
  3. PUT: Update a resource (crUd)
  4. DELETE: Delete a resource (cruD)
  5. HEAD: Check if a resource exists without returning its representation
  6. OPTIONS: Retrieve all the verbs supported by a resource

Some examples: Retrieve the collection of all post entities:

GET wp/v2/posts

Retrieve the post with id=100:

GET wp/v2/posts/100

Create a new post:

POST wp/v2/posts

Update the post with id=100:

PUT wp/v2/posts/100

Routes & endpoints

  • Routes are the paths to resources
  • Endpoints are combinations of verbs and routes

E.g.:

GET wp/v2/posts
  • GET is the verb
  • wp/v2/posts is the route
  • GET wp/v2/posts is the endpoint

Ander voorbeeld: Voeg /wp-json achter de URL van een WordPress-site, en je ziet alle routes & endpoints. Met behulp van de Chrome JSON Viewer, is het ook nog leesbaar. Voorbeeld: https://stapelbakken.nl/wp-json. Dit zijn 36.000 regels! De eerste paar regels:

// 20190408161835
// https://stapelbakken.nl/wp-json

{
  "name": "Stapelbakken.nl",
  "description": "Snel en gemakkelijk Haceka Stapelbakken bestellen",
  "url": "https://stapelbakken.nl",
  "home": "https://stapelbakken.nl",
  "gmt_offset": "0",
  "timezone_string": "",
  "namespaces": [
    "oembed/1.0",
    "contact-form-7/v1",
    "wc/v1",
    "wc/v2",
    "wc/v3",
    "wordfence/v1",
    "wp/v2"
  ],

Openstaande vraagstukken

  • Hoe kan ik een product posten en daarbij de taal specificeren? Het gaat om de combinatie van WooCommerce & Polylang

Zie ook

Bronnen

Introductie & overzicht

Tutorial Tutsplus

Oogt compleet, maar is uit jan. 2016. Dit is op dit moment (april 2019) mijn belangrijkste bron.

Voorbeelden