Getting Facebook comments count via FQL I'm currently getting the Facebook comments count over a query request to the Facbeook api.

The problem is, there are about 15 queries on each site which makes it real slow to load (up to 6 seconds)

The next step is to get the comments count from the array object, as there's no native PHP function for that i wrote my own.

Is there any possibility to speed things up?

Is there something wrong with my code, which makes it slow, or is this up to the facebook query request?

Here's my code:

function objectToArray($object)
{
    if(!is_object($object) &amp;&amp; !is_array($object))
    return $object;

    $array=array();
    foreach($object as $member=&gt;$data)
    {
        $array[$member]=objectToArray($data);
    }
    return $array;
}

function count_comments($url) 
{
    $fql  = "SELECT commentsbox_count FROM ";
    $fql .= "link_stat WHERE url = '".$url."'";

    $apifql='https://api.facebook.com/method/fql.query?format=json&amp;query='.urlencode($fql);
    $json=file_get_contents($apifql);
    $obj = json_decode($json);
    $obj= objectToArray($obj);
    $obj = $obj[0]['commentsbox_count'];

    if($obj == 1)
    {
        echo '1 comment';   
    }
    else if($obj == 0)
    {
        echo 'No comments yet';
    }
    else
    {
        echo $obj.' comments';
    }
}


Here I'm calling the function:

count_comments('http://example.com'.$data["id"].'');


Thanks in advance for any hints, tips and solutions!
Please use braces {} to wrap your code, even those one line if statements, makes it so much easier to read.

First off, you do know that JSON has an option to output an array right? json_decode( $json, TRUE ) returns an array. No need to create a function for it.

"there's no native PHP function for that" - If it uses the countable interface, which is any data object I've come across (XML, JSON, SQL, etc...) then you can just use count(). If you have an object that holds the comments I would just try using count() on that first before creating a new query just to get a count.

Make sure you are not calling the "same" object twice. I imagine you can just get the whole table into an array then perform array operations on it to get the desired results without having to query the server multiple times. This is probably the biggest reason your program runs so slow. Hope this helps!