3

i have sometimes following error Fatal error: Cannot use object of type stdClass as array in.. with this function:

function deliciousCount($domain_name)
{
    $data = json_decode(
        file_get_contents(
            "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
        )
    );
    if ($data) {
        return $data[0]->total_posts;
    } else {
        return 0;
    }
}

$delic = deliciousCount($domain_name);

but this error happen sometimes only for specific domains any help?

5
  • Can you tell us an example of an url that "triggers" the error? Must be something where the response from delicious.com is valid json but not of the form [ ... ]. Commented Dec 13, 2011 at 8:46
  • Note: my assumption is that the only time the error was not triggered was if/when the request file_get_contents request was unsuccessful.. or whatever the case, json_decode will return false or null if there is nothing to decode; therefore, his function would return false thus evading the error. Commented Dec 13, 2011 at 8:53
  • domain is xcursionsindia.in sometimes happen sometimes no Commented Dec 13, 2011 at 8:56
  • This function of delicous.com is broken. For existing domains like google.de one time you'll get an entry ([{"url":...}]) and other times an empty object ({}). For no-existing domains like example.example [] or {}. Commented Dec 13, 2011 at 9:01
  • infact Saxoier you have right and so what is solution? Commented Dec 13, 2011 at 9:05

5 Answers 5

3

According to the manual, there is an optional second boolean param which specifies whether the returned object should be converted to an associative array (default is false). If you want access it as an array then just pass true as the second param.

$data = json_decode(
    file_get_contents(
        "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
    ),
    true
);
Sign up to request clarification or add additional context in comments.

Comments

2
function deliciousCount($domain_name) {
    $data = json_decode(
        file_get_contents(
            "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
        )
    );
    // You should double check everything because this delicious function is broken
    if (is_array($data) && isset($data[ 0 ]) &&
        $data[ 0 ] instanceof stdClass  && isset($data[ 0 ]->total_posts)) {
        return $data[ 0 ]->total_posts;
    } else {
        return 0;
    }
}

Comments

2

Before using $data as array:

$data = (array) $data;

And then simply get your total_posts value from array.

$data[0]['total_posts']

Comments

0

json_decode returns an instance of stdClass, which you can't access like you would access an array. json_decode does have the possibility to return an array, by passing true as a second parameter.

Comments

-1
function deliciousCount($domain_name)
{
    $data = json_decode(
        file_get_contents(
            "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
        )
    );
    if ($data) {
        return $data->total_posts;
    } else {
        return 0;
    }
}

$delic = deliciousCount($domain_name); 

or

function deliciousCount($domain_name)
{
    $data = json_decode(
        file_get_contents(
            "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name",true
        )
    );
    if ($data) {
        return $data['total_posts'];
    } else {
        return 0;
    }
}

$delic = deliciousCount($domain_name);

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.