PHP - 从SOAP客户端请求访问返回的对象数组

问题描述 投票:0回答:5

我是PHP的新手。我正在开发一个项目,该项目向从java创建的WSDL发出soap客户端请求,并将来自java程序的响应作为List返回。我想从php中返回的数组对象访问该字符串,但我无法这样做。

请找到我在下面使用的代码 -

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

我为我的程序获得了以下var_dump结果。对不起,如果格式不正确。

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)        
    ....

我尝试了几种方法。令我困惑的是,如果我给计数($result->return)来访问该对象,它给出41,这是正确的。但是,如果我尝试在while循环中使用echo $result->return[$i]显示字符串相同的东西,我只得到一个空白页

对于你们这里的一些人来说,这听起来可能微不足道,但我从昨天起就一直在努力。任何帮助,将不胜感激。

php arrays list
5个回答
0
投票

插入htmlspecialchars()

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

如何链接:

// 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

请尝试在脚本的开头插入以下行:

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

我怀疑弦的尖括号是“问题”。浏览器尝试将它们解释为HTML标记,但失败然后隐藏它们。

发送另一种内容类型(如纯文本)将阻止浏览器解释输出。

您也可以尝试使用此方法,而不是我提供的第一个方法:

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

1
投票

您需要将对象转换为数组。我喜欢使用以下功能:

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

0
投票

尝试:

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

0
投票

您应该将属性结果解析为数组:

print_r((array)$result->return)

0
投票

我的版本

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

    }        
}
© www.soinside.com 2019 - 2024. All rights reserved.