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;
}

SoapClient::__construct()詳細については、HTTP コンテキスト オプションを参照してください。

于 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 に答える