[英]Modifying PHP / SOAP code to add HTTP Header in all requests
我繼承了一些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
添加流上下文以向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上下文選項 ,
如果沒有看到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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.