How to debug apis in PHP (without using var_dump();die();) - php

So I'm writing an api in php, and I'd like to use a full-service debugging tool. i.e., set breakpoints, view stack traces, inspect variables, etc. This kind of thing is very common in compiled languages.
I've checked out xdebug, and after configuring it to work with phpstorm (my IDE), I was disappointed to find that it only works when I run from within phpstorm, not when I actually service real api requests.
To add additional complexity to this, the api dbs are actually hosted on a vagrant instance, so although I write and edit code on my local machine, the code being run is in a virtual machine vagrant environment.
Any other way of doing this? Or should I just get used to something along the lines of print_r();exit; and rerun the request?

Use XDebug, and configure it properly. You can configure it to work without running the code from PhpStorm quite easily.
xdebug.remote_enable = on
xdebug.remote_connect_back = on
xdebug.idekey = "vagrant"
Make sure those are set. Then, in PhpStorm, there is something that looks like a phone icon in the top right (along with the rest of the debugging and running stuff in the toolbar). Make sure that is all green (i.e. listening for connections).
You can use Xdebug to set breakpoints. It'll even work with CLI apps if you've set it up properly. You can even debug code from inside Vagrant VMs, or on remote servers.
You just need to do some research into how to set up your IDE. ;)
More details: http://www.sitepoint.com/install-xdebug-phpstorm-vagrant/
This may come in handy for you: https://www.jetbrains.com/phpstorm/marklets/
And this: https://www.jetbrains.com/phpstorm/help/configuring-xdebug.html
And this: https://confluence.jetbrains.com/display/PhpStorm/Zero-configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm

Php xdebug
Take a look this example: http://www.sitepoint.com/install-xdebug-phpstorm-vagrant/

best tool available for php debugging is Xdebug. http://xdebug.org/ here is homepage.

Related

Debug CLI PHP script in PhpStorm, cannot establish connection to Xdebug 2.6.0

I'm trying to run a CLI script in PhpStorm and use the debugger to step through it.
It took some doing, but I now have Xdebug installed and the PhpStorm CLI Interpreters dialog confirms that:
I created a Run/Debug config and when I try to execute/debug it, the script runs all the way through without breaking at my breakpoints and I get this error at the bottom of PhpStorm:
The PhpStorm docs say that xdebug for CLI scripts can configured with an environment variable like this:
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"
I've confirmed that PhpStorm sees that environment variable:
Here is my Run/Debug config:
I'm at a complete loss here. How the heck can I get this thing to debug a CLI php script? Any help is appreciated.
Gah, I hate it when I spend a couple of hours trying to fix something and discover it's totally unrelated.
The problem was that I'm using short tags and my cli php.ini file had short_open_tag=Off. The code I'm running is normally run in a web context and the php.ini for that web context had short_open_tag=On so the code was working.
This code is an api endpoint and I needed to be able to step through and see what was going on and there's nothing that inherently requires it to run in the web context (except for the input params, but I'm faking those for testing purposes anyway).
So, my debug configuration was fine.
I discovered it by trying to reduce the problem further and further and I just tried running the code directly from a command line (php sync.php) to see that it was at least working and producing the correct output.
It wasn't producing the normal api json output, it was just echoing all of the code in the file. That's what led me down the path to realizing my stupid mistake.

Why can't I step through PHPunit tests with XDebug when I run them from the command line?

PHP, XDebug, and my IDE (PhpStorm) are all configured properly. PhpStorm is listening, and it's picking up the debug session, but it never hits the breakpoint in my unit test class and I'm unable to step through it. The entire test runs.
Anyone have any ideas?
I can step into PHPUnit tests without a problem using XDebug. However, I did notice that it sometimes seems to skip certain statements (such as simple assignment to variables in a function without anything else), so spread some breakpoints around.
Make sure you've set up path mapping properly, with the correct version of the library mapped over.
If you're using XDebug from the command-line, you have to set certain environment variables - see here and here.
In my case, the following environment variables needed to be set in the remote box and run from the remote box. 10.0.2.2 works OK for Virtualbox VMs - basically you need to point to your local machine.
export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=10.0.2.2 remote_port=9000"
export PHP_IDE_CONFIG="serverName=[yourservernamehere]"

How to set up remote debugging with phpstorm and Xdebug

