【问题标题】:PHP SoapClient(): send "User-Agent" and "Accept" HTTP HeaderPHP SoapClient():发送“User-Agent”和“Accept”HTTP Header
【发布时间】:2014-04-29 07:50:42
【问题描述】:

由于防火墙审核,请求必须始终具有“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

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

【问题讨论】:

    标签: php soap http-headers


    【解决方案1】:

    SoapClient 构造函数在生成请求标头时不会读取所有 stream_context 选项。但是,您可以在 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"               
            ]
        ])
    ]);
    

    如果设置多个,用\r\n分隔。

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

    【讨论】:

      【解决方案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',
      ]);
      

      【讨论】:

        【解决方案3】:

        如果你想让你的代码更灵活,就用这个吧。

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

        【讨论】:

          【解决方案4】:

          也许你可以使用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 数据。

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-16
          • 1970-01-01
          • 1970-01-01
          • 2020-03-06
          • 2020-07-07
          • 2014-05-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多