I've got a nice question here :)
I need to debug my web service written in PHP. Its client is written in C#.
After a couple of days of searching I realized this is not an easy task. At least it seems nobody knows the right solution.
What is the problem in, actually?
We have 2 popular PHP debugging libraries : PHP Debugger from NuSphere and XDebug extension.
The problem is they both are controlled from URL query string or with the help of cookies. For example, to enable debugging with PHP Debugger you need to add ?DBGSESSID=xxx parameter to your URL or to have DBGSESSID cookie.
But when your web service is called from the external client, the client doesn't have a cookie and doesn't add DBGSESSID url parameter. So how can we debug in this situation?
PS. I don't want to write to log files, see request and response headers/data or something like this. I want normal step-by-step debugging and breakpoints.
Anyone?
Well, I am answering to myself.
If we use PHPEd & DBG, then we can use the magic function DebugBreak().
Make sure PHPEd & PHP DBG Listenere are running, write
DebugBreak('1#127.0.0.1');
anywhere in your werbservice's code, make a call from the client, and voila! - you are in PHPEd on that line in debugging mode!
you could set xdebug.remote_autostart to 1 to always debug (no request parameter needed). this could be limited to some url with the <Location> or <Files> directive.
Or just log some debug information (using Zend_Log or Pear Log if you want a generic library) using var_export.
quick and dirty way is:
file_put_contents('/tmp/log1.txt',
var_export(array($_REQUEST, $something), true));
You could write data to a log file (meh).
Or output debugging information in response headers (if the client can view them). But as far as using breakpoints, you may be out of luck.
You could also look into connection hijacking on your local computer (something similar to the Firefox AddOn Tamper Data) where you can interrupt the request and add the url parameter.
Try SoapUI to issue requests manually and get the detailed responses. Not sure if you can fake the cookie, but you can control the endpoints, and therefore the URL to an extent.
I seem to remember that you can configure NuSphere's product to automatically attempt to connect to the debug listener with or without the DBGSESSID parameter (in query string or cookie). I'm not positive if that's the case, though. However, you can get the effect you're looking for by doing the following. It may be a little more manually intensive than you're hoping for.
Setup some sort of HTTP query/response listener.
Perform desired access against web service from client.
Manually re-issue those requests, appending the appropriate DBGSESSID
For a little more initial setup, but lower friction debugging later:
Configure your client to access an alternate URL.
Setup a proxy to listen on that URL (for debugging, I've seen Privoxy recommended, though I have no experience with it personally).
Configure the proxy to forward all requests to the real web service, appending an appropriate DBGSESSID parameter or including the cookie
I use the plugin Poster to help debug my php Webservice
Edit :
Found a better tool to debug web service : Advanced REST client Application
It's a Chrome Plugin, works great to test all kind of web services that use REST
Related
I know that there are a number of libraries available, but I am trying to learn more about the WebDav protocol itself for a project I’m developing.
For stage 1, I would like to implement a virtual read-only file system in PHP, presenting as a WebDav server.
As far as I can tell, it would need to be able to:
list virtual files & directories
change directories
print the contents of a single file
I’ve found a number of sources, but they either try to do too much or gloss over the implementation of the protocol itself.
Can someone explain or point me to a source that might answer the following:
What are the steps in the communication between the client & server?
How does PHP receive a request, and how should the response be formatted?
Thanks
When I originally started sabre/dav I still made sure to read the entire rfc first. You really need to have a good idea of all the features, the data model and how they work together.
After that, you probably only really need to look at the PROPFIND, OPTIONS and GET methods. One option is to just look at what a client sends your way... figure out based on the rfcs what the response should be, and then write the code that sends the correct response.
Another good way to start learning is to hook up an existing webdav client to a webdav server and inspect what kind of messages they send back and forward.
I have an Android application that is connectiont to a web server running MySQL DB. On the web server I have php files that run scripts and return JSON Objects to the client (application). I recently upgraded the versions of my web server and as of then, the SELECT statements are no longer returning anything to the client if they have parameteres, however if I run a SELECT with no parameters, it works fine. Has anyone ever encountered a similar problem?
To be more clear about my question:
Is there anyway I can debugphp scripts?
I thought maybe debugging via a web browser wouldn't work because the client is triggering the php files directly and not a direct access to the web.
All help is much appreciated!
Chrome has a set of tools that let you remote debug from your android.
Check them out: https://developers.google.com/chrome-developer-tools/docs/remote-debugging
You could wirte your own error handler an let him wirting some debug informations to a own log-file. than you can read the log file and check it for errors
Look at the MySQL error log and the php error log on your server
Check you activated the correct php_mysql* extensions in the newly upgraded PHP
Run the scripts on the server through a browser with the same parameters that your app uses, watch for errors
Upgrade the PHP code running on the server so it captures any errors and does something OBVIOUS with them.
I would recommend to use XDEBUG for debugging PHP code, or any analogue for yours web server. It is really simple to set-up and use it with any PHP IDE (like Netbeans) and it gives you a lot of abilities to explore your application condition. There is no difference in what browser you will open link with ?XDEBUG_SESSION_START=netbeans-xdebug parameter.
Also you can watch MySQL and Web-server logs.
If you cannot do anything above and you need to watch network action (AJAX requests), you can set-up ADB and use chrome developer console for debugging js (or to see php var_dump() in ajax calls) in you android chrome browser.
If you need to debug js in native android browser - try to redirect to "about:debug" page, and after this you will be able to see browsers js console (in some cases this button appears only when console has something to show - error or any othe message, in some cases it just doesn't work).
Update Watch Duplicate Copy Move
As mentioned by others, you can debug using the error logs given by mysql and php. There are also IDEs which come with debugging tools for scripting languages like php.
With an IDE like phpStorm by JetBrains, which I highly recommend using over just a normal text editor, you can configure data sources like a connection to your MySQL db. This is useful because you can run scripts in the IDE and see whether it's the retreival of the data from your db or something in your scripts or handling of the JSON object back in the client. So if you configure a data source and run those same scripts and you get the correct data back then you know there is either something wrong in the code where you send the data or something wrong in the code where you receive it.
**p.s. I know that I am 5 years (oh my days its been 5 years since 2013) late to this post but none the less you never know who may be having this same issue and they stumble accross this answer
1st - Setup Xdebug for remote debugging with your IDE(Eclipse, PHPStrom, etc), and put breakpoints in your script.(for example here's link for setting up eclipse envirompment).
2-nd - When sending your url request to server add parametres to your url(http://your/url.php&XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=XXXXX,
-1st parameter - XDEBUG_SESSION_START=ECLIPSE_DBGP starts debugging session. (as for me I am using eclipse IDE for developing, so my key will be ECLIPSE_DBGP).
-2nd parameter - KEY=XXXXXX - is a session number(type any number here).
After that you'll get your code breaks at your debugging point in your script.
Disclaimer: May be a insane question but I have suffered a lot so came here.
I am working on a legacy application which uses JS + PHP + Web services (Written in spring).
Flow of the application :
Whenever any web service is called from JS it is redirected to one php file. The php file authenticates the user(using one web service) and then forwards the request to actual web service.
How can I debug this application ? I have debugged JS using Firebug and servr side code using Eclipse but never debugged such a application.
~Ajinkya.
I think there are a variety of things that need to be done, and I must say this question is sufficiently general as to not have a straight answer so I will do my best. As xdazz mentioned, var_dump (and die) are necessary from the PHP standpoint.
Whenever anything is returned to JS console.log it. In addition, ensure XHTTP requests are turned on for Firebug or alternatively view the output of each request in the Chrome Network tab.
With a combination of console.log, var_dump, and die, you can trace non-functioning parts of the application repeatedly step by step until you come across the bug.
Alternatively, and in the long run you ought to be doing this anyway, build error handling code into all the PHP code that is only activated when a debug flag is set to true. This way you can get detailed error messages and then when you deploy, you can turn them off to avoid compromising security.
If you are needing to inspect the entire lifecycle of a Web service request in your scenario you will need to combine a several techniques. Considering the fact that the scope of your scenario spans from client to server you will need to decide with what you will persist the information you need to inspect.
Personally, I would choose the path of least resistance which in my case would probably be cookies. With that being said you should be able chronologically log the necessary information via JavaScript and PHP, both before, during and after the request and even redirect has occurred.
This strategy would then allow for the information logged with cookies to then be dumped or analyzed via JavaScript, WebKit inspector or Firebug. Again, this is probably how I would handle such a scenario. Lastly, you can apply different storage strategies to this technique such as using a session or database for persistence.
Note: You can use something like WebKit Inspector, and possibly Firebug, to analyze data transmitted and received for GET, POST and even WebSocket requests.
I am developing a web application.
I would like to extend my error messages (and their backtraces) so that I can click on the mentioned file, and have it opened automatically in my PHP IDE.
I would like to make this a easy-to-activate feature so that whoever works on the web application, can easily map the error message to point to their local copy of the site, and open their IDE.
What - short of developing a custom FF extension - is the simplest way you can think of to execute a local command (a batch file that calls the IDE) on click in Firefox on Windows(7)?
I have looked for extensions but had no luck. Maybe using another extension like Firebug or Greasemonkey?
Security is not an issue, as this is supposed to work on the developer's workstation only and I can change my local Firefox's settings.
You can add a new protocol (like "edit://") to windows (http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx) and write a small handler program that picks a filename from the "edit://" url and passes that file to the editor. This way i taught windows to understand txmt links (http://blog.macromates.com/2007/the-textmate-url-scheme/) in the way my mac does.
There's not a way to do this with javascript. But it looks possible with a firefox addon. Have a look at this.
http://mozex.mozdev.org/
MozEX is an extension which allows the user to use external programs for these actions:
* edit content of textareas (possibly utilizing a spell-checker, color syntax etc.)
* view page source
* handle mailto, news, telnet and FTP links
* download files
* ... and many more :)
The universal handler lets you enter a list of protocol schemes, e.g., "abc://,def://" and a
command to handle them. So you just have your application generate a url that begins with
your chosen (made up) protocol, and mozex will intercept a click on the url and send the
url to your chosen command as a paramater.
I think this is exactly what you want.
I think that the closest you can get to this, is by having the configuration of the web browser associate a particular mime type with a given "helper application" (here the IDE program), and to have the HTTP server return such a file.
Otherwise, security concerns dictate that browser would not run any "abritrary" program/logic on the client.
Pekka,
After reading the thread so far, it seems that you want to build an application that somehow authenticates with the server--i.e.: the "average user" wouldn't have access to it. If this were the case, then delivering it through the browser is an impossibility without writing a custom extension.
Running authentication through GreaseMonkey is difficult, but once the client is authenticated, there is no real way to "run" the trace.
If the server generates a batch file or some kind of instruction set (script, shortcut, etc.), you can simply configure the browser (or have the local instance of your app configure the browser) to run the file. The problem here is that you have no way to automatically authenticate!
The only other way I can imagine that you could get this to work is via a Java applet, which would only be cumbersome and require Java to initialize every time you wanted to import a trace.
The problem you have is that the browser is inherently secure. It's designed to protect the computer from malware, rogue websites, etc etc., and so without developing a custom extension for the browser, there's no way to make the hop to any applications that run in tandem with the browser.
So on that note, I'll suggest that you reconsider writing a Firefox XUL extension as mentioned above. You'll probably need to implement some XPCOM code to make it work, too. Here are some resources that will help get you started:
https://developer.mozilla.org/en/xpcom
https://developer.mozilla.org/En/XUL
http://ted.mielczarek.org/code/mozilla/extensiondev/
https://developer.mozilla.org/en/XUL_Tutorial/Introduction
I don't know which IDE you're using, but in for example Eclipse you can also use the built-in webbrowser to test your webapp and the exceptions/traces in the Eclipse console log already have links to the source code in question. Easy as that. See if your IDE provides something similar.
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?
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.
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.
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.
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.
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
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.
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.