PHP SoapClient - Returning attribute values in a r

2019-05-18 13:27发布

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条回答
疯言疯语
2楼-- · 2019-05-18 13:56

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.

查看更多
登录 后发表回答