今天,用php编程遇到了一个问题

echo $result;

出现错误:

Catchable fatal error: Object of class stdClass could not be converted to string

用var_dump($result)

object(GetInfoByIPResponse)#3 (1) {
[“GetInfoByIPResult”]=>
object(stdClass)#4 (1) {
[“string”]=>
array(3) {
[0]=>
string(93) “本web服务由www.365.net免费提供;请访问:http://www.365.net 获取更多web服务”
[1]=>
string(12) “210.77.19.29”
[2]=>
string(27) “中国科学院研究生院”
}
}
}

然后这么调用,就成功了:

printf($result->GetInfoByIPResult->string[2]);

输出:

linhui@59:~/Desktop/mashup/wsdl2php-0.2.1/bin$ php test.php
中国科学院研究生院linhui@59:~/Desktop/mashup/wsdl2php-0.2.1/bin$

我上google查了,有的说要改php版本,有的说我脚本写错了,现在我发现,这个$result确实是一个对象,如果你想使用里面的东西,确实是需要另外一种形式。

这个问题困扰了我好长时间,终于在网上找到一个文章,才把问题解决了。

那篇文章如下:

ok, today i was working on a script and i got this error when i tried to display the information from an array.

now, i am not an expert on arrays, but ususally i can figure them out, but this error i’ve never seen.

at the end of the script there was this variabled: $response

so when i echo it, it didn’t show anything,
echo $response;

then i thought, maybe its an array so i put this code:
echo “<pre>”;
var_dump($response);
echo “</pre>”;

it worked, i got these results on my browser: OUTPUT:
object(stdClass)#6 (9) {
[“Timestamp”]=>
string(20) “2007-07-18T04:50:52Z”
[“Ack”]=>
string(7) “Success”
[“CorrelationID”]=>
string(13) “28c01821ba2c2”
[“Version”]=>
string(8) “2.400000”
[“Build”]=>
string(6) “1.0006”
[“Amount”]=>
object(stdClass)#7 (2) {
[“_”]=>
string(5) “40.00”
[“currencyID”]=>
string(3) “USD”
}
[“AVSCode”]=>
string(1) “X”
[“CVV2Code”]=>
string(1) “M”
[“TransactionID”]=>
string(17) “14E028078XJ827961UX”
}

ok, i didn’t want to show this to my users, instead, i wanted to show only the TransactionID part so i put this code instead:
echo $response[‘TransactionID’][17];

that’s when i got this error:

Fatal error: Cannot use object of type stdClass as array in C:windows-htdocs-cart.php on line 535

ok, so how do you fix this error,

i looked it up on google and found some post and forums where it say to upgrade my php or change the softwar or that i have a bad script. well, i finally figured out. it turns out that $response is actually an object. i have no idea what are objects in PHP but i came across an article about ojects and i found out that if you want to display it or show it with echo or print, you actually have to use the a different format. so in my example above, if i wanted to show the value TransactionID, this is how i would do it:

CODE:
echo $response->TransactionID;

OUTPUT:14E028078XJ827961UX

Perfect, just how i wanted.

well i hope this helps someone.

Thanks to webune.com for helping with this question. i have a dedicated server with them and they are very helpful with their support.