2010-08-22 114 views
2

我繼承了一些php SOAP代碼,並且由於我們正在使用的服務發生更改,我需要修改以「在所有請求的HTTP標頭中添加授權」。我不確定該做什麼,甚至可能。相關代碼的修改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 

回答

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)); 
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瞭解更多信息,

+0

完美。我甚至不知道你可以做到這一點。謝謝! – 2011-01-24 22:24:29