0

I am using a soap client in php to obtain information from a Soap service

I finally managed to get the call itself to work which is good news!

<?
$url = "MYURL";

$client = new SoapClient($url, array("trace" => 1, "exception" => 0)); 
$SOAPCall = "Method";
$SoapCallParameters = new stdClass();
$SoapCallParameters->webUserKey = 'Value1';
$SoapCallParameters->urn = 'Value2';

$obj = $client->Method($SoapCallParameters);
var_dump ($obj);
echo $obj->{'LastName'};
?>

The method returns via Dump

object(stdClass)#3 (1) { [["LastName"]=> string(10) "Test"} ]}

However the echo statement does not print the LastName property

What am I doing wrong? I have tried different combinations like putting the property into a string variable

I have tried other ways and its still not working

This method also does not work

$obj = $client->Method($SoapCallParameters);
$vars = get_object_vars($obj);
$arr = (array)$vars;

var_dump($arr);

$lastName = (string) $arr[0]["LastName"];
echo $lastName;

Doing a var_dump using the logic above gives

I can see from the var_dump that there are values in the response

array(2) {
  ["CheckRegistrationStatus_UrnResult"]=>
  string(10) "MatchFound"
  ["result"]=>
  object(stdClass)#4 (6) {
    ["Age"]=>
    string(2) "21"
    ["FirstClaimClub"]=>
    string(47) "VALUE"
    ["FirstName"]=>
    string(6) "FIRSTNAME"
    ["LastName"]=>
    string(10) "LASTNAME"
    ["Registered"]=>
    bool(true)
    ["URN"]=>
    string(7) "VALUE"
  }
}

This now means that the way I am trying to reference last name is wrong

What is the correct syntax?

Paul

6
  • Is there an typo in your example? There seems to be an opening [ but no closing Commented Oct 30, 2018 at 8:08
  • 1
    Hard to believe that's the actual var_dump output - that method does not tend to lie about the number of properties ((2), but only one property named LastName?) or the length of strings (string(10) vs "Test"? Granted, non-printable characters can be an issue, but in that case you usually see at least one whitespace before or after the actual value. Check the source code view of this debug output, not what your browser made of it when it interprets it as HTML.) Commented Oct 30, 2018 at 8:25
  • Sorry Dump is not lying I removed a load of other properties to simplify the question there is no issue with the service or the dump my question is why nothing is being extracted from the LastName property Commented Oct 30, 2018 at 8:27
  • I remember running into problems with this kind of stuff once. Try casting your value to string. I.e. echo (string) $obj->LastName;. Might work, cannot guarantee. Commented Oct 30, 2018 at 8:27
  • Nope that doesnt work Commented Oct 30, 2018 at 12:57

2 Answers 2

1

Use get_object_vars()

$obj = $client->Method($SoapCallParameters);
$obj = get_object_vars($obj);
var_dump($obj['result']->LastName);

that will give you the idea.

Sign up to request clarification or add additional context in comments.

6 Comments

This is just a different way of calling var_dump I am looking for getting to the value LastName and putting it into a variable
Now let me know what you are getting after var_dump this?
Please see main question - now it looks like the issue is with how do I get to the LastName property of the second element in the array which is where the result object is?
Exactly I know that, but if you will not provide output of my code, I will be unable to help you as I can create the data you are getting.
I added the output above I didnt want to put it in the comment as it is tidier in the main question
|
0

Example StdClass Object:

$obj = new stdClass();

$obj->foo = "bar";

Other example

echo $obj->foo; // -> "bar"

By variable's value:

$my_foo = 'foo';

echo $obj->{$my_foo};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.