├── .gitattributes ├── .gitignore ├── Chapter_01_Codes ├── rest-chash.txt ├── rest-java.txt ├── rest-js.txt ├── rest-perl.txt ├── rest-php.txt ├── rest-python.txt └── rest-ruby.txt ├── Chapter_02_Codes ├── chapter2-getmeta.txt ├── chapter2-getrequest.txt ├── chapter2-postmeta.txt ├── chapter2-postoutputs.txt ├── chapter2-remotecopy.txt └── chapter2-remotecreate.txt ├── Chapter_03_Codes ├── code-file-1.txt ├── code-file-2.txt ├── code-file-3.txt ├── code-file-4.txt └── code-file-5.txt ├── Chapter_04_Codes ├── code-file-chap4-1.txt ├── code-file-chap4-2.txt └── code-file-chap4-3.txt ├── Chapter_05_Codes ├── code-file-1.txt └── code-file-2.txt ├── Chapter_06_Codes ├── code-chap6-1.txt ├── code-chap6-2.txt ├── code-chap6-3.txt └── code-chap6-4.txt ├── README.md └── license /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-chash.txt: -------------------------------------------------------------------------------- 1 | static string HttpGet(string url) { 2 | HttpWebRequest req = WebRequest.Create(url) 3 | as HttpWebRequest; 4 | string result = null; 5 | using (HttpWebResponse resp = req.GetResponse() 6 | as HttpWebResponse) 7 | { 8 | StreamReader reader = 9 | new StreamReader(resp.GetResponseStream()); 10 | result = reader.ReadToEnd(); 11 | } 12 | return result; 13 | } 14 | 15 | /*POST request*/ 16 | static string HttpPost(string url, 17 | string[] prName, string[] prVal) 18 | { 19 | HttpWebRequest req = WebRequest.Create(new Uri(url)) 20 | as HttpWebRequest; 21 | req.Method = "POST"; 22 | req.ContentType = "application/x-www-form-urlencoded"; 23 | 24 | // Creating a string, encoded and with all parameters 25 | // Assuming that the arrays prName and prVal are of equal length 26 | StringBuilder przz = new StringBuilder(); 27 | for (int i = 0; i < prName.Length; i++) { 28 | przz.Append(prName[i]); 29 | przz.Append("="); 30 | przz.Append(HttpUtility.UrlEncode(prVal[i])); 31 | przz.Append("&"); 32 | } 33 | 34 | // Encoding the parameters 35 | byte[] frDat = 36 | UTF8Encoding.UTF8.GetBytes(przz.ToString()); 37 | req.ContentLength = frDat.Length; 38 | 39 | // Sending the request 40 | using (Stream post = req.GetRequestStream()) 41 | { 42 | post.Write(frDat, 0, frDat.Length); 43 | } 44 | 45 | // Getting the response 46 | string result = null; 47 | using (HttpWebResponse resp = req.GetResponse() 48 | as HttpWebResponse) 49 | { 50 | StreamReader reader = 51 | new StreamReader(resp.GetResponseStream()); 52 | result = reader.ReadToEnd(); 53 | } 54 | 55 | return result; 56 | } 57 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-java.txt: -------------------------------------------------------------------------------- 1 | public static String httpGet(String urlStr) throws IOException { 2 | URL url = new URL(urlStr); 3 | HttpURLConnection conn = 4 | (HttpURLConnection) url.openConnection(); 5 | if (conn.getResponseCode() != 200) { 6 | throw new IOException(conn.getResponseMessage()); 7 | } 8 | // Buffering the result into a string 9 | BufferedReader drdr = new BufferedReader( 10 | new InputStreamReader(conn.getInputStream())); 11 | StringBuilder sb = new StringBuilder(); 12 | String line; 13 | while ((line = drdr.readLine()) != null) { 14 | sb.append(line); 15 | } 16 | drdr.close(); 17 | conn.disconnect(); 18 | return sb.toString(); 19 | } 20 | 21 | //POST request 22 | 23 | public static String httpPost(String urlStr, String[] prName, 24 | String[] prVal) throws Exception { 25 | URL url = new URL(urlStr); 26 | HttpURLConnection conn = 27 | (HttpURLConnection) url.openConnection(); 28 | conn.setRequestMethod("POST"); 29 | conn.setDoOutput(true); 30 | conn.setDoInput(true); 31 | conn.setUseCaches(false); 32 | conn.setAllowUserInteraction(false); 33 | conn.setRequestProperty("Content-Type", 34 | "application/x-www-form-urlencoded"); 35 | 36 | // Creating form content 37 | OutputStream out = conn.getOutputStream(); 38 | Writer writer = new OutputStreamWriter(out, "UTF-8"); 39 | for (int i = 0; i < prName.length; i++) { 40 | writer.write(prName[i]); 41 | writer.write("="); 42 | writer.write(URLEncoder.encode(prVal[i], "UTF-8")); 43 | writer.write("&"); 44 | } 45 | writer.close(); 46 | out.close(); 47 | 48 | if (conn.getResponseCode() != 200) { 49 | throw new IOException(conn.getResponseMessage()); 50 | } 51 | 52 | // Buffering the result into a string 53 | BufferedReader drdr = new BufferedReader( 54 | new InputStreamReader(conn.getInputStream())); 55 | StringBuilder bsbs = new StringBuilder(); 56 | String line; 57 | while ((line = drdr.readLine()) != null) { 58 | bsbs.append(line); 59 | } 60 | drdr.close(); 61 | 62 | conn.disconnect(); 63 | return bsbs.toString(); 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-js.txt: -------------------------------------------------------------------------------- 1 | function createRequest() { 2 | var result = null; 3 | if (window.XMLHttpRequest) { 4 | result = new XMLHttpRequest(); 5 | if (typeof xmlhttp.overrideMimeType != 'undefined') { 6 | result.overrideMimeType('text/xml'); // Or anything else 7 | } 8 | } 9 | else if (window.ActiveXObject) { 10 | result = new ActiveXObject("Microsoft.XMLHTTP"); 11 | } 12 | return result; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-perl.txt: -------------------------------------------------------------------------------- 1 | my $browser = LWP::UserAgent->new; 2 | my $url = 'http://www.example.com/database/1191'; 3 | my $response = $browser->post($url, 4 | [ 5 | 'firstName' => 'Sample', 6 | 'lastName' => 'User' 7 | ]; 8 | ); 9 | die 'Error getting $url' unless $response->is_success; 10 | print 'Content type is ', $response->content_type; 11 | print 'Content is:'; 12 | print $response->content; 13 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-php.txt: -------------------------------------------------------------------------------- 1 | function httpRequest($host, $port, $method, $path, $prms) { 2 | // prms is to map from name to value 3 | $prmstr = ""; 4 | foreach ($prms as $name, $val) { 5 | $prmstr .= $name . "="; 6 | $prmstr .= urlencode($val); 7 | $prmstr .= "&"; 8 | } 9 | // Assign defaults to $method and $port 10 | if (empty($method)) { 11 | $method = 'GET'; 12 | } 13 | $method = strtoupper($method); 14 | if (empty($port)) { 15 | $port = 80; // Default HTTP port 16 | } 17 | 18 | // Create the connection 19 | $sock = fsockopen($host, $port); 20 | if ($method == "GET") { 21 | $path .= "?" . $prmstr; 22 | } 23 | fputs($sock, "$method $path HTTP/1.1\r\n"); 24 | fputs($sock, "Host: $host\r\n"); 25 | fputs($sock, "Content-type: " . 26 | "application/x-www-form-urlencoded\r\n"); 27 | if ($method == "POST") { 28 | fputs($sock, "Content-length: " . 29 | strlen($prmstr) . "\r\n"); 30 | } 31 | fputs($sock, "Connection: close\r\n\r\n"); 32 | if ($method == "POST") { 33 | fputs($sock, $prmstr); 34 | } 35 | // Buffer the result 36 | $result = ""; 37 | while (!feof($sock)) { 38 | $result .= fgets($sock,1024); 39 | } 40 | fclose($sock); 41 | return $result; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-python.txt: -------------------------------------------------------------------------------- 1 | import urllib2 2 | url = 'http://www.example.com/database/1191' 3 | response = urllib2.urlopen(url).read() 4 | And for POST requests, we will once again rely on the urllib2 module: 5 | import urllib 6 | import urllib2 7 | url = 'http://www.example.com/database/user' 8 | params = urllib.urlencode({ 9 | 'firstName': 'Sample', 10 | 'lastName': 'User' 11 | }) 12 | response = urllib2.urlopen(url, params).read() 13 | -------------------------------------------------------------------------------- /Chapter_01_Codes/rest-ruby.txt: -------------------------------------------------------------------------------- 1 | require 'net/http' 2 | url = 'http://www.example.com/database/1191' 3 | resp = Net::HTTP.get_response(URI.parse(url)) 4 | resp_text = resp.body 5 | In the above example, we are using an object to handle the HTTP response code. 6 | Similarly, for POST requests: 7 | require 'net/http' 8 | url = 'http://www.example.com/database/user' 9 | params = { 10 | firstName => 'Sample', 11 | lastName => 'User' 12 | } 13 | resp = Net::HTTP.post_form(url, params) 14 | resp_text = resp.body 15 | -------------------------------------------------------------------------------- /Chapter_02_Codes/chapter2-getmeta.txt: -------------------------------------------------------------------------------- 1 | //GET post meta fields; chapter 2 2 | 3 | function get_post_meta_clbk( $object, $field_name, $request ) { 4 | return get_post_meta( $object[ 'id' ], $field_name ); 5 | } 6 | function update_post_meta_clbk( $value, $object, $field_name ) { 7 | return update_post_meta( $object[ 'id' ], $field_name, $value ); 8 | } 9 | 10 | add_action( 'rest_api_init', function() { 11 | register_api_field( 'painter', 12 | 'water_color', 13 | array( 14 | 'get_callback' => 'get_post_meta_clbk', 15 | 'update_callback' => 'update_post_meta_clbk', 16 | 'schema' => null, 17 | ) 18 | ); 19 | }); 20 | -------------------------------------------------------------------------------- /Chapter_02_Codes/chapter2-getrequest.txt: -------------------------------------------------------------------------------- 1 | //GET request; chapter 2 2 | 3 | $response = wp_remote_get( $url ); 4 | function get_json( $url ) { 5 | //GET remote site 6 | $response = wp_remote_get( $url ); 7 | //Checking for errors 8 | if ( is_wp_error( $response ) ) { 9 | return sprintf( 'Your URL %1s could not be retrieved', $url ); 10 | //GET only body 11 | $data = wp_remote_retrieve_body( $response ); 12 | } 13 | //return if no error 14 | if ( ! is_wp_error( $response ) ) { 15 | //Now, decode and return 16 | return json_decode( $response ); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter_02_Codes/chapter2-postmeta.txt: -------------------------------------------------------------------------------- 1 | //POST meta fields; chapter 2 2 | 3 | //GET URL request 4 | $url = rest_url( 'wp/v2/posts/1/meta' ); 5 | //ADD basic headers for authentication 6 | $headers = array ( 7 | 'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ), 8 | ); 9 | //ADD meta value to body 10 | $body = array( 11 | 'key' => 'water_color', 12 | 'value' => 'blue' 13 | ); 14 | //POST request 15 | $response = wp_remote_request( $url, array( 16 | 'method' => 'POST', 17 | 'headers' => $headers, 18 | 'body' => $body 19 | ) 20 | ); 21 | //if no error, we GET ID of meta key 22 | $body = wp_remote_retrieve_body( $response ); 23 | if ( ! is_wp_error( $body ) ) { 24 | $body = json_decode( $body ); 25 | $meta_id = $body->id; 26 | echo $body->value; 27 | if ( $meta_id ) { 28 | //ADD meta ID to URL 29 | $url .= '/' . $meta_id; 30 | //SEND value 31 | $body = array( 32 | 'value' => 'blue' 33 | ); 34 | $response = wp_remote_request( $url, array( 35 | 'method' => 'POST', 36 | ) 37 | ); 38 | 'headers' => $headers, 39 | 'body' => $body 40 | //if no error, then echo the value 41 | $body = wp_remote_retrieve_body( $response ); 42 | if ( ! is_wp_error( $body ) ) { 43 | $body = json_decode( $body ); 44 | echo $body->value; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Chapter_02_Codes/chapter2-postoutputs.txt: -------------------------------------------------------------------------------- 1 | //Post output; chapter 2 2 | 3 | $response = wp_remote_get( $url ); 4 | function get_json( $url ) { 5 | //GET remote site 6 | $response = wp_remote_get( $url ); 7 | //Checking for errors 8 | if ( is_wp_error( $response ) ) { 9 | return sprintf( 'Your URL %1s could not be retrieved', $url ); 10 | //GET only body 11 | $data = wp_remote_retrieve_body( $response ); 12 | } 13 | //return if no error 14 | if ( ! is_wp_error( $response ) ) { 15 | //Now, decode and return 16 | return json_decode( $response ); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter_02_Codes/chapter2-remotecopy.txt: -------------------------------------------------------------------------------- 1 | //Copy post from remote site; chapter 2 2 | 3 | function insert_post_from_json( $post ) { 4 | //checking to see if array or object; because we can work only with array 5 | if ( is_array( $post ) || ( is_object( $post ) && ! is_wp_error( $post ) ) ) { 6 | //ensure $post is an array; or force typecast 7 | $post = (array) $post; 8 | } 9 | else { 10 | return sprintf( '%1s must be an object or array', __FUNCTION__ ); 11 | } 12 | //Create new array for conversion 13 | //Set ID as post_id to try and use the same ID; note that leaving ID as ID would //UPDATE an existing post of the same ID 14 | $convert_keys = array( 15 | 'title' => 'post_title', 16 | 'content' => 'post_content', 17 | 'slug' => 'post_name', 18 | 'status' => 'post_status', 19 | 'parent' => 'post_parent', 20 | 'excerpt' => 'post_excerpt', 21 | 'date' => 'post_date', 22 | 'type' => 'post_type', 23 | 'ID' => 'post_id', 24 | ); 25 | //copy API response and unset old key 26 | foreach ( $convert_keys as $from => $to ) { 27 | if ( isset( $post[ $from ] ) ) { 28 | $post[ $to ] = $post[ $from ]; 29 | unset( $post[ $from ] ); 30 | } 31 | } 32 | //remove all keys of $post that are disallowed and convert objects (if any) to strings 33 | $allowed = array_values( $convert_keys ); 34 | foreach( $post as $key => $value ) { 35 | if( ! in_array( $key, $allowed ) ) { 36 | unset( $post[ $key ] ); 37 | } else{ 38 | if ( is_object( $value ) ) { 39 | $post[ $key ] = $value->rendered; 40 | } 41 | } 42 | } 43 | //All done. Create post and return its ID 44 | return wp_insert_post( $post ); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Chapter_02_Codes/chapter2-remotecreate.txt: -------------------------------------------------------------------------------- 1 | //Create post on remote site; chapter 2 2 | 3 | //Authentication 4 | $url = 'http://example.com/wp-json/wp/v2/posts/1'; 5 | $body = get_json( $url ); 6 | if ( is_object( $post ) ) { 7 | $body = $post; 8 | $headers = array ( 9 | 'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ), 10 | ); 11 | $remote_url = 'http://example-remote.com/wp-json/wp/v2/posts'; 12 | } 13 | 14 | $headers = array ('Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ), 15 | ); 16 | 17 | //Copying response 18 | $response = wp_remote_post( $remote_url, array ( 19 | 'method' => 'POST', 20 | 'timeout' => 45, 21 | 'redirection' => 5, 22 | 'httpversion' => '1.0', 23 | 'blocking' => true, 24 | 'headers' => $headers, 25 | 'body' => json_encode( $post ), 26 | 'cookies' => array () 27 | ) 28 | ); 29 | 30 | //Checking if error occurred. 31 | if ( is_wp_error( $response ) ) { 32 | $error_message = $response->get_error_message(); 33 | echo sprintf( '
Error: %1s
', $error_message ); 34 | } 35 | else { 36 | echo 'Response:'; 37 | print_r( $response ); 38 | echo ''; 39 | } 40 | } 41 | else { 42 | $error_message = 'Data invalid!'; 43 | echo sprintf( '
Error: %1s
', $error_message ); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Chapter_03_Codes/code-file-1.txt: -------------------------------------------------------------------------------- 1 | $sites = wp_get_sites(); 2 | $the_list = arr(); 3 | foreach( $sites as $site ) { 4 | $response = wp_remote_get( get_rest_url( $site->site_id, 'wp/v2/terms/categories' ) ); 5 | if ( ! is_wp_error( $response ) ) { 6 | $terms = json_decode( wp_remote_retrieve_body( $response ) ); 7 | $term_list = arr(); 8 | foreach( $terms as $term ) { 9 | $term_list[] = sprintf( '' + user.name + '
' ); 10 | $( '#email' ).val( user.email ); 11 | } ); 12 | }); 13 | 14 | jQuery( document ).ready(function( $ ) { 15 | var get_user_data; 16 | (get_user_data = function () { 17 | $.ajax( { 18 | url: Slug_API_Settings.root + 'wp/v2/users/' + Slug_API_Settings.current_user_id + 19 | '?context=edit', 20 | method: 'GET', 21 | beforeSend: function ( xhr ) { 22 | xhr.setRequestHeader( 'X-WP-Nonce', Slug_API_Settings.nonce ); 23 | } 24 | } ).done( function ( user ) { 25 | $( '#username' ).html( '' + user.name + '
' ); 26 | $( '#email' ).val( user.email ); 27 | } ); 28 | })(); 29 | $( '#profile-form' ).on( 'submit', function(e) { 30 | e.preventDefault(); 31 | $.ajax( { 32 | url: Slug_API_Settings.root + 'wp/v2/users/' + Slug_API_Settings.current_user_id, 33 | method: 'POST', 34 | beforeSend: function ( xhr ) { 35 | xhr.setRequestHeader( 'X-WP-Nonce', Slug_API_Settings.nonce ); 36 | }, 37 | data:{ 38 | email: $( '#email' ).val() 39 | } 40 | } ).done( function ( response ) { 41 | console.log( response ) 42 | } ) 43 | }); 44 | }); 45 | 46 | -------------------------------------------------------------------------------- /Chapter_04_Codes/code-file-chap4-1.txt: -------------------------------------------------------------------------------- 1 | //Basic function for setup 2 | 3 | function my_rest_post_editor_form( ) { 4 | $form = ' 5 | 10 |