谷歌镜像 伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: SoapVar::SoapVar

来源:网络转载 浏览:32593次 时间:2024-05-07
Yar » « SoapVar::__construct
  • PHP 手册
  • 函数参考
  • Web 服务
  • SOAP
  • SoapVar

SoapVar::SoapVar

(PHP 5, PHP 7)

SoapVar::SoapVar — SoapVar constructor

说明

SoapVar::SoapVar ( mixed $data , string $encoding [, string $type_name [, string $type_namespace [, string $node_name [, string $node_namespace ]]]] )

Constructs a new SoapVar object.

参数

data

The data to pass or return.

encoding

The encoding ID, one of the XSD_... constants.

type_name

The type name.

type_namespace

The type namespace.

node_name

The XML node name.

node_namespace

The XML node namespace.

范例

Example #1 SoapVar::SoapVar() example

<?php
class SOAPStruct {
    function SOAPStruct($s, $i, $f) 
    {
        $this->varString = $s;
        $this->varInt = $i;
        $this->varFloat = $f;
    }
}
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$struct = new SOAPStruct('arg', 34, 325.325);
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "SOAPStruct", "http://soapinterop.org/xsd");
$client->echoStruct(new SoapParam($soapstruct, "inputStruct"));
?>

参见

  • SoapClient::__soapCall() - Calls a SOAP function
  • SoapParam::SoapParam() - SoapParam constructor
add a note

User Contributed Notes 9 notes

up down 50 Francesco Nardone5 years ago I spent hours trying to find how to send a request where an element is repeated. Here is how I managed to do it:
<?php
$parm = array();
$parm[] = new SoapVar('123', XSD_STRING, null, null, 'customerNo' );
$parm[] = new SoapVar('THIS', XSD_STRING, null, null, 'selection' );
$parm[] = new SoapVar('THAT', XSD_STRING, null, null, 'selection' );
$resp = $client->getStuff( new SoapVar($parm, SOAP_ENC_OBJECT) );
?>

This will send something like:
<getStuff>
  <customerNo>123</customerNo>
  <selection>THIS</selection>
  <selection>THAT</selection>
</getStuff>

Hope this will save someone else's time.
up down 14 Joni Lindqvist6 years ago I worked for hours to figure out how to create following structure:

<ns:Foo attr='bar'>
   <ns:Baz attr='foobar'>barbaz</ns:Baz>
</ns:Foo>

It can be done with following array structure:

$arr = array(
  'Foo' => array(
     'attr'=>'bar',
     'Baz'=>array( '_' => 'barbaz', 'attr'=>'foobar')
  )
);
up down 4 OrionI13 years ago This class is useful when dealing with the "anyType" type (generic object reference): it lets you specify the xsd type to provide "late binding" type information.

