PHP SoapVar Object Attribute?(PHP SoapVar 对象属性?)
问题描述
有没有人知道如何向 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 对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP SoapVar 对象属性?
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01