Skip to content

Instantly share code, notes, and snippets.

@ProcessEight
Last active January 18, 2021 13:20
Show Gist options
  • Save ProcessEight/431f824b7d632fa3307f1a1b7c698f64 to your computer and use it in GitHub Desktop.
Save ProcessEight/431f824b7d632fa3307f1a1b7c698f64 to your computer and use it in GitHub Desktop.
Example scripts showing how to debug API calls with Xdebug

Debugging with Xdebug

Debug from the command line in Xdebug

Just export the XDEBUG_CONFIG variable:

export XDEBUG_CONFIG="idekey=PHPSTORM"

Remember to enable Xdebug first:

/var/www/html/magento2-deployment/dev/enable-xdebug-php70.sh && export XDEBUG_CONFIG="idekey=PHPSTORM"

REST

Set the Xdebug cookie against the HTTP request.

There are more flexible ways to add cookies to a Guzzle HTTP request, especially if you want to send multiple cookies, however, for just one cookie, this is the easiest way.

If triggering this script through the browser, don't use the Xdebug bookmarklet.

<?php

    // This example uses the Guzzle HTTP library installed using composer, but you can of course use any library you prefer

    require_once 'vendor/autoload.php';

    $guzzleHttpClient = new \GuzzleHttp\Client([
        // You can set any number of default request options.
        'timeout'  => 2.0,
    ]);
        
    $options['headers'] = [
        "Content-Type" => "application/json",
        "Cookie" => "XDEBUG_SESSION=PHPSTORM",
    ];

    $response = $guzzleHttpClient->request(
        'POST', 
        'http://www.magento2-example-modules.dev/index.php/rest/default/V1/customer/', 
        $options
    );

    $newCustomerJson = (string)$response->getBody();
    

SOAP

Set the Xdebug cookie against the SOAP client object.

If triggering this script through the browser, don't use the Xdebug bookmarklet.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
$username = 'username';
$password = 'password';

$client = new SoapClient('http://www.example.com/index.php/api/soap/?wsdl', array('trace' => 1));
$client->__setCookie('XDEBUG_SESSION', 'PHPSTORM');

$session = $client->login($username, $password);

try {
    $result = $client->call(
        $session,
        'api.methodToCall',
        [
            $param1,
            $param2,
            $someData,
        ]
    );
    echo "<pre>";
    print_r($result);
    echo "</pre>";
} catch (SoapFault $e) {
    echo $e->getMessage();
}

$client->endSession($session);

XML-RPC

Create a Zend_Http_Client object and set the Xdebug cookie against that.

If triggering this script through the browser, don't use the Xdebug bookmarklet.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
$username = 'username';
$password = 'password';

require_once 'vendor/autoload.php';

try {

    $httpClient = new Zend_Http_Client();
    $httpClient->setConfig(['timeout' => '30']);
    $httpClient->setCookie('XDEBUG_SESSION', 'PHPSTORM');

    $client = new Zend_XmlRpc_Client('http://www.example.com/api/xmlrpc/', $httpClient);

    $session = $client->call('login', [$username, $password]);

    $result = $client->call('call', [$session, 'sales_order.info', '700005204']);

    echo "<pre>";
    print_r($result);

    $client->call('endSession', [$session]);

} catch (Exception $exception) {

    echo "<pre>";
    print_r($exception);

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment