Home » Php » eclipse – Using XDebug to trace a PHP web service page

eclipse – Using XDebug to trace a PHP web service page

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m using Eclipse and XDebug to develop a PHP application that relies on web services.
I have test pages that consume my services in 2 ways: AJAX (using jQuery) and cURL.

I add breakpoints to my service page and launch the debugger. When I call the the service from AJAX, execution stops nicely at the breakpoint, and I get my variables, step-by-step control etc.

But when I call the service using cURL (i.e. from within a PHP page), the breakpoints fail to function. Even if I turn on the “Break at first line” debugger option, I cannot get the execution to stop when using cURL.

Is it a debugger behavior? Do I need to add a hearder to my cURL calls? Alter the URL? Or is it an XDebug limitation?

Thanks for your time and effort,
Guy

How to&Answers:

I can’t comment yet, so I post this as an answer.

Can you debug more than one AJAX request in one session?
Was your debug session still running in Eclipse when you tried to debug using cURL?

Description on how it works for me:

  1. Start debug session with a simple debug.php file that contains only a <?php and nothing else. It stops on the first line, you “continue” it and it finishes execution.
  2. Now request the script using cURL (or another browser) adding ?XDEBUG_SESSION_START=ECLIPSE_DBGP to its path (I even think this addition is optional)
  3. Your script should show up in the debug view stopped at the first line

Hope ths helps.

Answer:

Here is tip on how to trigger Xdebugger client from Curl without browser:

1- From command line:

curl -H "Cookie: XDEBUG_SESSION=1" http://YOUR-SITE.com/your-script.php

2- From PHP

<?php 
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, 'http://YOUR-SITE.com/your-script.php');
curl_setopt ($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1');
curl_exec ($ch);
?>

So it doesn’t matter if you attach “XDEBUG_SESSION=1” to CURL URL, but what is necessary is to send a proper cookie together with request.

Answer:

I know this is a pretty old thread, but I thought I’d post my experience for others that may come across it, like I did, with the same problem. What I discovered is that, if you are debugging remotely (which I always do), there are a couple settings you have to change in php.ini to make this work. Here are the ones that worked for me:

xdebug.remote_connect_back = false
xdebug.remote_host = {client host name or IP}

The first setting is normally “true,” and tells xdebug to look for the client at the same IP address where the HTTP request originated. In this case however, the request is coming from the server, so that won’t work. Instead you must use the second setting to tell xdebug where to find the client. Hope this helps save somebody a little time!

Answer:

To trigger the debugger the simplest solution is to use the cookie approach -b XDEBUG_SESSION=ECLIPSE_DBGP worked for me on eclipse, see below:

curl  -H 'Content-type: application/json' \
      -b XDEBUG_SESSION="ECLIPSE_DBGP" \
      -X POST \
      -d '{"uid":200, "message":"asdsad","message_type":1}' 
      http://daxuebao.local:8083/api/message/send

Answer:

When you are debugging the Ajax request, that one is sent by the browser, in the same navigation context as the other (non-Ajax) requests — which is why it works fine.

The request sent by curl is in another, different, context — and I’m not sure you can hook the debugger into that… But, maybe…

First of all, here’s an information that might prove helpful, quoting the Xdebug’s documentation :

Xdebug contains functionality to keep
track of a debug session when started
through a browser: cookies. This works
like this:

  • When the URL variable XDEBUG_SESSION_START=name is
    appended to an URL Xdebug emits a
    cookie with the name
    XDEBUG_SESSION” and as value the
    value of the XDEBUG_SESSION_START
    URL parameter.
  • When there is a GET (or POST) variable XDEBUG_SESSION_START or the
    XDEBUG_SESSION cookie is set, Xdebug
    will try to connect to a debugclient.
  • To stop a debug session (and to destroy the cookie) simply add the URL
    parameter XDEBUG_SESSION_STOP.
    Xdebug will then no longer try to make
    a connection to the debugclient.

Maybe it might work if you set that cookie “by hand”, sending it allong the curl request…

I suppose you’d first have to get its value, as set by Xdebug at the beginning of the debugging session — re-using the cookie you have in your browser should be possible, though.

Note : I’ve never tried this — if you try, and it works, could you please confirm it worked ?

Answer:

I ran into this same exact issue. I solved it by turning the auto-start feature off in php.ini:

xdebug.remote_autostart = 0

and then adding the API key to the webservice URL that my webservice client calls:

?XDEBUG_SESSION_START=<your API key here>

and I’m not sure if this matters, but I entered the API key into my debugger (MacGDBp). Now the debugger fires up only when the webervice server-side script is called, not when the client is started.

Hope this helps.