Andrea Azzola

Tips and Techniques for Lifestyle Design

How to log HTTP requests with PHP

Posted on [Permalink]

The Problem

Sometimes you need to understand precisely how the HTTP request is submitted to the server. If you're working with Salesforce.com, you probably don't have easy or intuitive tools to understand how a message is serialized and sent by the runtime to the server.

The Solution

I've put togheter this little script that allows you to draw all HTTP requests to a file. Becomes so much easier to verify the output sent to a web service.

<?php
	$myFile = "requestslog.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	fwrite($fh, "\n\n--------------------------------------
		-------------------------\n");
	foreach($_SERVER as $h=>$v)
		if(ereg('HTTP_(.+)',$h,$hp))
			fwrite($fh, "$h = $v\n");
	fwrite($fh, "\r\n");
	fwrite($fh, file_get_contents('php://input'));
	fclose($fh);
	echo "<html><head /><body><iframe src=\"$myFile\" 
		style=\"height:100%; width:100%;\"></iframe></body></html>"
?>

This applies to all requests that travel with the HTTP protocol, even SOAP for instance.

Categories:

Comments

Thanks! Saved me some time coding my own solution up!

~Php Coder