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

时间:2010-08-22 13:47:13

标签: php http soap soap-client

我继承了一些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
...

documentatino说最终的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

2 个答案:

答案 0 :(得分:9)

添加流上下文,为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 context options

答案 1 :(得分: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));