Can I debug api on same server

I am trying to debug our app that uses a call to an api located on the same server.

The function uses curl in the code below to execute an api located on the same server. The url variable for this function is: news.hubsdev.com/administrator/index.php?option=com_api&task=acymailing.listcreate 

I can use stops and step through everything before and after it calls the api, but not while it is inside the file that contains the listcreate function.  Is there a way to get PhpStorm to step through that function?

thanks

5 comments

Hi there,

Sure.

In general:

  1. You will need to pass "debug me" flag with such sub-request (standard Xdebug way: be it via GET/POST parameter or COOKIE). That's unless you have configured xdebug to attempt to debug every single request automatically...
  2. You will need to increase the number of simultaneous debug connections in PhpStorm settings: by default it's just 1; make it 2 or 3. This will allow to debug sub-requests (to allow another active debug connection while 1st (main one) is still there waiting).
  3. Obviously, you should be able to debug the API code itself in first place (be it in a separate project or the same one).

If all done correctly and the code is in the same project then you should see separate debug tab for subrequest. If API code is in separate project then it should be triggered in that IDE window.

1

Thanks so much for this, it is very helpful.

If I have to modify the code, and add the "$debuggingQuerystring ''if (isset($_GET['XDEBUG_SESSION_START']))......" I can do that but would prefer not to.  Part of the page you sent has this:

"If we want Xdebug or Zend Debugger to start debugging for the called script as well, we'll have to either configure the debugger to start debugging all the time (xdebug.remote_autostart=1 for Xdebug), or pass the request parameters along."

Does that refer to what you said above as far as " unless you have configured xdebug to attempt to debug every single request automatically."?  Is there a downside to doing this?  I am assuming it will still be restricted by the number of sessions I set in PhpStorm.

Thanks again.

0

>Does that refer to what you said above as far as " unless you have configured xdebug to attempt to debug every single request automatically."?

Yes. That's the option.

 

> Is there a downside to doing this?

It will attempt to debug every single request/script. So there will be 0.5-1sec or so delay (depends on Xdebug version) while Xdebug tries to establish connection with IDE if you forget to turn that off. This will also apply to any CLI commands that will use the same PHP interpreter/php config (e.g. your "php artisan" commands from Laravel (or equivalents from other frameworks) or cron tasks or PHPUnit etc) -- it greatly depends on your setup.

If you have "phone handle" icon enabled in IDE ... then IDE will see the incoming debug requests and may notify you with "Debug session ended without breaking ..." kind of notices.

 

>I am assuming it will still be restricted by the number of sessions I set in PhpStorm.

Well... PHP may behave like it's "hanged" while Xdebug waits for a free debug slot (so you would need to end some existing debug session first for new one to kick in). That's if you go over the simultaneous sessions limit... which should not be happening if you do it properly.

 

>If I have to modify the code, and add ...

It depends on how you have initiated the debug session. If it was a cookie (which is what bookmarklet and xdebug browser extensions use) then your suggested code will not be able to see it (as it only checks one possible parameter)...

Having auto-debug enabled might be the best option here (just remember to turn it off when not needed).

Other possible option is to use programmatic breakpoint by adding "xdebug_break();" in the actual code where needed. It should stop even if execution was started without the establishing the debug session.

 

BTW: It's rather rare situation when you need to debug both main request and subrequest in one go (unless they are tightly related in real time). For me, having subrequest means that subrequest may work independently (think of micorservice / 3rd party API) .. and you can test them separately by using Postman or alike (to initiate the request).

0

Thanks again.

I am new to PhpStorm - so I don't quite understand exactly how it works.  If it is rare as you say, maybe I need to have another project that starts with the file containing the api?  Since I am currently starting it through the browser with the listener on, how will it know to start debugging?  

In the meantime, I think it is best to try the xdebug.remote_autostart=1.  It is not that often that I will need to test the api, so I can turn it on and off as needed.

0

Please sign in to leave a comment.