Pergunta

Due to a firewall audit, requests must always have the "UserAgent" and "Accept" header.

I tried this:

$soapclient = new soapclient('http://www.soap.com/soap.php?wsdl',
    array('stream_context' => stream_context_create(
        array(
            'http'=> array(
                'user_agent' => 'PHP/SOAP',
                'accept' => 'application/xml')
            )
        )
    )
);

the request received by the server soap

GET /soap.php?wsdl HTTP/1.1
Host: www.soap.com
User-Agent: PHP/SOAP
Connection: close

the expected result

GET /soap.php?wsdl HTTP/1.1
Host: www.soap.com
Accept application/xml
User-Agent: PHP/SOAP
Connection: close

Why "Accept" has not been sent? "User-Agent" works!

Foi útil?

Solução

The SoapClient constructor will not read all of the stream_context options when generating the request headers. However, you can place arbitrary headers in a single string in a header option inside http:

$soapclient = new SoapClient($wsdl, [
    'stream_context' => stream_context_create([
        'user_agent' => 'PHP/SOAP',
        'http'=> [
            'header' => "Accept: application/xml\r\n
                         X-WHATEVER: something"               
        ]
    ])
]);

For setting more than one, separate them by \r\n.

(As mentioned by Ian Phillips, the "user_agent" can be placed either at the root of the stream_context, or inside the "http" part.)

Outras dicas

According to the PHP SoapClient manual page, user_agent is a top-level option. So you should modify your example like so:

$soapclient = new SoapClient('http://www.soap.com/soap.php?wsdl', [
    'stream_context' => stream_context_create([
        'http' => ['accept' => 'application/xml'],
    ]),
    'user_agent' => 'My custom user agent',
]);

If you want your code more flexible juste use this.

$client = new SoapClient(
            dirname(__FILE__) . "/wsdl/" . $env . "/ServiceAvailabilityService.wsdl",
            array(
                'login' => $login,
                'password' => $password
            )
        );
        //Define the SOAP Envelope Headers
        $headers = array();
        $headers[] = new SoapHeader(
            'http://api.com/pws/datatypes/v1',
            'RequestContext',
            array(
                'GroupID' => 'xxx',
                'RequestReference' => 'Rating Example',
                'UserToken' => $token
            )
        );

//Apply the SOAP Header to your client
$client->__setSoapHeaders($headers);

Maybe you can use fsockopen() method to do that like this

<?php
$sock = fsockopen('127.0.0.1' /* server */, 80 /* port */, $errno, $errstr, 1);

$request = "<Hello><Get>1</Get></Hello>";
fputs($sock, "POST /iWsService HTTP/1.0\r\n");
fputs($sock, "Content-Type: text/xml\r\n");
fputs($sock, "Content-Length: ".strlen($request)."\r\n\r\n");
fputs($sock, "$request\r\n");
$buffer = '';
while($response = fgets($request, 1024)){
    $buffer .= $response;
}
// Then you can parse that result as you want
?>

I use that method manually to get SOAP data from my fingerprint machine now time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top