Here's a really simple example: I have a .NET service that can take a string, a date, and integer, or other types, so I use the .NET "object" type. Here's an example of such a service--this one just tells me what type I passed in. (It's nice to use when checking to see if PHP passed in the type information the way .NET expects it.)

//inside a service.asmx.cs file...
[WebMethod]
public string WhatTypeIsThis(object ObjectParameter)
{
   return "You passed in a " + ObjectParameter.GetType().Name
     + ": " + ObjectParamter.ToString();
}

To call this service with a string from PHP, I used this code:
<?php
//set up the service client using WSDL
$client = new SoapClient("http://localhost/folder/service.asmx?WSDL");

//This is the variable that will be typed as an XSD string
$typedVar = new SoapVar("mystring", XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
//This is the wrapper object for incoming parameters
$wrapper->ObjectParameter = $typedVar;
//This is the named parameter object that will be passed in to the service
$params = new SoapParam($wrapper, "WhatTypeIsThis");

//call the service with the string
$result = $client->WhatTypeIsThis($params);
//show the result
echo $result->WhatTypeIsThisResult;
?>

The output from this should be:
"You passed in a String: mystring"

The SOAP message that is passed in looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:WhatTypeIsThis>
            <ns1:ObjectParameter xsi:type="xsd:string">mystring</ns1:ObjectParameter>
        </ns1:WhatTypeIsThis>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

It's that xsi:type="xsd:string" that gives .NET the heads up that though the ObjectParameter is being passed in as an object, it is also a string.

When trying other types, it's helpful to see exactly what is being sent to the service--to see the SOAP messages like the one above, use the trace option when making your SOAP client and then call the $client->__getLastRequest() function. (See http://www.php.net/soap_soapclient_getlastrequest)  You may need to use try/catch constructs if you're generating errors.

Another helpful function is var_dump($client->__getTypes())--it shows how PHP parsed the WSDL file to create types to pass back and forth.
up down 3 it at teamamericany dot com5 years ago To send requests containing ComplexTypes we needed to create stdClass objects for each ComplexType. If the ComplexType has an attribute we created a class with all the fields and attributes as members and added this class to the classmap with the correct type name from the wsdl. For example:

//to create soap client
            $soapClient = new SoapClient("<your wsdl url>",
                 array('classmap' => array('RoomType' => 'RoomType'), 'trace' => 1));
                
             //creating request object using custom class since there is an attribute needed

    class RoomType {
        //attribute
        var $code;       
        var $name;
    }
            $RoomType = new RoomType();
            $RoomType->code = $code;
           
                        //plain stdClass is ok since no attributes defined
            $LRoomCode = new stdClass();
            $LRoomCode->RoomType = new SoapVar($RoomType, SOAP_ENC_OBJECT, "RoomType", "<your namespace url from wsdl>");
           
            //define a basic class object since attributes are not needed for this complex type
             $RateAvailabilityReq = new stdClass();
             $RateAvailabilityReq->LRoomCode = new SoapVar($LRoomCode, SOAP_ENC_OBJECT, "ArrayOfRoomType", "<your namespace url>");           
            $RateAvailabilityReq->idB2BHotel = $idB2BHotel;
             $RateAvailabilityReq->idProduct = $idProduct;
             $RateAvailabilityReq->lenght = $lenght;
            $RateAvailabilityReq->nameDistributor = $nameDistributor;
            $RateAvailabilityReq->password = $password;
            $RateAvailabilityReq->propertyCode = $propertyCode;           
             $RateAvailabilityReq->startDate = $startDate;
             $RateAvailabilityReq->userName = $userName;
             $RateUpdateReqType = new SoapVar($RateAvailabilityReq , SOAP_ENC_OBJECT, "RateAvailabilityReq", "<your namespace url>");
            
             $soapRequestType = new stdClass();
             $soapRequestType->in0 = $RateUpdateReqType;
             $getRateAvailabilityReq = new SoapVar($soapRequestType , SOAP_ENC_OBJECT, "getRateAvailability", "<your namespace url>");
            
             $result = $soapClient->getRateAvailability($getRateAvailabilityReq);

Reading attributes from ComplexType response objects also required a custom class added to the classmap array with the attribute as a member of the class.
up down 6 Krzych8 years ago It might be obvious to some but not to everyone
if you ever wondered how to encode attribute in soap object, use proper coding and not hacking by passing xml and flag XSD_ANYXML

try using this
<?php
$amount['_'] = 25;
$amount['currencyId'] = 'GBP';
$encodded = new SoapVar($amount, SOAP_ENC_OBJECT);

?>
and end result wound be
<amount currencyId="GBP">25</amount>

hope that helps someone
up down 1 ericvaneldik at gmail dot com1 month ago If you want to create an soap header wihtout namespace and without an item key value setup, you can use SoapVar

To get this:
<SOAP-ENV:Header>
    <IdentityHeader>
      <SessionID>123456789</SessionID>
    </IdentityHeader>
  </SOAP-ENV:Header>

you can use this php code:
<?php
$headerVar = new SoapVar('<IdentityHeader><SessionID>123456789</SessionID></IdentityHeader>',
      XSD_ANYXML);
$header = new SoapHeader('http://tempuri.org/','RequestParams',
      $headerVar);
?>
up down -1 tcell7 years ago One potential gotcha with this is that if the XML you are passing in has the <?xml... declaration, it may cause problems on the server side. It's best to strip this out with str_replace. up down -1 J. Fiala1 year ago In WSDL-mode you can simply use an object as request and php-soap will map attribute and node-values automatically!
So there's no need to use SoapParam/SoapVar or whatever.

Just use plain objects in WSDL mode and happy soap'ing.
up down -6 cb at wasteland dot org9 years ago If you need to add attributes XML entities in your SOAP query like this:

"<FilterBy Column="Id" FilterOperator="=" FilterValue="NUMBER" Table="Case">"

Use SoapVar() with the type XSD_ANYXML as shown below.  Example uses __soapCall and other SoapParams.

<?php
$response = $client->__soapCall('GetFilteredRecordList',
    array(new SoapParam($DSToken, 'Token'),
          new SoapParam('Slide', 'TableName'),
          new SoapVar('<FilterBy Column="Id" FilterOperator="=" FilterValue="NUMBER" Table="Case"/>', XSD_ANYXML)),
    array('soapaction' => 'http://www.example.com/webservices/GetFilteredRecordList'));
?>

Use this so you don't waste HOURS looking around:
add a note

官方地址:https://www.php.net/manual/en/soapvar.soapvar.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net