PHP Web Services, 2nd Edition

Book description

Whether you’re sharing data between two internal systems or building an API so that users can access their data, this practical guide has everything you need to build APIs with PHP. Author Lorna Jane Mitchell provides lots of hands-on code samples, real-world examples, and advice based on her extensive experience to guide you through the process—from the underlying theory to methods for making your service robust.

You’ll learn how to use this language to work with JSON, XML, and other web service technologies. This updated second edition includes new tools and features that reflect PHP updates and changes on the Web.

  • Explore HTTP, from the request/response cycle to its verbs, headers, and cookies
  • Work with and publish webhooks—user-defined HTTP callbacks
  • Determine whether JSON or XML is the best data format for your application
  • Get advice for working with RPC, SOAP, and RESTful services
  • Use several tools and techniques for debugging HTTP web services
  • Choose the service that works best for your application, and learn how to make it robust
  • Document your API—and learn how to design it to handle errors

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. Safari® Books Online
    4. How to Contact Us
    5. Acknowledgments
  2. 1. HTTP
    1. Clients and Servers
    2. Making HTTP Requests
      1. Command-Line HTTP
      2. Browser Tools
      3. Doing HTTP with PHP
  3. 2. HTTP Verbs
    1. Serving GET Requests
    2. Making GET Requests
    3. Handling POST Requests
    4. Making POST Requests
    5. Using Other HTTP Verbs
  4. 3. Headers
    1. Request and Response Headers
    2. Identify Clients with User-Agent
    3. Headers for Content Negotiation
      1. Parsing an Accept Header
      2. Demonstrating Accept Headers with cURL
    4. Securing Requests with the Authorization Header
      1. HTTP Basic Authentication
      2. HTTP Digest Authentication
      3. OAuth
    5. Caching Headers
    6. Custom Headers
  5. 4. Cookies
    1. Cookie Mechanics
    2. Reading and Writing Cookies
    3. Making Requests with Cookies
    4. Cookies and APIs
  6. 5. JSON
    1. When to Choose JSON
    2. Handling JSON with PHP
      1. The JSONSerializable Interface
    3. Consuming JSON APIs
  7. 6. XML
    1. XML in PHP
      1. Creating XML
    2. Consuming XML APIs
      1. Parsing XML
      2. Flickr’s XML API
  8. 7. RPC and SOAP Services
    1. RPC
    2. SOAP
      1. WSDL
      2. PHP SOAP Client
      3. PHP SOAP Server
      4. Generating a WSDL File from PHP
      5. PHP Client and Server with WSDL
  9. 8. REST
    1. RESTful URLs
    2. Resource Structure and Hypermedia
    3. Build the Basic RESTful Server
      1. Example Project: The Wishlist
      2. Create Resources with POST
      3. Fetch a Resource or Collection with GET
      4. Update a Resource with PUT
      5. DELETE a Resource
    4. RESTful Versus Useful
  10. 9. Webhooks
    1. GitHub’s Webhooks
    2. Publishing Your Own Webhooks
  11. 10. HTTP Tools
    1. Easy Command-Line JSON
    2. Graphical cURL Alternatives
    3. Inspect HTTP Traffic with Wireshark
    4. Tunnel Local Traffic Remotely with ngrok
    5. Inspect, Edit, Repeat, and Share Requests
    6. Proxying PHP Applications
      1. Proxy Settings for Guzzle
      2. Proxy Settings for HTTP Stream Handling
    7. Finding the Tool for the Job
  12. 11. Maintainable Web Services
    1. Sample API Application
    2. Consistent Output Formats
    3. Debug Output as a Tool
    4. Effective Logging Techniques
      1. Error Logging in PHP Applications with Monolog
    5. Error Handling with PHP Exceptions
  13. 12. Making Service Design Decisions
    1. Service Type Decisions
    2. How to Present API Data
      1. Hypermedia for Easy API Navigation
      2. Nested Data or Many Round Trips
      3. Data Formats and Media Types
    3. Customizable Experiences
    4. Pick Your Defaults
  14. 13. Building a Robust Service
    1. Consistency Is Key
      1. Consistent and Meaningful Naming
      2. Common Validation Rules
      3. Predictable Structures
    2. Error Handling in APIs
    3. Meaningful Error Messages
    4. What to Do When You See Errors
    5. Making Design Decisions for Robustness
  15. 14. Publishing Your API
    1. Documentation Is Key
      1. Overview Documentation
      2. Generated API Documentation
      3. Interactive Documentation
    2. API Description Languages
    3. Automated Testing Tools
    4. Tutorials and the Wider Ecosystem
  16. A. A Guide to Common Status Codes
  17. B. Common HTTP Headers
  18. Index

Product information

  • Title: PHP Web Services, 2nd Edition
  • Author(s): Lorna Jane Mitchell
  • Release date: January 2016
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491933046