PHP SoapClient - Returning attribute values in a r

2019-05-18 13:38发布

问题:

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.