I've seen other questions/answer about this topic but none of them seem to have the same issue I have, so here we go:
What I'm trying
I'm using phpStorm 8 to develop PHP websites (CakePHP 2.5.1 in this specific case). I have a copy of the website on my computer, make whatever changes there and upload the new version to the production server via the integrated FTP tool. So far all pretty simple, no issues at all.
Now I would like to start using Xdebug to debug the websites using the production server (PHP 5.3.28), so I'm trying to set up remote debugging with phpStorm and Xdebug.
What I have done so far
I have installed Xdebug 2.1.3 on the production server, and it seems to be working. To test that I've done whats recommended in this other SO question, and all those things work.
This is how the config in php.ini looks like:
zend_extension="/usr/local/src/xdebug-2.1.0/modules/xdebug.so"
xdebug.profiler_enable='0'
xdebug.profiler_enable_trigger='1'
xdebug.profiler_output_dir='/home/username/debug'
xdebug.remote_enable='1'
xdebug.remote_connect_back ='1'
I'm not setting the remote_port variable because I'm fine with the default port (9000). Also, I'm not setting the remote_host IP because I'm using the remote_connect_back option to allow multiple IPs, as explained here.
I've also tried 2 different approaches to set all this up:
I followed this Zero Configuration tutorial, but at step 7 I never get the Incoming Connection dialog.
I also followed this different tutorial but in the Integrating XDebug with PhpStorm step I don't have the choose XDebug from the Debugger drop-down list option on step 3
What I need
If someone could help me figure out what I'm missing or doing wrong that would be great!
I would have added this to the comments, but don't have the needed rep.
Have you set the preferences correctly in your project? Were you able to configure and validate your deployment server (under Deployment)?
After that, set up the server under PHP > Server and validate it as well.
Don't forget to check the firewall on your host.
Make sure that you can get XDebug working without PHPStorm, then circle back around and integrate it.
These are the php.ini settings, other than the driver path, that I am using for my CLI project:
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_autostart = 1
xdebug.remote_host = 192.168.100.1
Most importantly, listen to LazyOne. Specify your remote host. And don't run debuggers on your production gear. Spend some time learning about Virtual Machines. My recommendation is to check out VirtualBox, Vagrant, and SaltStack. Used together, these tools will allow you to debug your code in an environment that is as close to production as possible without adding the burdens and risks involved with your debugging tools.

Using netbeans, xdebug, symfony and phpunit together

I installed xdebug on my Apache and if I define a breakpoint in Netbeans, the execution breaks fine. But if I execute the tests with symfony phpunit:test-all, the execution will not break on the given break point.
Any ideas?
cowabunga!
To debug a command-line script, export the XDEBUG_CONFIG variable first, like so:
export XDEBUG_CONFIG="idekey=netbeans-xdebug" (Unix/Linux/OS X)
set XDEBUG_CONFIG=idekey=netbeans-xdebug (Windows)
(Note: I did not test the Windows command. You may have to escape the = character, or the command may look a bit different. If that's the case, I hope someone comes along and corrects me in a comment.)
Explanation: When you open a debugging session for a script that runs through Apache, NetBeans will open your browser to a URL that ends with "XDEBUG_SESSION_START=netbeans-xdebug". When this happens, Xdebug knows to intercept this argument and give your browser a cookie with this debug session information.
The command above is the equivalent for setting the cookie in the command line environment.
When you say you define a breakpoint in Netbeans and it breaks fine, are you talking about running the unit tests from within Netbeans, or running a web application that triggers the breakpoints in Netbeans?
Either way, my first guess would be that the two scenarios are using different PHP.INI files. Get one of your unit tests to dump configuration information and you'll see pretty quickly I daresay. Find the PHP.INI that's being used on the command line and make sure that XDebug is set up for that scenario.
I hope that makes sense. I'm entirely coffee-free at the moment.
I don't use netbeans but I don't think netbeans modifies the file you put break points in and I don't think that symfony can read your netbean configuration to find out where to break.
Also if you are running the test all task inside netbeans and expect the test files to break I don't think that will work either as the task forks php processes I believe and these processes won't be readable by netbeans.
All hypothesis of course.

Remote Debugging PHP Command Line Scripts with Zend?

I'm using Zend Studio to do remote debugging of my php scripts on a dev server. It works great for web code, but can I make it work with command line scripts?
I have several helper apps to make my application run. It would be really useful to fire up the remote debugger through command line instead of a web browser so I can test these out.
I assume it's possible, since I think Zend is using xdebug to talk to Eclipse. Apparently, it adds some parameters to the request to wake the Zend code up on a request. I'm guessing I'd need to tap into that?
UPDATE
I ended up using xdebug with protoeditor over X to do my debugging.
I was able to get remote CLI debugging working in Eclipse, using xdebug, though I've not tried it with the zend debugger. I would assume this should work the same with ZSfE, if that's the "Zend Studio" you're using.
Since this is more along the lines of product support, your best bet is probably emailing the support people. We bought Zend Studio at my last job and they were always able to help us in a matter of hours.
Feel free to post the answer though, I am sure there are more people looking for it. :)
There's an option to debug a php script,
run->run as->php script
I believe it also has to be in your project root though. Just for clarification, Zend studio uses their own debugger, while the eclipse pdt project you have the option for Xdebug or Zend's debugger.
Haven't tried, but you can set the QUERY_STRING environment variable to the one that toggles the Zend debugger on.
Per this article.
export QUERY_STRING=start_debug=1&debug_host=<host name or IP of the local machine>&debug_port=<the port that is configured in your ZDE settings>&debug_stop=1
And then run the CLI script.
Remote command-line debugging is possible, I just tried it.
In my case I used Zend Studio + Zend Debugger.
This official article here by the Zend people will help you out, it's what I used. It explains all the parameters that must go into the shell command.
Make sure that you have the php.ini properly set on the remote server, and that it allows your IP address and it will work.
Also, you don't need to export the QUERY_STRING variable.
You can just do:
QUERY_STRING="start_debug=1&debug_host=[127.0.0.1]&no_remote=0&debug_port=10137&debug_stop=0" /path/to/php/binary /your/php/script.php
Running that on an SSH shell will light up your Zend Studio. Sweet!

Categories