wos2 WSF/PHP soap header 认证实现

用SwA模式上传附件, 下载的包里有相关实例。但是看产生的soap消息包不是SwA模式的, 依然是MTOM模式的.

 

$uri = "http://wsi.portalservice.dpf.huawei.com";
$key = "asdf";
$pwd = "asdf";
$time = date("mdhms");
$pwd = base64_encode(hash("SHA256",$key.$pwd.$time));

$url = "http://127.0.0.1:1234/Service";
$reqPayload = <<<XML
<ns1:AddRequest xmlns:ns1="http://localhost/namespace">
<albumId>1106011156080031</albumId>
<fileName>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:content"></xop:Include>
</fileName>
</ns1:AddRequest>
XML;

$auth_data = array(
	new WSHeader(array('name'=>'operatorId','data'=>$key)),
	new WSHeader(array('name'=>'password','data'=>$pwd)),
	new WSHeader(array('name'=>'timeStamp','data'=>$time)),
);

$header_options = array(
	"ns"=>$uri,
	'prefix'=>"ns1",
	"name"=>"RequestSOAPHeader",
	"data"=>$auth_data,
);
$header = new WSHeader($header_options);

$filePath = "/path/to/file/file.jpg";
$f = file_get_contents($filePath);

$msg = new WSMessage($reqPayload,
	array(
		'inputHeaders'=>array($header),
		'attachments'=>array('content'=>$f),
		'defaultAttachmentContentType'=>"image/jpeg",
	)
);
$options = array(
	'to'=>$url,
	'useMTOM'=>"swa",
);
$api = new WSClient($options);

try {
	$resMsg = $api->request($msg);
} catch (Exception $e) {
	if ($e instanceof WSFault)
		printf("Soap Fault: %s\n", $e->Reason);
	else
		printf("Message = %s\n", $e->getMessage());
}

 

PHP: Soap 通过 soap header 认证(non-wsdl 模式)

 

soap service 是 java的 axis2

soap client 是 php. = =

                 $url = "http://localhost:8000/PortalService";
                // 对应wsdl 文件定义中的targetNamespace
		$uri = "http://localhost/partalService";

		$key = "xxx";
		$pwd = "xxx";

		$options=array(
			//'soap_version'=>SOAP_1_2,
			'trace'=>1,
			//"exceptions"=>1,
			'location'=>$url,
			'uri'=>$uri,
		);

		$auth_header = array(
			'user'=>$key,
			'password'=>$pwd,
		);
                // 下面的RequestSOAPHeader 对应 wsdl 定义里面的 <xsd:element name="RequestSOAPHeader">.....
		$authvalues = new SoapVar($auth_header, SOAP_ENC_OBJECT,"RequestSOAPHeader",$uri);  
		$header = new SoapHeader($uri, 'RequestSOAPHeader', $authvalues);

		$api = new SoapClient(null,$options);
		$api->__setSoapHeaders(array($header));
		
                // 在调用 java axis2 soap 的使用要这么定义, 不然会变成 <param0><item>xxx</item><value>xxx</value></param0>
                // 所以会报错. 用一下定义生成的就是这样的格式: <user>asdf</user>
		$p = array(
			new SoapParam("asdf",'user'),
			new SoapParam(1,'password'),
		);
		try {
			$call="Login";
			$result = $api->__soapCall($call,$p);
			print_r($result);
		} catch(SoapFault $e) {
			if(1) {
				var_dump($api->__getLastRequestHeaders());
				var_dump($api->__getLastRequest());
				var_dump($api->__getLastResponseHeaders());
				var_dump($api->__getLastResponse());
				echo $e->getMessage();
			}
		}