RakeshD
RakeshD

Reputation: 7

PHP - accessing a returned object array from a SOAP client request

I am very new to PHP. I am working on a project that makes a soap client request to a WSDL created from java and returns the response from the java program as List. I want to access the string from the returned array object in php, but am not able to do so.

Please find the code that I have used below -

$client = new SoapClient("http://rakesh-pc:8080/WikiEdit/wikiSearchService?wsdl");
$user = $_SESSION['user'];
$params = array(
"arg0" => $user,
);
$result = $client->wikiFind($params);
var_dump($result);

I am getting the following var_dump result for my program. Sorry, if it is not properly formatted.

object(stdClass)[2]                                                                     
    public 'return' => 
    array (size=41)                                                                         
    0 => string '<http://en.wikipedia.org/wiki/Anarchism>' (length=40)    
    1 => string '<http://en.wikipedia.org/wiki/Red_and_Anarchist_SkinHeads>' (length=58)    
    2 => string '<http://en.wikipedia.org/wiki/Red_and_Anarchist_Skinheads>' (length=58)     
    3 => string '<http://en.wikipedia.org/wiki/Anti-statism>' (length=43)                   
    4 => string '<http://en.wikipedia.org/wiki/Anarcho-capitalism>' (length=49)            
    5 => string '<http://en.wikipedia.org/wiki/Anarcho-Capitalism>' (length=49)           
    6 => string '<http://en.wikipedia.org/wiki/Individualist_anarchism>' (length=54)        
    7 => string '<http://en.wikipedia.org/wiki/Individualist_Anarchism>' (length=54)        
    ....

I have tried several ways. The thing which is confusing me is that if I give count($result->return) to access the object, it is giving 41, which is correct. But if I try the same thing to display the string using echo $result->return[$i] in a while loop, I am getting only a blank page

This may sound trivial to some of you guys here, but I have been struggling with it from yesterday. Any help would be appreciated.

Upvotes: 0

Views: 3676

Answers (5)

Alejandro Aranda
Alejandro Aranda

Reputation: 749

My version

function objectToArray($data) {
    if (is_object($data)&& !is_array($data)) {
        $array = array();            
        $array[0] = $data;
        return $array;
    }else{
        if (empty($data)){
            return array();
        }else{
            return $data;
        }

    }        
}

Upvotes: 0

tibc-dev
tibc-dev

Reputation: 983

You need to convert your object to an array. I like to use the following function:

function objectToArray( $object ){

    if( !is_object( $object ) && !is_array( $object ) ){
           return $object;
    }

    if( is_object( $object ) ){
        $object = get_object_vars( $object );
    }

    return array_map( 'objectToArray', $object );
}

$myResultArray = objectToArray($result);
var_dump($myResultArray);
echo $myResultArray[0];

Upvotes: 1

ComFreek
ComFreek

Reputation: 29424

Solution

Insert htmlspecialchars():

echo htmlspecialchars($result->return[$i], ENT_HTML5);

How to linkify:

// Grab the raw URL
$url = substr($result->return[$i], 1, -1);
// Echo <a> incl. the text
echo "<a href='$url'>", echo htmlspecialchars($result->return[$i], ENT_HTML5), "</a>";


Explanation of the problem

Please try inserting the following line at the beginning of your script:

header('Content-type: text/plain');

I suspect that the angular brackets of the strings are the "problem". The browser tries to interpret them as HTML tags, but fails and then hides them.

Sending another content-type such as plain text will prevent the browser from interpreting the output.

You can also try this method instead of the first one I provided:

var_dump($result->return[$i]);

Upvotes: 0

PKolos
PKolos

Reputation: 402

You should parse property result to array:

print_r((array)$result->return)

Upvotes: 0

user1431775
user1431775

Reputation:

Try:

$result =  (array)$client->wikiFind($params);
print_r($result);

Upvotes: 0

Related Questions