PHP SoapClient():发送"用户代理"并且"接受" HTTP标头

时间:2014-04-29 07:50:43

标签: 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' => '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

为什么没有发送“接受”? “用户代理”有效!

4 个答案:

答案 0 :(得分:16)

生成请求标头时,SoapClient构造函数不会读取所有stream_context选项。但是,您可以在using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { const string FILENAME = @"c:\temp\test.xml"; static void Main(string[] args) { XDocument sdmx_response = XDocument.Load(FILENAME); XNamespace message = sdmx_response.Root.GetDefaultNamespace(); XNamespace generic = sdmx_response.Root.GetDefaultNamespace(); IEnumerable<XElement> DataSet = sdmx_response.Root.Elements(message + "DataSet"); IEnumerable<XElement> Series = DataSet.Elements(generic + "Series").Select(series => new XElement("Series", new object[] { new XElement("SeriesKey", series.Elements(generic + "SeriesKey").Elements("Value").Where(value =>((string)value.Attribute("id") == "PRODUCT" && (string)value.Attribute("value") == "Lumber") || ((string)value.Attribute("id") == "FIN" && (string)value.Attribute("export") == "Lumber")) ), series.Elements(generic + "Obs") })).ToList(); } } } 内的header选项中的单个字符串中放置任意标头:

http

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

(如Ian Phillips所述,“user_agent”可以放在stream_context的根目录下,也可以放在“http”部分内。)

答案 1 :(得分:2)

根据the PHP SoapClient manual pageuser_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',
]);

答案 2 :(得分:2)

如果您希望代码更灵活,请使用此功能。

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

答案 3 :(得分:-1)

也许你可以使用if and only if both conditions are met.方法来做到这一点 像这样

fsockopen()

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