問題描述
有沒有人知道如何向 SoapVar 對象添加屬性?看起來這很簡單,但我無法接受/工作.
我查看了 PHP 文檔和以下 stackoverflow 問題:
關于 SoapVar 的文檔,
stackoverflow 問題:SoapVar/Param 和 SOAP 中的嵌套重復元素>
我正在嘗試添加一個像這個數組示例一樣的屬性,但使用復雜的 SoapVar 對象.
最終結果是
25
謝謝.
在 SOAP 元素中獲取屬性有點麻煩.他們實施的方式有點令人困惑.
首先要做的是將屬性添加到 SoapServer
用來正確讀取和響應 SOAP 請求的 wsdl 文件.
<xs:simpleContent><xs:extension base=xs:string">**
我們必須告訴 SoapServer
使用一個 php 幫助類,方法是將它作為 classmap
傳遞到選項中:
$soap_server = new SoapServer($wsdl_file, array('cache_wsdl' =>1、'跟蹤' =>真的,'類圖' =>數組('mediaCollection' => 'SoapMediaHelper')));
我們在這里映射的是 SOAP 元素名稱 mediaCollection
到我們的一個類 SoapMediaHelper
.我們現在可以返回一個 class
,而不是返回數組,在這種情況下,它被命名為 SoapMediaHelper
.該類可以具有soap-element=>值對以及soap-attribute=>值對.
假設我們已經創建了一個處理 mediaCollection
的類,這會告訴 SoapServer
將一個名為 SoapMediaHelper
的類映射到它.這個類真的很簡單:
class SoapMediaHelper{公共函數 __construct(Array $properties = array()){foreach ($properties as $key => $value) {$this->{$key} = $value;}}}
必須填充此類的屬性.這些屬性應該是 tagname=>value 對以及我們想要添加到 mediaCollection
的屬性的屬性名稱和值對.SoapServer
將根據我們的 wsdl 文件找出哪個是哪個.
我們仍然需要填充這個對象,我們可以用另一個類來完成.
class SoapVarHelper{公共函數 get($the_playlist, $playlist_id, $owned_by_user){/* SoapMediaHelper 類映射到 mediaCollection wsdl.* 只需要在使用 php 的 SoapServer 時能夠在 XML 節點上設置屬性* */$media_helper = new SoapMediaHelper($the_playlist);/* 對于這種類型,必須設置以下 xml 屬性.(不在上面的 wsdl 示例中.)*/if($playlist_id === '播放列表'){$media_helper->readOnly = false;$media_helper->userContent = true;$media_helper->renameable = false;$media_helper->canDeleteItems = true;}如果($owned_by_user){$media_helper->readOnly = false;$media_helper->userContent = false;$media_helper->renameable = true;$media_helper->canDeleteItems = true;$media_helper->canReorderItems = true;}返回新的 SoapVar($media_helper, SOAP_ENC_OBJECT);}}
這個類應該用普通的標記名=>值對來調用.然后添加我們想要的屬性.在這種情況下是有條件的.我們將 SoapMediaHelper
對象提供給 SoapVar
.(我們之前告訴 SoapServer
這很好.)
現在我們需要做的最后一件事是,在我們的 mediaCollection
類中,使用幫助器 SoapVarHelper
返回一個 SoapMediaHelper
(我們之前告訴 SoapServer
的一個).
在我們的 mediaCollection
中,我們有一個函數 get_metadata_for_root
:
public function get_metadata_for_root($user_id, $index, $count){$titles = 數組('幻燈片' =>'精選',);$media_metadata = array();foreach($titles as $key => $title){$播放列表=數組('id' =>$key,'標題' =>$title,'img' =>$this->_utils->get_url() .'/public/sonos/images/browse-icons/icon-default-legacy.png');**$media_metadata[] = $this->_soap_var_helper->get($playlist, $key, false);**}$res = 數組('計數' =>計數($media_metadata),'索引' =>0,'總計' =>計數($media_metadata),'mediaCollection' =>$media_metadata);}
對于每個 mediaCollection
結果,我們通過 soap_var_helper
傳遞它以確保不僅添加了 element=>value 對,而且還添加了我們想要的屬性
總結:
確保為
SoapServer
提供 wsdl 文件,以便它知道元素和屬性.在
SoapServer
選項中添加classmap
以告訴SoapServer
當我們給它一個SoapMediaHelper
對象而不是常規輸入.在響應請求之前,在本例中為
mediaCollection
,通過SoapMediaHelper
傳遞此響應.SoapVarHelper
將所有 properties=>value 對映射為類屬性,然后SoapMediaHelper
將向其添加屬性(也作為 name=>value 對).SoapServer
會處理剩下的事情.
Does anyone have any clue as to how I can add an attribute to a SoapVar object? It seems like it would be simple, but I can't get it to take/work.
I've looked at the PHP docs and at the following stackoverflow question:
Documentation on SoapVar,
stackoverflow question: SoapVar/Param and nested, repeated elements in SOAP
I'm trying to add an attribute like this array example, but using complex SoapVar objects instead.
<?php
$amount['_'] = 25;
$amount['currencyId'] = 'GBP';
$encodded = new SoapVar($amount, SOAP_ENC_OBJECT);
?>
and end result wound be
<amount currencyId="GBP">25</amount>
Thanks.
Getting attributes into SOAP elements is a bit of a hassle. The way they implemented it is a bit confusing.
First thing to do is add the attributes to the wsdl file that SoapServer
uses to correctly read and respond to the SOAP requests.
<xs:complexType name="encryptionContext">
<xs:simpleContent>
<xs:extension base="xs:string">
**<xs:attribute name="type" type="tns:encryptionType" />**
</xs:extension>
</xs:simpleContent>
</xs:complexType>
We will have to tell SoapServer
to use a php helper class by passing it in the options as classmap
:
$soap_server = new SoapServer($wsdl_file, array(
'cache_wsdl' => 1,
'trace' => true,
'classmap' => array('mediaCollection' => 'SoapMediaHelper')
));
What we are mapping here is the SOAP element name mediaCollection
to one of our classes, SoapMediaHelper
. Instead of returning arrays, we can now return a class
, in this case, it's named SoapMediaHelper
. The class can have soap-element=>value pairs as well as soap-attribute=>value pairs.
Assuming we already have made a class that handles mediaCollection
s, this tells SoapServer
to map a class called SoapMediaHelper
to it. The class is really simple:
class SoapMediaHelper
{
public function __construct(Array $properties = array())
{
foreach ($properties as $key => $value) {
$this->{$key} = $value;
}
}
}
The properties of this class have to be populated. These properties should be both the tagname=>value pairs as well as the attribute name and value pair(s) for the attributes we want to add to our mediaCollection
. SoapServer
will figure out which is which according to our wsdl file.
We will still have to populate this object, which we can do with yet another class.
class SoapVarHelper
{
public function get($the_playlist, $playlist_id, $owned_by_user){
/* The SoapMediaHelper class is mapped to the mediaCollection wsdl.
* This is only needed to be able to set attributes on the XML nodes while using php's SoapServer
* */
$media_helper = new SoapMediaHelper($the_playlist);
/* For this type, the following xml attributes have to be set. (Not in the wsdl example above.) */
if($playlist_id === 'playlists'){
$media_helper->readOnly = false;
$media_helper->userContent = true;
$media_helper->renameable = false;
$media_helper->canDeleteItems = true;
}
if($owned_by_user){
$media_helper->readOnly = false;
$media_helper->userContent = false;
$media_helper->renameable = true;
$media_helper->canDeleteItems = true;
$media_helper->canReorderItems = true;
}
return new SoapVar($media_helper, SOAP_ENC_OBJECT);
}
}
This class should be called with the normal tagname=>value pairs. It then adds the attributes we want. In this case conditionally. We feed our SoapMediaHelper
object to SoapVar
. (We told SoapServer
earlier that this is fine.)
Now the last thing we need to do is, in our mediaCollection
class, to use the helper SoapVarHelper
to return a SoapMediaHelper
(the one we told SoapServer
about earlier).
In our mediaCollection
we have a function get_metadata_for_root
:
public function get_metadata_for_root($user_id, $index, $count){
$titles = array(
'slides' => 'Featured',
);
$media_metadata = array();
foreach($titles as $key => $title){
$playlist = array(
'id' => $key,
'title' => $title,
'img' => $this->_utils->get_url() . '/public/sonos/images/browse-icons/icon-default-legacy.png'
);
**$media_metadata[] = $this->_soap_var_helper->get($playlist, $key, false);**
}
$res = array(
'count' => count($media_metadata),
'index' => 0,
'total' => count($media_metadata),
'mediaCollection' => $media_metadata
);
}
For every mediaCollection
result we pass it through the soap_var_helper
to make sure not only the element=>value pairs are added, but also the attributes that we want on it.
TO SUMMARIZE:
Make sure you feed the
SoapServer
with a wsdl file, so it know the elements and the attributes.In the
SoapServer
options addclassmap
in order to tellSoapServer
that it is fine when we feed it aSoapMediaHelper
object instead of the regular input.Before responding to a request for, in this case,
mediaCollection
, pass this response through theSoapMediaHelper
. TheSoapVarHelper
will map all the properties=>value pairs as class properties, then theSoapMediaHelper
will add attributes (also as name=>value pairs) to it.SoapServer
will take care of the rest.
這篇關于PHP SoapVar 對象屬性?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!