SoapClient set custom HTTP Header

后端 未结 5 1895
傲寒
傲寒 2020-12-30 06:36

I am doing some work writing a PHP-based SOAP client application that uses the SOAP libraries native to PHP5. I need to send a an HTTP cookie and an additional HTTP header

5条回答
  •  别那么骄傲
    2020-12-30 07:21

    This answer is the proper way to do it in PHP 5.3+ SoapClient set custom HTTP Header

    However, PHP 5.2 does not take all of the values from the stream context into consideration. To get around this, you can make a subclass that handles it for you (in a hacky way, but it works).

    class SoapClientBackport extends SoapClient {
      public function __construct($wsdl, $options = array()){
        if($options['stream_context'] && is_resource($options['stream_context'])){
          $stream_context_options = stream_context_get_options($options['stream_context']);
          $user_agent = (isset($stream_context_options['http']['user_agent']) ? $stream_context_options['http']['user_agent'] : "PHP-SOAP/" . PHP_VERSION) . "\r\n";
          if(isset($stream_context_options['http']['header'])){
            if(is_string($stream_context_options['http']['header'])){
              $user_agent .= $stream_context_options['http']['header'] . "\r\n";
            }
            else if(is_array($stream_context_options['http']['header'])){
              $user_agent .= implode("\r\n", $stream_context_options['http']['header']);
            }
          }
          $options['user_agent'] = $user_agent;
        }
        parent::__construct($wsdl, $options);
      }
    }
    

提交回复
热议问题