So I am running PHP on Microsoft IIS web server. I have to open some sort of a socket to a remote host. I would like to inspect that socket because I would like to see the data being exchanged between PHP and the remote web service. In our case you could call this a server to server connection. But remember that as far as the remote web service is concerned PHP is acting as a client. Fiddler is an irreplaceable tool. I have been using it for years and I really can’t imagine my life without it.
Fiddler itself is a web debugging proxy. When it is started, it creates a proxy service (by default on port 8888). It also configures Windows and the browsers to automatically route the traffic through that proxy but only for the current user. After that you see a list of all http(s) requests that have went through the proxy and you can click on one to view all its of its parameters. As a bonus it can render XML, JSON or HTML. This is priceless.
However, if you are on a Windows Server box with PHP running on IIS. It is highly likely that IIS is running under a different user so server2server connections will not show in fiddler. Do not try to configure the IIS settings via the IIS console or web.config as that will not work as those are specific for .NET apps and do not affect PHP. Remember PHP is running in FastCGI mode.
What we need to do is we need to tell PHP to use the proxy server. I was surprised that there are no Proxy settings in php.ini. The recommended way to do this is using the stream_context_set_default() function.
$stream_default_opts = array(
'http'=>array(
'proxy'=>"tcp://127.0.0.1:8888",
'request_fulluri' => true,
)
);
stream_context_set_default($stream_default_opts);
As with most of the other PHP environment setters – this needs to be at the start of the PHP file. If you would like this to be global server setting, you can save the above snippet in a .php file on your server and then you could specify it to be prepended (i.e put in the start) of every other PHP file automatically in php.ini like this:
auto_prepend_file = "/path/to/proxySetter.php"
To make things more complicated, this method does not apply to all connections. If this doesn’t work – you will need to research how to set proxy for whatever objects you are using. In my case where I am using the SoapClient class, I have to explicitly specify the proxy host and port:
$wsdl = 'https://example.com/path/myWebService.wsdl';
$options=array(
'trace' => 1,
'exceptions' => 1,
'proxy_host'=>'127.0.0.1',
'proxy_port'=>'8888',
);
try
{
$client = new SoapClient($wsdl, $options);
}
catch (Exception $e)
{
echo 'The web service call failed with error "';
echo $e->getMessage();
echo '"';
exit();
}
If all of these fail (or you are too lazy) you could try a program called Proxifier but I can’t guarantee it will work.
Hope this will help someone out there 🙂