Zak
Zak

Reputation: 25205

Best method for debugging a called webservice in php?

I have an n-tier system where a frontend templating layer makes calls out to a backend application server. For instance, I need to retrieve some search results, and the frontend calls the backend to get the results.

Both the templating engine and the appserver are written in PHP. I currently use PHPed to initiate debug sessions to the templating engine, however, when the http request goes out to remote service my debugger just sits and waits for the IO to complete.

What I would like to do is emulate the HTTP call but really just stay inside my PHP process, do a giant push of the environment onto some kind of stack, then have my appserver environment load and process the call. After the call is done, I do an env pop, and get the results of the http call in a var (for instance, via an output buffer). I can run both services on the same server. Does anyone have any ideas or libraries that already do this?

Upvotes: 5

Views: 2383

Answers (7)

user2221743
user2221743

Reputation: 95

PhpED supports parallel debug sessions - meaning you can start debugging code that issues request to initial server and then inter-server requests too. All you need is to set breakpoints in corresponding projects and pass debugger request between the servers. Normally you can do this by re-transmitting value of DBGSESSID (the debugger request) variable with its value to the 2nd server. The variable can be found among $_COOKIES and/or $_GET (depending on how you start debugging -- from the IDE or usign Debugger Toolbar). To re-transmit the variable to the secondary server(s) you can add it to POST variables or as URL parameter or cookie. If you can't do that f.e. if your server filters out all from get/post/cookies, try to embed DebugBreak() call. Make sure that all your servers can find the IDE by its IP address in the request and allowed to connect back to the IDE -- e.g. you have necessary rules in firewall and LinuxSE (buy default this SE layer is enabled in all modern Linuxes these days). It took me a day to figure out why my server can't connect.

In case if connection from the server to the IDE is not possible (if workstation with the IDE is in a different network, for example at your home), you can use ssh tunnels. In this case the IDE address is localhost, of course.

Upvotes: 0

Evert
Evert

Reputation: 99571

This is not open source, but check out Charles. It works as a proxy, and is the best debugging proxy I've seen to date. It works on linux, os/x and windows.

Pretty much any HTTP library will allow you to specify a proxy.

Upvotes: 1

Jeffrey Knight
Jeffrey Knight

Reputation: 5975

I don't know much about PHP debugging, and I'm not sure I follow 'push of the environment onto some kind of stack', but I wonder if netcat + some shell scripting could be useful here for troubleshooting ?

You can use netcat to:

  • Spoof an HTTP Request
  • Act like a webserver (listen on a port - pick a port, any port!)

http://www.plenz.com/netcat-tips

You could use it to stub out a fake webservice on the one end:

echo "<xml .. <node>hello php!</node>" | netcat -lp 80 ... etc

... and you can certainly use it listening on a port to very clearly see what the incoming requests to the webservice look like.

Could you use a shell script with netcat as middle man that acts like your webservice, immediately returns something generic to make your PHP happy, then passes the request on to your actual appserver and logs the results?

Super simple.

netcat webserver http://img240.imageshack.us/img240/791/netcat.jpg

Upvotes: 1

AlexanderJohannesen
AlexanderJohannesen

Reputation: 2028

Can I assume you're talking about the lack of threads in PHP, so the service stops the flow of your program and halts the debugger? There's ways around it, but they are hard, cumbersome and hackish.

For example, if you use a framework like Zend for the HTTP traffic, you can hack the HTTP class to use primitive sockets for the service reading/writing instead of the built-in stuff, and create a small task switcher (loop :) to track what's going on.

You could of course use fopen ( 'http://...' ) and fread in chunks in a loop as well, that could do the trick, but you need http: support in streams turned on.

Upvotes: 1

apinstein
apinstein

Reputation: 5223

Why don't you use an HTTP sniffer? Something like tcpflow.

Alternatively, you could just log the complete XML to a file for each request & response.

Unfortunately it's not clear from your question what you're trying to achieve so these are just guesses. You should probably state more clearly exactly what problem you're trying to solve.

You could possibly re-factor your code that calls out to the remove service and use dependency injection and mocks. That would allow you to isolate the development of the front-end with the back by suppling "mocked" but valid data.

Hope that helps.

Upvotes: 1

willw
willw

Reputation: 1308

This is embarrassingly crude, and quite free of any study of how the debugger works, but have you tried adding

    debugBreak(); 

at the entry points to your called routine? (Assuming both processes running on the same machine).

I have used this technique to break back into a process called via AMFPHP. I have had a PHP file loading Flash file into browser, which then calls back to PHP using AMFPHP, all on the same server. When I hit the debugBreak() line, PhpED regains control.

Upvotes: 2

Steve Claridge
Steve Claridge

Reputation: 11080

Can you not run a debugger and set a breakpoint in the appserver too? Two different debug sessions - one to trap the templating engine call and one to trap the call in the appserver.

You should be able to trace the output from the appserver in the templating engine debugging session.

If it is not possible to run two debug sessions then create some test inputs for the appserver by capturing outputs from the templating engine and use a single debugger with your test appserver inputs.

Upvotes: 2

Related Questions