PHP SoapClient - Returning attribute values in a response

别来无恙 提交于 2020-01-21 09:07:31

问题


I'm attempting to get values from a webservice.

The responce is formated as..

<campaign Id="200"> <name> test </name> </campaign>

PHP Code

SoapClient( "WSDL");

$return = $client->GetCampaigns('Username', 'Password' );

Yet when I attempt to access the return, I get just a stdClass with the name attribute..

  public 'Campaign' => 
array
  0 => 
    object(stdClass)[46]
      public 'Name' => string 'chris test' (length=10)

回答1:


I find that I have to supply a "classmap" to SoapClient to get it to map the objects in the response to classes that are defined in PHP. In WSDLs the type name is usually lower camel case (starting with lower case and camel case the rest).

class MY_Campaign {
    private $name;
    function getName () { return $this->name; }
}

$options = array(
        'classmap' => array(
                'campaign' => 'MY_Campaign',
            );
    );
$client = new SoapClient('http://example.com/yourservice?wsdl', $options);
$return = $client->GetCampaigns ();

I might be able to supply a better answer if I had the WSDL. The classmap depends on the type definitions in the WSDL file.



来源:https://stackoverflow.com/questions/3959839/php-soapclient-returning-attribute-values-in-a-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!