PHP Web Services
by Lorna Jane Mitchell
Copyright © 2013 Lorna Jane Mitchell. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Maria Gulick and Rachel Roumeliotis
Production Editor: Marisa LaFleur
Proofreader: Marisa LaFleur
Cover Designer: Randy Comer
Interior Designer: David Futato
Illustrator: Rebecca Demarest
April 2013: First Edition
Revision History for the First Edition:
2013-04-19: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449356569 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. PHP Web Services, the image of an Alpine Accentor, and related trade dress are trademarks of
O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-35656-9
[LSI]
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1.
HTTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Clients and Servers 3
Making HTTP Requests 4
Curl 4
Browser Tools 6
PHP 8
2.
HTTP Verbs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Making GET Requests 11
Making POST Requests 13
Using Other HTTP Verbs 15
3.
Headers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Request and Response Headers 20
Common HTTP Headers 20
User-Agent 21
Headers for Content Negotiation 22
Securing Requests with the Authorization Header 26
Custom Headers 27
4.
Cookies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Cookie Mechanics 29
Working with Cookies in PHP 31
5.
JSON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
When to Choose JSON 34
Handling JSON with PHP 35
iii
www.it-ebooks.info