From 2720c6c6576df8905602f90feab0756e5b46d545 Mon Sep 17 00:00:00 2001 From: Josh Pollock Date: Mon, 3 Nov 2014 21:37:51 -0500 Subject: [PATCH 1/2] posts endpoint example code PHP/jQuery #33 --- examples/posts/jQuery-AJAX.md | 98 ++++++++++++++++++++ examples/posts/php.md | 164 ++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 examples/posts/jQuery-AJAX.md create mode 100644 examples/posts/php.md diff --git a/examples/posts/jQuery-AJAX.md b/examples/posts/jQuery-AJAX.md new file mode 100644 index 0000000..e9bede5 --- /dev/null +++ b/examples/posts/jQuery-AJAX.md @@ -0,0 +1,98 @@ +Requests which require authentication assume that you have enqueued the client-js and therefore the following variables are localized: +WP_API_Settings.root +WP_API_Settings.nonce + + +### Get Recent Posts +``` + + $.ajax({ + type: 'GET', + url: WP_API_Settings.root + '/posts, + dataType: 'json', + success: function(posts){ + $.each( posts, function(index, post) { + //do something with each post + }); + }, + error: function(error){ + console.log(error); + } + }); + +``` + +### Get A Specific Post +``` + $.ajax({ + type: 'GET', + url: WP_API_Settings.root + '/posts/42, + dataType: 'json', + success: function(post){ + //do something with posts + }, + error: function(error){ + console.log(error); + } + }); + +``` + +### Create A Post + +``` + //create post data + var post = array( + 'title' : 'Lord of the Rings', + 'post_type' : 'book', + 'content_raw' : 'Best book ever.' + ); + + //prepare data + var data = JSON.stringify( post ); + + + $.ajax({ + type:"POST", + url: WP_API_Settings.root + '/posts, + dataType : 'json', + data: data, + beforeSend : function( xhr ) { + xhr.setRequestHeader( 'X-WP-Nonce', JP_POST_EDITOR.nonce ); + }, + success: function(response) { + alert( 'Post created. ID is ' + response.ID ); + }, + failure: function( response ) { + alert( 'FAIL!' ); + } + }); +``` + +### Edit A Post + +``` + //post data to edit + var post = array( + 'title' : 'The Lord Of The Rings, Being the third part of the Lord of the Rings' + ); + + //prepare data + var data = JSON.stringify( post ); + + $.ajax({ + type:"POST", + url: WP_API_Settings.root + '/posts/42, + dataType : 'json', + data: data, + beforeSend : function( xhr ) { + xhr.setRequestHeader( 'X-WP-Nonce', JP_POST_EDITOR.nonce ); + }, + success: function(response) { + alert( 'Post ' + response.ID ' + ' updated succesfully.' ); + }, + failure: function( response ) { + alert( 'FAIL!' ); + } + }); +``` diff --git a/examples/posts/php.md b/examples/posts/php.md new file mode 100644 index 0000000..5b24ead --- /dev/null +++ b/examples/posts/php.md @@ -0,0 +1,164 @@ +Requests to the API, from within WordPress, or any other HTTP request should use [the WordPress HTTP API](http://codex.wordpress.org/HTTP_API). + +All URLs should be constructed using either the function `json_url()` or `get_json_url()` when using multisite. These functions will return the root URL for the API, according to the current permalink structure. In addition, they will take into account the current value of the 'json_url' filter, which can be used to change the root url for the API. + +The URL for the simplest request to the posts route, which would return the most recent posts, would be constructed with `json_url( 'posts' );` + +### Get Most Recent Posts +* Number of posts will equal the posts_per_page option +``` php + //create url + $url = json_url( 'posts' ); + + //Make request via the WordPress HTTP API + $response = wp_remote_get( $url ); + + + //Check for error + if ( ! is_wp_error( $response ) ) { + //get just the body + $data = wp_remote_retrieve_body( $response ); + + //decode + $data = json_decode( $data ); + } + + +} +``` + +### Get A Post By ID +* In this case post ID 42 will be retrieved. + +``` php + //create url + $url = json_url( 'posts/42' ); + + //Make request via the WordPress HTTP API + $response = wp_remote_get( $url ); + + + //Check for error + if ( ! is_wp_error( $response ) ) { + //get just the body + $data = wp_remote_retrieve_body( $response ); + + //decode + $data = json_decode( $data ); + } + + +} +``` + +### Use filters +In order to add filters to the request URL, use `add_query_args()` to generate the query string. In this 8 posts, in descending order, whose author has the username "fry" are returned: +``` php + + //build array of args + $args = array( + 'filter[author_name]' => 'fry', + 'filter[posts_per_page]' => 8, + 'filter[order]' => 'DESC' + ); + + $url = add_query_arg( $args, json_url( 'posts' ) ); + + //Make request via the WordPress HTTP API + $response = wp_remote_get( $url ); + + //Check for error + if ( ! is_wp_error( $response ) ) { + //get just the body + $data = wp_remote_retrieve_body( $response ); + + //decode + $data = json_decode( $data ); + } +``` + +### Create Post +* This example uses basic authentication. A more secure authentication method should be used in production environments: + + //set up post data + $post = array( + 'title' => 'The Lord Of The Rings', + 'post_type' => 'book', + 'content' => 'Best book ever.' + ); + //encode as JSON + $post = json_encode( $post ); + + //URL for posts route + $url = json_url( 'posts' ); + + //prepare headers with basic authentication + $headers = array ( + 'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ), + ); + + //prepare args for request + $arg = array ( + 'method' => 'POST', + 'timeout' => 45, + 'headers' => $headers, + 'body' => $post + ); + + //create post + $response = wp_remote_post( $url, $args ); + + if ( is_wp_error( $response ) ) { + //it failed + $id = false; + } + else { + //success! get the ID of new post + $post = json_decode( wp_remote_retrieve_body( $response ) ); + $id = $post[ 'id' ]; + } +``` + +### Update A Post +* Change the title of an existing post, in this case, post ID 42 + +``` php + +//set up post data + $post = array( + 'title' => 'The Lord Of The Rings, Being the third part of the Lord of the Rings', + ); + //encode as JSON + $post = json_encode( $post ); + + //URL for this post + $url = json_url( 'posts/42' ); + + //prepare headers with basic authentication + $headers = array ( + 'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ), + ); + + //prepare args for request + $arg = array ( + 'method' => 'POST', + 'timeout' => 45, + 'headers' => $headers, + 'body' => $post + ); + + //create post + $response = wp_remote_post( $url, $args ); + + if ( is_wp_error( $response ) ) { + //it failed + $id = false; + } + else { + //success! get the ID of new post + $post = json_decode( wp_remote_retrieve_body( $response ) ); + $id = $post[ 'id' ]; + } +``` + + From dbe6a5f117fe7fdc4accf44afdd79672def99444 Mon Sep 17 00:00:00 2001 From: Josh Pollock Date: Tue, 4 Nov 2014 16:11:40 -0500 Subject: [PATCH 2/2] update base url based on feedback from @rmccue --- examples/posts/php.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/posts/php.md b/examples/posts/php.md index 5b24ead..b56fdae 100644 --- a/examples/posts/php.md +++ b/examples/posts/php.md @@ -1,14 +1,16 @@ Requests to the API, from within WordPress, or any other HTTP request should use [the WordPress HTTP API](http://codex.wordpress.org/HTTP_API). -All URLs should be constructed using either the function `json_url()` or `get_json_url()` when using multisite. These functions will return the root URL for the API, according to the current permalink structure. In addition, they will take into account the current value of the 'json_url' filter, which can be used to change the root url for the API. +When making requests to a the current site, URLs should be constructed using either the function `json_url()` or `get_json_url()` for a site in a multisite network other than the current site. These functions will return the root URL for the API, according to the current permalink structure. In addition, they will take into account the current value of the 'json_url' filter, which can be used to change the root url for the API. -The URL for the simplest request to the posts route, which would return the most recent posts, would be constructed with `json_url( 'posts' );` +The URL for the simplest request to the posts route, using these functions, which would return the most recent posts, would be constructed with `json_url( 'posts' );` + +The example code below use an undefined variable `$root_url` which should be the root URL for the site's API. ### Get Most Recent Posts * Number of posts will equal the posts_per_page option ``` php //create url - $url = json_url( 'posts' ); + $url = $base_url . 'posts'; //Make request via the WordPress HTTP API $response = wp_remote_get( $url ); @@ -32,7 +34,7 @@ The URL for the simplest request to the posts route, which would return the most ``` php //create url - $url = json_url( 'posts/42' ); + $url = $base_url . 'posts/42'; //Make request via the WordPress HTTP API $response = wp_remote_get( $url ); @@ -62,7 +64,9 @@ In order to add filters to the request URL, use `add_query_args()` to generate t 'filter[order]' => 'DESC' ); - $url = add_query_arg( $args, json_url( 'posts' ) ); + //construct URL + $url = $base_url . 'posts'; + $url = add_query_arg( $args, $url ); //Make request via the WordPress HTTP API $response = wp_remote_get( $url ); @@ -90,7 +94,7 @@ In order to add filters to the request URL, use `add_query_args()` to generate t $post = json_encode( $post ); //URL for posts route - $url = json_url( 'posts' ); + $url = $base_url . 'posts'; //prepare headers with basic authentication $headers = array ( @@ -132,7 +136,7 @@ In order to add filters to the request URL, use `add_query_args()` to generate t $post = json_encode( $post ); //URL for this post - $url = json_url( 'posts/42' ); + $url = $base_url . 'posts/42'; //prepare headers with basic authentication $headers = array (