bScutt
bScutt

Reputation: 872

Running PHP SoapServer behind a proxy

I'm attempting to run both a PHP SoapClient and a SoapServer (for Magento) behind a proxy, where the only network traffic allowed out is via the proxy server.

I have got this working with the client as so:

$client = new SoapClient('https://www.domain.co.uk/api/v2_soap/?wsdl=1', [
    'soap_version' => SOAP_1_1,
    'connection_timeout' => 15000,
    'proxy_host' => '192.168.x.x',
    'proxy_port' => 'xxxx',
    'stream_context' => stream_context_create(
        [
            'ssl' => [
                'proxy' => 'tcp://192.168.x.x:xxxx',
                'request_fulluri' => true,
            ],
            'http' => [
                'proxy' => 'tcp://192.168.x.x:xxxx',
                'request_fulluri' => true,
            ],
        ]
    ),
]);

This works as expected - all the traffic is going via the proxy server.

However, with the SoapServer class, I can't work out how to force it to send all outbound traffic via the SoapServer. It appears to be trying to load http://schemas.xmlsoap.org/soap/encoding/ directly form the network, not via the proxy, which is causing the "can't import schema from 'http://schemas.xmlsoap.org/soap/encoding/'" error to be thrown.

I've tried adding a hosts file entry for schemas.xmlsoap.org to 127.0.0.1 and hosting this file locally, but I'm still getting the same issue.

Is there something I'm missing?

Upvotes: 16

Views: 1109

Answers (1)

PST
PST

Reputation: 346

Try stream_context_set_default like in file_get_contents: file_get_contents behind a proxy?

<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234";    // Proxy server port
$PROXY_USER = "LOGIN";    // Username
$PROXY_PASS = "PASSWORD";   // Password
// Username and Password are required only if your proxy server needs basic authentication

$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
 array(
    'http' => array(
    'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
    'request_fulluri' => true,
    'header' => "Proxy-Authorization: Basic $auth"
    // Remove the 'header' option if proxy authentication is not required
  )
 )
);
//Your SoapServer here

Or try to run server in non-WSDL mode

<?php
$server = new SoapServer(null, array('uri' => "http://localhost/namespace"));
$server->setClass('myClass');
$data = file_get_contents('php://input');
$server->handle($data);

Upvotes: 3

Related Questions