0

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.

5 Answers 5

1

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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]);

7 Comments

Thank You so much for the reply. I am trying your solution now. I will let you know in a while.
I have tried both your method. The first one displays the strings in echo command itself. But all the HTML tags are also displayed along with it. I forgot to mention it you that I am using HTML as well. The second one displays the URL with the string and length attribute.
Is there any way to display the string as URL directly?
@user3025552 Sorry for the late reply. Please see my updated answer. (var_dump() is for debugging purposes, therefore it also display the string's length along with some quotes.)
Sorry about the very late reply, was outside.. It did work. It is now displaying the link as <http//wikipedai..> string. Is there any way to display the same string as a URL. Thanks in Advance!
|
0

Try:

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

Comments

0

You should parse property result to array:

print_r((array)$result->return)

Comments

0

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;
        }

    }        
}

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.