Debugging Multiple PHP Sites/Projects


When you develop applications that needs to communicate with each other you might need to debug two or more projects at once. You can achieve this very easily with PHP Tools for VS.

Let's say we have a PHP web site, which will be sending requests (e.g. AJAX, SOAP or anything else) to the web service. The web site will be handling the responses and do some work with it. For this scenario we will assume both are running locally (it would be similar when run on remote sever).

Download Sample Project

Setting it up

I'm going to assume you have PHP Tools for Visual Studio installed with PHP and Xdebug properly configured. For more information, see Configuring Xdebug

I'll demonstrate this scenario on a simple example.

  1. We will create one solution with two PHP Web Projects (*.phpproj). One for the web site, let's call it just simply PHPWebProject and the second PHPWebService.

    Solution with two PHP projects

  2. I will use a simple code:

    PHPWebProject | index.php :

    <?php
    
    $ch = curl_init("http://localhost:38190/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    
    echo $response;
    
    ?>

    PHPWebService | index.php :

    <?php
    
    echo "Hello World from WebService";
    
    ?>
  3. We want both projects to start when F5 is pressed. In the solution Properties (right click on Solution node in the Solution Explorer and choose Properties). The select Startup Project tab, choose Multiple startup projects and select Action for both projects to Start.

    Solution properties

  4. The step 2. Makes sure than when we press F5 both PHP web Projects will start. But that would mean two browsers would be lunched. That's obviously something we don't want for PHPWebService. Let's open PHPWebService properties (right on PHPWebService node in the Solution Explorer, choose Properties). As Start Action select Don't open a page. Wait for a request from and external application.

    Start Action

  5. PHP Tools would usually initiate debug session by opening your project in browser with ?XDEBUG_SESSION_START=1 query string. But PHPWebService will not open any browser on F5. So we need to instruct Xdebug to start debugging (and connect back to Visual Studio) with other means. We can do that by adding this line xdebug.remote_autostart = 1 to php.ini file in Xdebug configuration section which will force it to start on every request.

    There are more options available, but this one is the easiest one to use.

Demonstration

  1. Put a breakpoint at the first line of code at both index.php at PHPWebProject and PHPWebService. Press F5. It will break in the PHPWebProject at the breakpoint location.

  2. By pressing F10 you can step until the line with curl_exec.

    Debugging PHPWebProject

  3. Another F10 will break at PHPWebService

    Debugging PHPWebService

  4. Press F10 twice and you'll get back to PHPWebProject

    Debugging PHPWebProject

Summary

This simple scenario demonstrates how you can debug multiple projects. Both projects can be in PHP, or some of them could be written in another language, e.g. C#. It would work exactly the same.