Hey

When I try to interpret thru PHP, a Java web service that returns a boolean type, this error (more like a warning) pops up:

Notice: Object of class stdClass could not be converted to int in C:\testing\page.php on line 29

How can I make the PHP page understand true and false? Or at least recieve true or and false, if neccesary convert them to 1 and 0 and understand that? No, I cant change the web service to send 1 or 0, sorry.

Dani AI

Generated

Short diagnosis and recommended approach.

When PHP calls a Java SOAP service the result is often wrapped as a generic PHP object (stdClass) rather than a raw scalar; that wrapper must be inspected and the inner value extracted before any numeric or boolean cast is attempted. (php.net)

Practical steps to fix the warning (inspect → extract → convert):

  1. Dump the raw response to learn the property name (var_dump or print_r).
  2. Pull the scalar out of the stdClass (common names are return or the method name).
  3. Normalize the value to a true boolean using FILTER_VALIDATE_BOOLEAN, then convert to int if needed.

Example pattern:

$response = $client->SomeMethod($params);
var_dump($response); // inspect structure

// extract the first scalar value from the returned object
if (is_object($response)) {
    if (isset($response->return)) {
        $val = $response->return;
    } else {
        $arr = get_object_vars($response);
        $val = reset($arr); // first property value
    }
} else {
    $val = $response;
}

// parse boolean reliably, then turn into 0/1
$bool = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($bool === null) {
    // fallback to loose cast if format was unexpected
    $bool = (bool) $val;
}
$int = $bool ? 1 : 0;

Using filter_var(..., FILTER_VALIDATE_BOOLEAN) is safer than boolval() for string values like "true"/"false" because it recognizes common textual forms (on/off, yes/no, 1/0) in a case-insensitive way. Use var_dump (or __getLastResponse with the SoapClient trace option) to see the raw SOAP payload when debugging. (php.net)

This follows the hints from (check object properties) and (cast after extracting). The reliable sequence is: inspect the returned stdClass, extract the scalar property, then convert that scalar to boolean/int.

Recommended Answers

All 5 Replies

What is the definition for stdClass ? Perhaps you need to use a property of this class.

What is the definition for stdClass ? Perhaps you need to use a property of this class

Never in my life have I seen "stdClass" (meaning it is not in my code and I did not write it) so It has to be a base class of Java.

Googling it a bit, it seems "stdClass" is something from PHP....

Maybe you should try type-casting it into "bool" or an "int". As stdClass is rather a dynamic Object. I think it would do some good.

Solved in this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.