7

I have the following simplified API:

http://localhost/app/apiA
http://localhost/app/apiB

where apiA does some processing and then performs these simple operations so that apiA calls apiB:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, site_url('apiB'));
//various other options
$response = curl_exec($curl);

Now I put a breakpoint in PhpStorm right on the curl_exec call in apiA and another right on the first line of the apiB method. What happens is that, first of all, XDebug is stuck on the curl_exec call and will remain there indefinitely; however if I press Break, stopping the interpreter, XDebug breaks but activates my breakpoint in apiB!

I'd like it to perform the call to curl_exec and hit the breakpoint in apiB and then come back to the first breakpoint after it's completed. Is there any way to configure XDebug and/or PhpStorm to do this?

SolarBear
  • 4,534
  • 4
  • 37
  • 53

2 Answers2

16

This is very simple to do in PhpStorm, simply go to Settings -> PHP -> Debug and in the External Connections section increase the value of the Max simultaneous connections to the number you require - in my case, 2 was enough.

Screenshot of settings

SolarBear
  • 4,534
  • 4
  • 37
  • 53
7

I finally got it to work reliably. So I will revise my answer. I realize that my answer is essentially the same as I have seen in other places, but somehow I didn't understand them, so I will try to make it more clear.

This is tested on phpstorm 9.5 and 10 but most likely work the same on earlier versions. I use Linux, (Kubuntu 14.04). (The assumption is that xdebug is already working normally within phpstorm.)

The setup is that I initiate a session from curl on the command line, the request is then handled by a route (routeA) in my application which sends a new request to another route (routeB) with cURL (ie. curl_exec()). The result is then returned to routeA and finally returned to the command line.

The problem: to have full debugging in phpstorm/xdebug through out the request/response cycle.

In order for this to work the following settings in xdebug are needed. xdebug must be set up to handle remote debugging session and there must be an idekey that can be used to trigger the debug session - I didn't find other settings that seemed relevant. Restart your server after changes.

xdebug.remote_enable=1
xdebug.idekey=PHPSTORM

Secondly, in phpstorm, you need to allow for multiple external simultaneous connection. I believe that the cURL call (from one route to another) is seen as external, in the sense that the first connection remains active while waiting for the second to return, at least 2 are needed maybe more if the chain is longer. This setting can be found in settings

Language & Frameworks > php > Debug > External Connections

Finally you need to tell phpstorm to listen. You can find this option in several places, one place is in the run menu (in version 10 it is towards the bottom)

Run > Start Listening for PHP Debug Connections

Then on the command line you will use the PHPSTORM ide key to trigger debugging:

curl -i -v http://yoursite/routea/?XDEBUG_SESSION_START=PHPSTORM

In routeA you create the cURL call like this, you

public function routeaAction()
{
    ...

    //initialize the curl object
    $curl = curl_init("http://yoursite/routeb/");
    // trigger the second debug connection by setting a special cookie
    curl_setopt($curl, CURLOPT_COOKIE,"XDEBUG_SESSION=PHPSTORM");

    // debugging options
    // short timeout, stop on errors, show headers, be verbose etc.
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_FAILONERROR,true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLINFO_HEADER_OUT,true);
    curl_setopt($curl, CURLOPT_TIMEOUT,2);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);

    // follow along to the second route and the return the result
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    // if your site is a virtual host - typical on your local dev machine
    // you need to tell your web server which site (virtual host) you want
    // the url is not enough - you need to set the Host header
    // there may be other headers you want to set - do it like this
    $requestHeaders[] = "Host: yoursite";
    $requestHeaders[] = "Cache-Control: no-cache";
    $requestHeaders[] = "Pragma: no-cache";
    curl_setopt($curl, CURLOPT_HTTPHEADER,$requestHeaders);

    // finally you're ready to send the request
    $data = curl_exec($curl);
    $errno = curl_errno($curl)
    if ($errno){
        $data .= 'Error: ' . curl_error($curl) ."\n";
    }
    curl_close($curl);

    return $data;
}

I will leave the links because they're helpful but remove my quotes from them because they're not essential.

This one gives a good example on how to set options for cURL:

https://stackoverflow.com/users/3813605/misunderstood

this link explains some xdebug.ini settings under windows

Curl with XDebug on NetBeans

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug-2.2.5-5.4-vc9.dll"
xdebug.profiler_append = 0
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 0 
xdebug.profiler_output_dir = "C:\xampp\tmp"
xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_autostart = 1
xdebug.remote_host = "127.0.0.1"
xdebug.remote_port = "9000"
xdebug.trace_output_dir = "C:\xampp\tmp"
xdebug.max_nesting_level = 200
xdebug.idekey = "netbeans-xdebug"

This link has some info specific to netbeans but may also apply to phpstorm

Launch XDebug in Netbeans on an external request

go to project properties > run configuration > advanced > debug url and check do not open web browser (*). do not set the host under debugger proxy. save these settings. in the project window, on your project: right mouse click > debug (this starts listening for debug connections). no browser is started. enter http://www.mywebsite.com?XDEBUG_SESSION_START=netbeans-xdebug in your browser. it should break in netbeans. at least that's what happens here :)

FileNotFoundException with 404 status for valid URL on HTTP GET request

404 not found in curl

Community
  • 1
  • 1
fsando
  • 71
  • 3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Reeno Oct 21 '15 at 14:20
  • I updated my answer, not sure if it's ok to copy so much from others' answers but now it's done – fsando Oct 21 '15 at 20:01
  • Agree, but there doesn't seem to be an answer yet. I've looked - I believe - at every single mentioning of this issue. None with actual solutions, one ending with OP saying "disregard, figured it out". I think there are several different issues involved related to both curl, xdebug and phpstorm. Some of them are a question of getting the various configs right - this is where the posted links will help. They helped me narrow it down to either a bug in phpstorm/xdebug/curl or some logical fallacy in how I (and many others) try to make curl connection from/to the same server. – fsando Oct 22 '15 at 19:10
  • [@Alex Tartan](http://stackoverflow.com/users/1181435/alex-tartan), I have updated my answer. – fsando Oct 28 '15 at 10:32