Integrating With
Third-Party APIs
     Randy Hoyt
       @randyhoyt
Overview


• APIs: Defined
• Three Examples:
    1. Get Treehouse Badges
    2. Get ShopLocket Products
    3. Post Facebook & Twitter Updates
• Best Practices
                                 @randyhoyt
API
Application Programming Interface
A point where two systems,
  subjects, organizations, etc.,
  meet and interact.


Interface, http://dictionary.com/browse/interface
[Keyboard]
[Remote Control]
[Remote Control]
[Lock and Key]
An interface for software
 components to communicate
 with each other.


Wikipedia: API, http://trhou.se/defineAPI
API Examples




• Android SDK: Camera API




                            @randyhoyt
import android.hardware.Camera;


Camera.getNumberOfCameras();
Camera.open(cameraId);
API Examples




• Android SDK: Camera API
• WordPress: Plugin API


                            @randyhoyt
<?php
/*
Plugin Name: Treehouse Badges
Description: Displays the badges ...
Version: 1.0
*/
?>


add_action('init','treehouse_badges_init');
...
API Examples




• Android SDK: Camera API
• WordPress: Plugin API
• HTML5: Canvas API

                            @randyhoyt
var canvas = document.getElementById("canvas");

var context = canvas.getContext("2d");

context.fillRect(10, 10, 40, 380, "#000000");

context.drawImage(img, x, y, 100, 77);
Web APIs
REST
Representational State Transfer
RESTful API
Example #1: Treehouse
[Treehouse screenshot]
JavaScript Object Notation:
 Text-based open standard
 designed for human-readable
 data interchange.


Wikipedia: JSON, http://trhou.se/defineJSON
{
     "firstName": "John",
     "lastName": "Smith",
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": 10021
     },
     "phoneNumber": [
         {
             "type": "home",
             "number": "212 555-1234"
         },
         {
             "type": "fax",
             "number": "646 555-4567"
         }
     ]
 }

Wikipedia: JSON, http://trhou.se/defineJSON
[Treehouse JSON]
[HTML]
Asynchronous JavaScript and XML:
 Technique used by the browser to
 retrieve data from a server in the
 background without interfering with
 the existing page


Wikipedia: Ajax, http://trhou.se/defineAJAX
[AJAX Request Code]
[Badge Code]
[Badge Display]
<?php

$curl = curl_init();

curl_setopt_array($curl, array(

      CURLOPT_RETURNTRANSFER => 1,

      CURLOPT_URL => 'http://teamtreehou...

));

$resp = curl_exec($curl);

curl_close($curl);

?>
GET	
  Request	
  with	
  HTTPful




HTTPful, http://phphttpclient.com/
GET	
  Request	
  with	
  HTTParty




HTTParty, http://httparty.rubyforge.org/
GET	
  Request	
  with	
  WordPress	
  HTTP	
  API




WordPress, http://codex.wordpress.org/HTTP_API
API Questions




• Does it have to
  be real time?

• What if something
  goes wrong?

                      @randyhoyt
Example #2: ShopLocket
[Screenshot of WP]
[Screenshot of WordPress]
[Screenshot of Product Picker]
[Screenshot of Product Picker]
[Screenshot of Shortcode]
[Screenshot of Website]
Authentication
OAuth
  An open protocol to allow secure
  authorization in a simple and standard
  method from web, mobile and desktop
  applications.




OAuth, http://oauth.net/
App ID
5f4a89f2d6655ac7a8859343d6d15257
9410e7b659b200e00aaf3721cf46269e


App Secret
fa963a1ef3702c16d934500f7621697e2
d6bfd05d3ef1751a32bdeb2a0599020
[ShopLocket]
[ShopLocket Login]
[ShopLocket]
/? code = 53fe... & state = auth...
$args:
          code = $_GET["code"]
        app id = [app id]
    app secret = [app secret]


           [GET Request]
$response = wp_remote_post(
               'https://www.shoplocket.com/'
                   . 'oauth/token',
               $args
            );
Token:
310826b20a535a422ccaa46d65c7065f
83e403b9218a38962ab4e43021b93585
[Token]
API Questions




• Does it have to
  be real time?

• What if something
  goes wrong?

                      @randyhoyt
[Refresh Button]
Example #3: Facebook & Twitter
[Screenshot of Cultivatr]
[Screenshot of Edit Screen]
[Screenshot of Date Picker]
[Diagram]
API Questions




• Does it have to
  be real time?

• What if something
  goes wrong?

                      @randyhoyt
REST: Types of Requests



• GET          (Read)
• POST         (Create)
• PUT          (Update)
• DELETE       (Delete)

                            @randyhoyt
require	
  '../tmhOAuth.php';
  require	
  '../tmhUtilities.php';
  $tw	
  =	
  new	
  tmhOAuth(array(
  	
  	
  'consumer_key'	
  	
  	
  	
  =>	
  'APP_ID',
  	
  	
  'consumer_secret'	
  =>	
  'APP_SECRET',
  	
  	
  'user_token'	
  	
  	
  	
  	
  	
  =>	
  'A_USER_TOKEN',
  	
  	
  'user_secret'	
  	
  	
  	
  	
  =>	
  'A_USER_SECRET',
  ));

  $code	
  =	
  $tw-­‐>request(
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  'POST',
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $tw-­‐>url('1/statuses/update'),
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  array('status'	
  =>	
  'My	
  Tweet')
  	
  	
  	
  	
  	
  	
  	
  	
  	
  );

Twitter Libraries, https://dev.twitter.com/docs/twitter-libraries
Best Practices
Best Practices




• Read the Documentation
• Use Available Libraries
• Log Everything

                            @randyhoyt
Questions?
             @randyhoyt

Api