PHP SoapClient():send";“用户代理”;及;接受;HTTP头

PHP SoapClient():send";“用户代理”;及;接受;HTTP头,php,soap,http-headers,Php,Soap,Http Headers,由于防火墙审核,请求必须始终具有“UserAgent”和“Accept”头 我试过这个: $soapclient = new soapclient('http://www.soap.com/soap.php?wsdl', array('stream_context' => stream_context_create( array( 'http'=> array( 'user_agent' => 'P

由于防火墙审核,请求必须始终具有“UserAgent”和“Accept”头

我试过这个:

$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')
            )
        )
    )
);
服务器soap接收到的请求

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

GET /soap.php?wsdl HTTP/1.1
Host: www.soap.com
Accept application/xml
User-Agent: PHP/SOAP
Connection: close
为什么“接受”尚未发送?“用户代理”有效

根据,
user\u-agent
是一个顶级选项。因此,您应该修改您的示例,如下所示:

$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',
]);

SoapClient构造函数在生成请求头时不会读取所有流上下文选项。但是,您可以在
http
中的
header
选项中的单个字符串中放置任意头:

$soapclient = new SoapClient($wsdl, [
    'stream_context' => stream_context_create([
        'user_agent' => 'PHP/SOAP',
        'http'=> [
            'header' => "Accept: application/xml\r\n
                         X-WHATEVER: something"               
        ]
    ])
]);
要设置多个,请通过
\r\n
将它们分开


(如所述,“用户代理”可以放在流上下文的根目录下,也可以放在“http”部分中。)

也许您可以使用
fsockopen()
方法来实现这一点 像这样

<?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
?>


现在,我手动使用该方法从指纹机获取SOAP数据。

如果您希望代码更灵活,请使用此方法

$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);

我投了反对票,因为这个问题明确地询问了如何使用
SoapClient
(),而这并没有对请求提供任何帮助。