修改 PHP / SOAP 代码以在所有请求中添加 HTTP 标头

发布于 2024-09-15 11:00:13 字数 1156 浏览 2 评论 0原文

我继承了一些 php SOAP 代码,由于我们正在使用的服务发生了变化,我需要修改为“在所有请求的 HTTP 标头中添加授权”。我不知道该怎么做,甚至不知道是否可能。

部分相关代码如下所示:

    function soap_connect() {
            $soap_options = array(
                    'soap_version' => SOAP_1_2,
                    'encoding' => 'UTF-8',
                    'exceptions' => FALSE
            );
            try {
                    $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
            } catch (SoapFault $fault) {
                    return FALSE;
            }
            return TRUE;
    }

我认为,据我了解,它应该只是输出以下内容(现在):

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Content-Length: 255
...

文档说最终的 HTTP 请求应该如下所示:

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw
Content-Length: 255

I;ve inherited some php SOAP code and due to changes in the service we are using, I need to modify to "add an authorization in the HTTP headers of all requests". I'm not sure what to do and if its even possible.

Part of the relevant code looks like this:

    function soap_connect() {
            $soap_options = array(
                    'soap_version' => SOAP_1_2,
                    'encoding' => 'UTF-8',
                    'exceptions' => FALSE
            );
            try {
                    $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
            } catch (SoapFault $fault) {
                    return FALSE;
            }
            return TRUE;
    }

I think, as I understand it, it should be just outputting the following (right now):

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Content-Length: 255
...

The documentatino says that the final HTTP request should look like:

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw
Content-Length: 255

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-09-22 11:00:13

添加流上下文以向 HTTP 调用提供附加标头。

function soap_connect() {
    $context = array('http' =>
        array(
            'header'  => 'Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw'
        )
    );
    $soap_options = array(
        'soap_version' => SOAP_1_2,
        'encoding' => 'UTF-8',
        'exceptions' => FALSE,
        'stream_context' => stream_context_create($context)
    );
    try {
        $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
    } catch (SoapFault $fault) {
        return FALSE;
    }
    return TRUE;
}

请参阅 SoapClient::__construct()HTTP 上下文选项 了解更多信息,

Add a stream context to provide the additional headers to the HTTP call.

function soap_connect() {
    $context = array('http' =>
        array(
            'header'  => 'Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw'
        )
    );
    $soap_options = array(
        'soap_version' => SOAP_1_2,
        'encoding' => 'UTF-8',
        'exceptions' => FALSE,
        'stream_context' => stream_context_create($context)
    );
    try {
        $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
    } catch (SoapFault $fault) {
        return FALSE;
    }
    return TRUE;
}

Please see SoapClient::__construct() and HTTP context options for further information,

澉约 2024-09-22 11:00:13

在没有看到 wsdl 的情况下,很难弄清楚服务器期望什么类型的结构,但这里有一些示例:

简单 HTTP 身份验证

$soap_options = array(
                'soap_version'  =>  SOAP_1_2,
                'encoding'      =>  'UTF-8',
                'exceptions'    =>  FALSE,
                    'login'         =>  'username',
                    'password'      =>  'password'
        );
        try {
                $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
        } catch (SoapFault $fault) {
                return FALSE;
        }
        return TRUE;    

对于实现更高级的自定义方法的服务器:

// Namespace for SOAP functions
$ns         =   'Namespace/Goes/Here';

// Build an auth array
$auth = array();
$auth['AccountName']    =   new SOAPVar($this->account['AccountName'], XSD_STRING, null, null, null, $ns);
$auth['ClientCode']     =   new SOAPVar($this->account['ClientCode'], XSD_STRING, null, null, null, $ns);
$auth['Password']       =   new SOAPVar($this->account['Password'], XSD_STRING, null, null, null, $ns);

// Create soap headers base off of the Namespace
$headerBody = new SOAPVar($auth, SOAP_ENC_OBJECT);
$header = new SOAPHeader($ns, 'SecuritySoapHeader', $headerBody);
$client->__setSOAPHeaders(array($header));

It's quite difficult to figure out what type of structure the server is expecting without seeing the wsdl, but here are a few examples:

Simple HTTP auth

$soap_options = array(
                'soap_version'  =>  SOAP_1_2,
                'encoding'      =>  'UTF-8',
                'exceptions'    =>  FALSE,
                    'login'         =>  'username',
                    'password'      =>  'password'
        );
        try {
                $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
        } catch (SoapFault $fault) {
                return FALSE;
        }
        return TRUE;    

For the servers which implement a more advanced, custom method:

// Namespace for SOAP functions
$ns         =   'Namespace/Goes/Here';

// Build an auth array
$auth = array();
$auth['AccountName']    =   new SOAPVar($this->account['AccountName'], XSD_STRING, null, null, null, $ns);
$auth['ClientCode']     =   new SOAPVar($this->account['ClientCode'], XSD_STRING, null, null, null, $ns);
$auth['Password']       =   new SOAPVar($this->account['Password'], XSD_STRING, null, null, null, $ns);

// Create soap headers base off of the Namespace
$headerBody = new SOAPVar($auth, SOAP_ENC_OBJECT);
$header = new SOAPHeader($ns, 'SecuritySoapHeader', $headerBody);
$client->__setSOAPHeaders(array($header));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文