Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Web Services with Drupal 7

Building Web Services with Drupal 7

Session based on the Drupalize.Me video series with the same name. http://drupalize.me/series/building-web-services-drupal-7

Learn about what web services are and how to use the popular services module for Drupal 7 to add a web service to your Drupal powered site.

eojthebrave

March 19, 2014
Tweet

More Decks by eojthebrave

Other Decks in Technology

Transcript

  1. Building Web Services with Drupal 7
    March 19th, 2014 - Joe Shindelar - @eojthebrave
    !
    http://lb.cm/acquia-servicesmodule

    View full-size slide

  2. Hi, I’m Joe …
    @eojthebrave
    Lullabot / Drupalize.Me

    View full-size slide

  3. Services & Drupal 7
    http://lb.cm/acquia-servicesmodule
    !
    Drupalize.Me Series:
    Building Web Services with Drupal 7

    View full-size slide

  4. Lets GO!
    ‣ What are web services and why do I want one?
    ‣ Drupal 7 services module core concepts and features.
    ‣ How to test your new web service.
    ‣ The future.

    View full-size slide

  5. What Are Web Services?
    The W3C defines web service as: a software system designed to support
    interoperable machine-to-machine interaction over a network.
    API stands for Application Programmer Interface
    API’s specify how software components should interact with one another.
    APIs

    View full-size slide

  6. A standardized, and documented way to access and
    interact with your applications content and features.

    View full-size slide

  7. Examples of Existing Web Services
    ‣ Twitter
    ‣ Netflix
    ‣ Amazon S3, EC2, etc.
    ‣ Yahoo weather API
    ‣ Flight tracking data

    View full-size slide

  8. Why Web Services?
    ‣ Provide access for mobile devices (internet of things)
    ‣ Legacy integration
    ‣ Digital asset management system
    ‣ Partners who need/want your data - weather, MLS listing, etc.
    ‣ Social media

    View full-size slide

  9. Services & Drupal 7
    Services Module - 7.x-3.x
    http://drupal.org/project/services
    !
    Drupal 7.x
    Information is specific to Drupal 7 though applicable to
    other versions as well.

    View full-size slide

  10. /videos/theming-component
    /api/v1/video/1381.json

    View full-size slide

  11. Chunks vs. Blobs
    It is easier for machines to understand structured
    content like JSON or XML than a giant blob of text.
    !
    Drupal already stores data in structured chunks
    (fields) making it an ideal backend for a data API.
    VIDEO NODE
    VIDEO ASSET

    View full-size slide

  12. Drupal’s Data & Actions
    Data
    Nodes, users, comments, views, etc.
    !
    Actions
    User login, node create, and trigger sending a
    password reset email.

    View full-size slide

  13. Server Types
    Services allows many server formats.
    !
    ‣ REST
    ‣ XML-RPC
    ‣ Soap
    ‣ AMF-PHP, and more …

    View full-size slide

  14. Request & Response Formats
    ‣ Response: JSON, XML, JSONp, YAML, etc.
    !
    ‣ Request: application/json, application/xml, multipart/
    form-data

    View full-size slide

  15. Authentication
    ‣ Session Authentication
    ‣ OAuth Authentication
    Pro Tip: Want to do API
    keys? Use 2-legged OAuth.

    View full-size slide

  16. Resources
    ‣ Provide basic CRUD support
    for data objects.
    ‣ Provide additional actions.
    ‣ Map requests to PHP code
    ‣ Likely 1st place you’ll write
    custom code.

    View full-size slide

  17. Resource Definitions
    ‣ A URI
    ‣ Access control
    ‣ Callback function(s)
    ‣ Arguments & Paramaters
    ‣ services.services.api.php

    View full-size slide

  18. Resource Documentation
    Knowing what resources are available and especially
    what arguments/parameters they accept can be tricky.
    !
    ‣ Code is the canonical source.
    ‣ http://drupal.org/projet/services_tools module helps.

    View full-size slide

  19. Services Endpoint(s)
    Basic configuration for your sites web service API.
    ‣ Base URL, eg. api/v1.
    ‣ Server type.
    ‣ Request & response formats accepted.
    ‣ Authentication type.
    ‣ Resources enabled.

    View full-size slide

  20. Views & Services
    ‣ http://drupal.org/project/services_views
    !
    ‣ Provides a resource to expose existing views.
    ‣ Create new views and expose them as a resource.

    View full-size slide

  21. Testing Your API
    Request:
    GET : http://example.com/resource/{ID}
    Accept: application/json
    !
    Response:
    HTTP 200 Ok
    Content-type: application/json
    {“message”: “Hello World!”}

    View full-size slide

  22. Services Tools : https://drupal.org/project/services_tools
    What to
    Test?

    View full-size slide

  23. cURL
    ‣ It’s not pretty but it is ubiquitous.
    ‣ http://curl.haxx.se/
    curl --data '{"title":"hello world!","type":"page"}' --header
    "Content-Type:application/json" http://localhost/demos/
    services-7x/docroot/api/v1/node!

    View full-size slide

  24. Chrome REST
    Console Plugin
    http://lb.cm/rest-console

    View full-size slide

  25. The Future?
    Drupal 8 has a built in REST server!
    !
    Try it now with REST WS in contrib.

    View full-size slide

  26. Review
    ‣ Web services allow access to your applications data
    and actions.
    ‣ Services module for Drupal 7:
    • Easy, extensible, and flexible

    View full-size slide

  27. Thanks!
    @eojthebrave
    !
    http://lb.cm/acquia-servicesmodule

    View full-size slide