簡體   English   中英

如何使用php為我的xml內容設置soap標頭

[英]how to set soap header using php for my xml content

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xxxxx="xxxxx" xmlns:tem="http://tempuri.org/">
   <soapenv:Header>
      <Auth>
         <!--Optional:-->
         <xxxxx:id></xxxxx:id>
         <!--Optional:-->
         <xxxxx:Login></xxxxx:Login>
         <!--Optional:-->
         <xxxxx:pass></xxxxx:pass>
      </Auth>
   </soapenv:Header>
   <soapenv:Body>
      <tem:test>
         <!--Optional:-->
         <tem:test>test</tem:test>
      </tem:test>
   </soapenv:Body>
</soapenv:Envelope>

的PHP

<?php

try {
    $client = new SoapClient(self::$url, array());
    $header = new SoapHeader("xxxxx", "Auth", $header_data, false);

    $client->__setSoapHeaders([$header]); 

    $result = $client->ProcessarIntegracao($header, $test);
}
catch (Exception $e) {
    return 'soap error: ' . $e->getMessage();
}

我嘗試了許多方法來發送請求,但始終會返回

找不到肥皂標頭

okay I fixed this thaks for your help, here is my code

<?php
error_reporting(-1);
ini_set('error_reporting', E_ALL);
ini_set("soap.wsdl_cache_enabled", "0");
ini_set('display_errors', 1);


class XSoapClient extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = null)
    {
        $request = preg_replace('/ns2:xxxxx/','xxxxx',$request, -1);  
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
}



//phpinfo();die();
$url = 'xxxxx';
$data = array(
               'test' => 'test'
            );

    $client = new XSoapClient($url, array(
     "trace" => 1,
     "classmap" => array(
          'xxxxx' => 'xxxxx'
     )
    ));

    $ns = "xxxxx";

$auth = "<your header xml><ns2:id>xx</ns2:id><xxx:Login>xxxx</xxx:Login><xxx:pass>xxx</xxx:pass></your header xml>";
$auth_block = new SoapVar( $auth, XSD_ANYXML, NULL, NULL, NULL, NULL );

class ServiceHeader{}
$serviceHeader = new ServiceHeader();
$serviceHeader->id = xxx;
$serviceHeader->Login = "xxxx";
$serviceHeader->pass = "xxxx";

$header = new SoapHeader($ns, "xxxx", $serviceHeader);
//$header = new SoapHeader( $ns, 'Header', $auth_block );
    $client->__setSoapHeaders($header);
try{
    $result =  $client->ProcessarIntegracao($data);
} catch (exception $ex){

echo "<pre>";  

print_r($client);
//echo "</pre>";
print_r($header);
print_r($client->__getLastRequest());

echo "</pre>";

}
<?php
error_reporting(-1);
ini_set('error_reporting', E_ALL);
ini_set("soap.wsdl_cache_enabled", "0");
ini_set('display_errors', 1);

//Generating a header

$client = new soapClient("https://secure.payzen.eu/vads-ws/v5?wsdl", $options = array(
    'trace'      => 1,
    'exceptions' => 0,
    'encoding'   => 'UTF-8',
    'soapaction' => '')
);

//Calculating values transmitted in the header

$ns        = 'http://v5.ws.vads.lyra.com/Header/';
$shopId    = "12345678";
$requestId = gen_uuid();
$timestamp = gmdate("Y-m-d\TH:i:s\Z");
$mode      = "TEST";
$key       = "123";
$authToken = base64_encode(hash_hmac('sha256', $requestId . $timestamp, $key, true));

//Creating the shopId, requestId, timestamp, mode et authToken headers

$headerShopId    = new SOAPHeader($ns, 'shopId', $shopId);
$headerRequestId = new SOAPHeader($ns, 'requestId', $requestId);
$headerTimestamp = new SOAPHeader($ns, 'timestamp', $timestamp);
$headerMode      = new SOAPHeader($ns, 'mode', $mode);
$headerAuthToken = new SOAPHeader($ns, 'authToken', $authToken);

//Adding headers into the SOAP Header

$headers = array(
    $headerShopId,
    $headerRequestId,
    $headerTimestamp,
    $headerMode,
    $headerAuthToken,
);

$client->__setSoapHeaders($headers);

function gen_uuid()
{
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),
        mt_rand(0, 0xffff),
        mt_rand(0, 0x0fff) | 0x4000,
        mt_rand(0, 0x3fff) | 0x8000,
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM