0

I keep getting this error when I parse the trip advisor feed. Trying to pull up the username.

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

Here's my code with the link to the feed.

<?php
function getCode($url)
{
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
$html=getCode("http://api.tripadvisor.com/api/partner/1.0/location/258705/reviews");
$json = json_decode($html);
$cnt=0;
foreach ($json as $item)
{
    foreach($item as $row)
    {
        echo "Image Url:".$row->text."<br>";
        echo "ID:".$row->id."<br>";
        //username
        foreach($row->user as $row1)
        {
            echo $row1."<br>";

        }


    }
    echo "<br><br>";
}

?>

1
  • As long as you express a stdClass in string context (e.g. echo), you will get this error as string context needs strings to output (that can be object, but only those which implement _toString() which is not the case for stdClass. Commented May 25, 2014 at 18:51

2 Answers 2

1
print_r($row1);
stdClass Object ( [id] => [name] => Mahwah )

Try changing

$row1."<br>";

to

$row1->name."<br>";
Sign up to request clarification or add additional context in comments.

2 Comments

Not working for me. I get this error message now "Notice: Trying to get property of non-object"
Look - you can't echo an object. Get familiar with the structure of the data you're working with.
0

You need to set the assoc attribute to true on the json_decode function.

Try this:

$json = json_decode($html,true);

Read more about json_decode here


Also as a suggestion for your next stack overflow post, don't share API keys or other sensitive information.

5 Comments

There is no need to set that in this case.
Just realised that now, my point about him not sharing a partner only api key still stands though. haha
jup, should better just provide exemplary json instead of the key :) - eval.in/156628
@hakre Well atleast I got a free trip advisor key out of this xD (jk jk)
It's a test key anyway :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.