Get Sandbox and Gateway URL of DHL Shipping Programmatically Magento 2.

Magento comes with Out of the box DHL shipping Method to use the shipping provider as the DHL method.

While you are developing features related to the DHL shipment and you need sandbox URL for the test shipping method or real gateway URL you can achieve this using a programmatic way.

DHL has already sandbox and gateway URL available, You can retrieve both of the URL by simple Code snippet,

<?php
namespace Jesadiya\Dhl\Model;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class ShippingUrl
{
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    public function __construct(
        ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * DHL Sandbox URL
     *
     * @return string
     */
    public function getSandboxUrl()
    {
        $path = 'carriers/dhl/sandbox_url';

        $sandboxUrl = $this->scopeConfig->getValue(
            $path,
            ScopeInterface::SCOPE_STORE
        );
        return $sandboxUrl;
    }

    /**
     * DHL Gateway URL
     *
     * @return string
     */
    public function getGatewayUrl()
    {
        $path = 'carriers/dhl/gateway_url';

        $gatewayUrl = $this->scopeConfig->getValue(
            $path,
            ScopeInterface::SCOPE_STORE
        );
        return $gatewayUrl;
    }
}

Call method,
echo $dhlGatewayUrl = $this->getGatewayUrl();
Result: https://xmlpi-ea.dhl.com/XMLShippingServlet

echo $dhlSandboxUrl = $this->getSandboxUrl();
Result: https://xmlpitest-ea.dhl.com/XMLShippingServlet