user1028037
user1028037

Reputation: 119

Convert php exception (soap fault) to a specific class

I am having problem converting a php Exception to a generic class.

I am calling a web service method and when it fails it returns a soap fault with information in the "detail tag". This is how the result looks like if I call the web service method with Soap UI (http://soapui.org) :

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode>s:Client</faultcode>
         <faultstring xml:lang="sv-SE">Error Posting New Sponsor Full to Middleware</faultstring>
         <detail>
            <MyPlanWSError xmlns="http://schemas.datacontract.org/2004/07/MyPlanPOA" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <BadParameters i:nil="true"/>
               <Details>2 duplicate(s) detected</Details>
               <Duplicates>
                  <DuplicateInfo>
                     <DuplicatedExtRelNo>0</DuplicatedExtRelNo>
                     <POAStatus>New</POAStatus>
                     <Source>NewExtRels</Source>
                     <SourceRecordID>194</SourceRecordID>
                     <Type>eMail</Type>
                  </DuplicateInfo>
                  <DuplicateInfo>
                     <DuplicatedExtRelNo>0</DuplicatedExtRelNo>
                     <POAStatus>New</POAStatus>
                     <Source>NewExtRels</Source>
                     <SourceRecordID>194</SourceRecordID>
                     <Type>Address</Type>
                  </DuplicateInfo>
               </Duplicates>
               <ErrorNumber>7</ErrorNumber>
            </MyPlanWSError>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

I want to get the MyPlanWSError content that is in the "detail" tag. I want to convert that to a php class.

I have used the nice tool wsdl2php (http://www.urdalen.no/wsdl2php) to generate helper classes based on a wsdl file, so that I don't have to write all code myself :) The tool generated the MyPlanWSError which looks like this:

class MyPlanWSError
{
  public $BadParameters;
  public $Details;
  public $Duplicates;
  public $ErrorNumber;
  public function __construct($BadParameters, $Details, $Duplicates, $ErrorNumber)
  {
    $this->BadParameters = $BadParameters;
    $this->Details = $Details;
    $this->Duplicates = $Duplicates;
    $this->ErrorNumber = $ErrorNumber;
  }
}

When I call the web service method (that returns the soap fault), I do that in a try catch clause:

function add_new_sponsor() {
  // ... code
  try {
    $new_sponsor = new PostNewSponsor(
        $accessKey,
        $type,
        $initialRecordStatus,
        $monthlyAmount,
        $categoryCode,
        $titleCode,
        $firstName,
        $lastName,
        $organisationName,
        $street,
        $houseNumber,
        $apartment,
        $extraAddressLine,
        $postCode,
        $town,
        $countryISOCode,
        $privatePhone,
        $mobilePhone,
        $workPhone,
        $eMailAddress,
        $sourceCode,
        $paymentFrequencyCode,
        $paymentTypeCode,
        $numberOfChildren,
        $scGender,
        $continentCode,
        $scCountryISOCode,
        $olderChildFlag,
        $personalID,
        $AddressTypeCode,
        $extRelNo,
        $comments,
        $iPAddress);

    $result = plan_utils_post_sponsor($new_sponsor);
  } catch (Exception $e) {
    echo 'Exception->detail var_dump: <br/>';
    var_dump($e->detail);
    echo '<br/></br>';

    $myplan_error = cast('MyPlanWSError', $e->detail);
    echo 'MyPlanWSError var_dump <br/>';
    var_dump($myplan_error);
    echo '<br/><br/>';

    echo 'MyPlanWSError->Details var_dump <br/>';
    var_dump($myplan_error->Details);
    echo '<br/><br/>';
  }
}

function cast($destination, $sourceObject)
{
    if (is_string($destination)) {
        $destination = new $destination();
    }
    $sourceReflection = new ReflectionObject($sourceObject);
    $destinationReflection = new ReflectionObject($destination);
    $sourceProperties = $sourceReflection->getProperties();
    foreach ($sourceProperties as $sourceProperty) {
        $sourceProperty->setAccessible(true);
        $name = $sourceProperty->getName();
        $value = $sourceProperty->getValue($sourceObject);
        if ($destinationReflection->hasProperty($name)) {
            $propDest = $destinationReflection->getProperty($name);
            $propDest->setAccessible(true);
            $propDest->setValue($destination,$value);
        } else {
            $destination->$name = $value;
        }
    }
    return $destination;
}

The cast method was taken from //http://stackoverflow.com/questions/3243900/convert-cast-an-stdclass-object-to-another-class.

So what I expect the output to be from the var_dump($myplan_error->Details) is "2 duplicate(s) detected" but instead I get NULL :(

Here is the whole output from the catch clause:

Exception->detail var_dump:
object(stdClass)#4 (1) { ["MyPlanWSError"]=> object(stdClass)#5 (4) { ["BadParameters"]=> NULL ["Details"]=> string(23) "2 duplicate(s) detected" ["Duplicates"]=> object(stdClass)#6 (1) { ["DuplicateInfo"]=> array(2) { [0]=> object(stdClass)#7 (5) { ["DuplicatedExtRelNo"]=> string(1) "0" ["POAStatus"]=> string(3) "New" ["Source"]=> string(10) "NewExtRels" ["SourceRecordID"]=> string(3) "194" ["Type"]=> string(5) "eMail" } [1]=> object(stdClass)#8 (5) { ["DuplicatedExtRelNo"]=> string(1) "0" ["POAStatus"]=> string(3) "New" ["Source"]=> string(10) "NewExtRels" ["SourceRecordID"]=> string(3) "194" ["Type"]=> string(7) "Address" } } } ["ErrorNumber"]=> string(1) "7" } }

MyPlanWSError var_dump
object(MyPlanWSError)#3 (5) { ["BadParameters"]=> NULL ["Details"]=> NULL ["Duplicates"]=> NULL ["ErrorNumber"]=> NULL ["MyPlanWSError"]=> object(stdClass)#5 (4) { ["BadParameters"]=> NULL ["Details"]=> string(23) "2 duplicate(s) detected" ["Duplicates"]=> object(stdClass)#6 (1) { ["DuplicateInfo"]=> array(2) { [0]=> object(stdClass)#7 (5) { ["DuplicatedExtRelNo"]=> string(1) "0" ["POAStatus"]=> string(3) "New" ["Source"]=> string(10) "NewExtRels" ["SourceRecordID"]=> string(3) "194" ["Type"]=> string(5) "eMail" } [1]=> object(stdClass)#8 (5) { ["DuplicatedExtRelNo"]=> string(1) "0" ["POAStatus"]=> string(3) "New" ["Source"]=> string(10) "NewExtRels" ["SourceRecordID"]=> string(3) "194" ["Type"]=> string(7) "Address" } } } ["ErrorNumber"]=> string(1) "7" } }

MyPlanWSError->Details var_dump
NULL

So it seems the problem is that I can't succeed converting the $e>detail to a MyPlanWSError class.

Any ideas? :)

Upvotes: 4

Views: 1179

Answers (1)

Steven Moseley
Steven Moseley

Reputation: 16325

Looks like you're trying to cast the detail object, not the MyPlanWSError object. Thus, its properties aren't matching up, and the resulting object has all null values.

Try this instead, and it should work fine:

$myplan_error = cast('MyPlanWSError', $e->detail->MyPlanWSError);

Upvotes: 1

Related Questions