Convert PHP exception (soap error) to a specific class

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

I call the web service method and, when it fails, it returns a soap error with the information in the "detail tag". Here's what the result looks like if I call the web service method using the soap interface (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 contents of MyPlanWSError, which is in the "detail" tag. I want to convert this to a php class.

I used the good wsdl2php tool (http://www.urdalen.no/wsdl2php) to create helper classes based on the wsdl file, so I don’t need to write all the code myself :) The MyPlanWSError tool created that 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 (which returns a soap error), I do this 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 casting method was taken from //http://stackoverflow.com/questions/3243900/convert-cast-an-stdclass-object-to-another-class.

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

Here is all the 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 the problem seems to be that I cannot convert the $e>detail class to MyPlanWSError .

Any ideas? :)

+4
source share
1 answer

It looks like you're trying to draw a part object, not a MyPlanWSError object. Thus, its properties do not match, and the resulting object has all zero values.

Try this instead and it should work fine:

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

Source: https://habr.com/ru/post/1447962/


All Articles