Debugging Multiple PHP Sites/Projects

Author by Miloslav Beno
7 minutes
Debugging Multiple PHP Sites/Projects

When you develop applications that need 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 to when run it on a 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 with 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), select Startup Project tab, choose Multiple startup projects and select Action for both projects to Start.

    Solution properties

  4. The second step makes sure that when we press F5 both PHP web Projects will start. But that would mean two browsers would be launched. 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. Since in PHPWebProject is addressing http://localhost:38190/, we have to make sure PHPWebService is listening on this particular port. In the PHPWebService project properties, open Server tab and change the port to 38190.

    Set the port manually

  6. 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.

    Update 2/24/2022: In the latest PHP Tools version if you are using PHP Built-in server, there is no need to do anything, this should work by itself.

    We can do that by adding this line xdebug.start_with_request = yes (in previous version of Xdebug it was 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 one of them could be written in another language, e.g. C#. It would work exactly the same.

If you have any questions, please, add them in the comments below.

Related articles

Introduction to PHP on Docker with Visual Studio Code

Commenting your PHP Code