3

我已经继承了一些 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
4

2 回答 2

10

添加流上下文以向 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;
}

请参阅HTTP 上下文选项以获取更多信息SoapClient::__construct()

于 2010-08-22T14:43:14.467 回答
0

如果不查看 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));
于 2010-08-22T13:57:07.480 回答