Debugging HTTP
Now that weve seen one type of service, it seems like a good time to look at some
tools and strategies for working with HTTP, and troubleshooting web services if we
need to.
Using Logging to Gather Information
Its common practice to debug a web application by adding some echo and print_r
statements into the code, and observing the output. This becomes trickier when we
work with web services because were serving prescriptive data formats that will
become invalid if we add unexpected output into them. To diagnose issues when
we serve APIs, its better to log errors, using a process along these lines:
1. Add error_log() entries (or framework-specific error logging, as appropriate)
into your server code.
2. Make a call to the web service, either from PHP or simply using cURL.
3. Check the log files to view the debugging output you added.
Tailing Log Files
Its rather tedious to keep repeating the above process, but it can be made easier
if you tail the log file. This means leaving the file open and viewed, so that all
new entries to the file appear on screen. On a Unix-based system, you can achieve
this with the command: tail -f <logfile>.
Using this technique, you can check variables and monitor progress of your web
server script without breaking the format of the output returned.
Inspecting HTTP Traffic
This strategy is one of our favorites; the idea is that we have a look at the request
and response messages without making any changes to the application code. There
are two main tools that are commonly used: Wireshark
8
and Charles Proxy.
9
Al-
though they work in different ways, both perform the basic function of showing us
the requests that we send and receive.
8
http://www.wireshark.org/
9
http://www.charlesproxy.com
PHP Master: Write Cutting-edge Code100

Get PHP Master now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.