getting started with wp rest api
TRANSCRIPT
#WCKTM2016
Facilitated By
Kishor Kumar Mahato
Abiral Neupane
Ashok Maharjan
A fast track Workshop
Getting Started with WPREST API
#WCKTM2016
Who we are?
Kishor K. MahatoPython & WP Developer
Eagle Vision IT
Abiral NeupaneProject Manager & Web
Programmer
Eagle Vision IT
Ashok MaharjanWeb Programmer
Eagle Vision IT
2 . 1
#WCKTM2016
Our Timeline
Presentation: 25 MinutesWorkshop: 1 hours 20 Minutes
2 . 2
What will we cover today?
Why JSON REST API?Introduction to RESTQuick look on JSONIntroduction to WP RESTSetting up serverSetting up clientWorkflow of GET, POST, PUT and DELETE verbsBrief overview of authentication mechanism
#WCKTM2016
2 . 3
What is REST?
1. REST stands for Representational State Transfer2. An Architectural style for networked hypermedia applications3. It is primarily used to build Web services that are lightweight,
maintainable, and scalable.4. A service based on REST is called a RESTful service
#WCKTM2016
3 . 1
What is REST?
#WCKTM2016
3 . 2
Features
RepresentationsMessagesURIsUniform interfaceStatelessLinks between resources
#WCKTM2016
Representations
<Person> <ID>1</ID> <Name>M Vaqqas</Name> <Email>[email protected]</Email <Country>India</Country> </Person>
XML JSON{ "ID": "1", "Name": "M Vaqqas", "Email": "[email protected]", "Country": "India" }
#WCKTM2016
5 . 1
What is JSON?
Abbreviation for "JavaScript Object Notation" Simply a way to describe data that is lightweight and extremelyeasy to use.
#WCKTM2016
5 . 2
Why JSON over XML?
#WCKTM2016
5 . 3
#WCKTM2016
< VERB >
Messages: Block Structure
< URI > < HTTP version>
< Request Header >
< Request Body >
< VERB >
6 . 1
#WCKTM2016
Block Structure: VERB
GETPOSTPUTDELETE
6 . 2
< VERB >
#WCKTM2016
Messages: Block Structure
< VERB > < HTTP version>
< Request Header >
< Request Body >
< URI >
6 . 3
Block Structure: URI
Route for sending the requestExample:
https://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts
#WCKTM2016
6 . 4
< VERB >
#WCKTM2016
Messages: Block Structure
< VERB > < URI >
< Request Header >
< Request Body >
< HTTP version>
6 . 5
Block Structure: HTTP Version
Defines several resources based on versionIncludes:
AuthenticationSessionsRequest Methods
1.0 : GET, POST, HEAD1.1 : OPTIONS, PUT, DELETE, TRACE and CONNECT
Status Codes
#WCKTM2016
6 . 6
< VERB >
#WCKTM2016
Messages: Block Structure
< VERB > < URI > < HTTP version>
< Request Header >
< Request Body >
6 . 7
Block Structure: Request/Response
Request - a data load sent to the URL specifiedResponse - a data load sent as an acknowledgement of theRequestThe Request get received only if URL matches, and same goesfor Response
#WCKTM2016
6 . 8
Block Structure: Request/Response
#WCKTM2016
{ "status": 200, "currentTimestamp": 1477447617, "message": "success", "data": { "name": "Test Demo ed ", "position": "Test post ed", "company": "Test Company ed", "id": "29" } }
https://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp-demo/attendee/29
6 . 9
Messages: Request Structure (POST)
POST http://MyService/Person/ Host: MyService Content-Type: text/xml; charset=utf-8 Content-Length: 123 <?xml version="1.0" encoding="utf-8"?> <Person> <ID>1</ID> <Name>M Vaqqas</Name> <Email>[email protected]</Email> <Country>India</Country> </Person>
#WCKTM2016
7 . 1
Messages: Request Structure (GET)
GET http://www.w3.org/Protocols/rfc2616/rfc2616.html HTTP/1.1 Host: www.w3.org Accept: text/html,application/xhtml+xml,application/xml; … User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 …Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8,hi;q=0.6
#WCKTM2016
7 . 2
URIs
#WCKTM2016
GEThttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts
POSThttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts
DELETEhttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts/<id> PUThttps://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wp/v2/posts/<id>
Uniform Interface
#WCKTM2016
HTTPmethods
Resourcenames
Uniforminterfaces
Stateless
#WCKTM2016
Text
Links between resources
#WCKTM2016
http://example.com/api/v1/messages
[ { "id": 12345, "text": "Hello, world!" }, { "id": 12346, "text": "Testing, testing" }, ... ]
GET
About WP API
#WCKTM2016
Gives us ability to access WordPress's site dataThe API is rich with functionality. Explore the documentationStarted as a plugin, but will be completely added into core from version4.7
12 . 1
Why WP REST API?
#WCKTM2016
12 . 2
Routes out of the box
#WCKTM2016
/wp-json/ Shows all the routes and endpoints available
/wp-json/posts/ Create, read, update, and delete posts
/wp-json/users/ Create, read, update, and delete users
/wp-json/media/ Create, read, update, and delete media items
/wp-json/taxonomies/ Read taxonomies and terms
/wp-json/pages/ Create, read, update, and delete pages
12 . 3
Authentication
#WCKTM2016
12 . 4
Authentication
#WCKTM2016
Cookie AuthenticationOnly applicable if the REST API is used within WordPress
OAuth AuthenticationNeed to use OAuth Server plugin
Basic AuthenticationCan be used ONLY during developmentUses Username & Password as security detailsHas plugins for assisting the process
12 . 5
Prepare yourself
Install WordPressInstall WP REST API PluginInstall Post Man applicationPrettify the permalink
#WCKTM2016
13 . 1
Thanks ( for Now ) !
Any Questions?
#WCKTM2016
Let's Start
#WCKTM2016
15 . 1
How will we learn?
#WCKTM2016
Lesson 1: Understanding Client and Route1. Learn to use Post Man2. CRUD on default route
Lesson 2: Registering your own route1. Register new route2. CRUD on custom route
15 . 2
#WCKTM2016
Basic: Learn to use Post Man
15 . 4
#WCKTM2016
Moving onto Defaults
Go to Exercises > Lesson 1 folderYou will find Routes.txt ; Open itBased on the URI, Verb, and Body ( if needed ) provided, sendrequest using Post man
15 . 5
#WCKTM2016
Grab the Post
[ { "id": 11, "date": "2016-09-21T04:18:48", "date_gmt": "2016-09-21T04:18:48", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/09/21/title-from-rest-api-5/" }, "modified": "2016-10-25T16:55:42", "modified_gmt": "2016-10-25T16:55:42", "slug": "title-from-rest-api-5", "type": "post", "link": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/09/21/title-from-rest-api-5/" "title": {
/wp-json/wp/v2/postsGET
Response:
15 . 6
#WCKTM2016
Add the Post
/wp-json/wp/v2/postsPOST
{ "id": 31, "date": "2016-10-26T06:01:32", "date_gmt": "2016-10-26T06:01:32", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" "raw": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" }, "modified": "2016-10-26T06:01:32", "modified_gmt": "2016-10-26T06:01:32", "password": "", "slug": "post-from-postman", "status": "publish", "type": "post",
Response:{ "title": "Post From postman ", "content": "Lorem text", "status": "publish" }
Request
15 . 7
#WCKTM2016
Delete the Post
{ "id": 31, "date": "2016-10-26T06:01:32", "date_gmt": "2016-10-26T06:01:32", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" "raw": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/10/26/post-from-postman/" }, "modified": "2016-10-26T06:01:32", "modified_gmt": "2016-10-26T06:01:32", "password": "", "slug": "post-from-postman", "status": "publish", "type": "post",
/wp-json/wp/v2/posts/<id>DELETE
Response:
15 . 8
#WCKTM2016
Update the Post
{ "id": 34, "date": "2016-11-16T14:53:38", "date_gmt": "2016-11-16T14:53:38", "guid": { "rendered": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/11/16/post-from-postman-444/" "raw": "https://wordcamp2016-rest-api-cyberkishor.c9users.io/2016/11/16/post-from-postman-444/" }, "modified": "2016-11-16T14:53:52", "modified_gmt": "2016-11-16T14:53:52", "password": "", "slug": "post-from-postman-444", "status": "publish", "type": "post",
/wp-json/wp/v2/posts/<id>PUT
Response:
15 . 9
#WCKTM2016
Advanced: Register new Route
1. Go to Resources folder and you will find wcktm20162. Copy the plugin and Activate it.3. Go to Exercises > Lesson 24. Open get-attendees-route.php and copy the code5. Open the plugin - wcktm2016 > wcktm2016.php file6. After the comment /* Your code for route here */ paste your code7. Back to Exercises > Lesson 2, Open get-attendees-callback.php
and copy the code8. In wcktm2016.php file after comment /* Your code for route
callback here */ paste the code
15 . 10
#WCKTM2016
Open Postman and enter following details in it:
Advanced: Using the Route
Click on Send button
Verb: GET URI: https://wordcamp2016-rest-api-cyberkishor.c9users.io/wp-json/wcktm2016/attendees
15 . 11
#WCKTM2016
Repeat the same
Go to Exercises > Lesson 2Find other snippets and do the same like you did earlier
15 . 12
Bingo!!!You did It
15 . 13
Thank you !
#WCKTM2016
148910111416