The following on the attachment below are the page feeds with its comments returned by facebook PHP sdk and Graph API.
What I want here is to get number of comments per user on each feed.

Here are the codes

$helper=new FacebookRedirectLoginHelper($url);
            try{
                $session=$helper->getSessionFromRedirect();
                if(isset($session))
                {                    
                    $request=(new FacebookRequest($session,"GET","/357971644373038/feed"))->execute()->getGraphObject()->asArray();                                        
                    $comment=$request["data"];
                    $counter=count($comment,1);
                    for($index=0; $index<$counter; $index++)
                    {
                        echo $request["data"][$index]->id."<br>";
                        echo $request["data"][$index]->message."<br><br>";
                        echo $this->get_stream(array("session"=>$session,"idfeed"=>($request["data"][$index]->id)))."<br><br>";
                    } 
//                  print_r($request);
                }
                else{
                    $permision=array("scope"=>"email,publish_actions");                   
                    echo "<a title=".$helper->getLoginUrl($permision)." href='".$helper->getLoginUrl($permision)."'>Login here</a>";
                }
            } 
            catch(Exception $ex) 
            {

            }
        }

        private function get_stream($data)
        {
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {
                //$count=0;
                foreach($request["data"] as $result)
                {
                    return json_encode($request["data"]);
                }
//                return json_encode($request["data"]);
            }
        }

Please I need your help guys....

Recommended Answers

All 20 Replies

Is it real there is no expert here?

You apparently have an array with comments. Just loop them and increment a counter for every user you find in that particular feed.

You apparently have an array with comments. Just loop them and increment a counter for every user you find in that particular feed.

Ok thank you but can you show me interms of codes?

I am unsure why get_stream() returns JSON, it would be easier to return the array instead. So change that first:

return $request["data"];

Then show me exactly how the returned array looks by using:

print_r($this->get_stream(array("session"=>$session,"idfeed"=>($request["data"][$index]->id))));

I am unsure why get_stream() returns JSON, it would be easier to return the array instead. So change that first:

return $request["data"];
Then show me exactly how the returned array looks by using:

print_r($this->get_stream(array("session"=>$session,"idfeed"=>($request["data"]>[$index]->id))));

I have changed the codes like this below

private function get_stream($data)
        {    
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {                
                foreach($request["data"] as $key=>$result)
                {                    
                    $name=array($key=>$result->from->name);                  
                    print_r($name);
                }                
//                echo(var_dump($name));

            }
        }

Here below on the attachment is the results that I get

In your foreach you can now do something like this:

if (in_array($name, $feedsPerName))
    $feedsPerName[$name]++;
else
    $feedsPerName[$name] = 1;

After changing my code into this one

private function get_stream($data)
        {       
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {    
                $number=0;
                foreach($request["data"] as $key=>$result)
                {                    
                    $name=array($key=>$result->from->name);
                    if(in_array($name[$key],$name))
                    {                        
                        $name[$key]++;                        
                    }
                    else{
                        $name[$key]=1;
                    }                    
                    echo $name[$key]."<br>";                    
                }                
            }
        }

I get the following results, see the attachment.

The next attachment just after using print_r()

You are not using what I posted.

Ooh! I'm sory but I get confused when you say $feedsPerName.
is it right to put into this way?

private function get_stream($data)
        {       
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {          
                foreach($request["data"] as $key=>$result)
                {                    
                    $name=array($key=>$result->from->name);
                    if(in_array($name,$result->from->name))
                    {                        
                        $result->from->name[$name]++;                        
                    }
                    else{
                        $result->from->name[$name]=1;
                    }                    
                    print_r($result->from->name);
                }                
                //echo(var_dump($name));                
            }
        }

But I get kind of errors, see the attachment

Ooh! I'm sory but I get confused when you say $feedsPerName.

It is a new array to hold the number of replies per user, so I don't know why you didn't just try what I posted.

It is a new array to hold the number of replies per user, so I don't know why you didn't just try what I posted.

Please can you show me how to construct this $feedsPerName from my codes below

private function get_stream($data)
        {    
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {                
                foreach($request["data"] as $key=>$result)
                {                    
                    $name=array($key=>$result->from->name);                  
                    print_r($name);
                }
            }
        }

Add it after line 8.

Just something like this ?

private function get_stream($data)
        {       
            $feedsPerName=array();
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {          
                foreach($request["data"] as $key=>$result)
                {                    
                    $name=array($key=>$result->from->name);
                    if(in_array($name,$feedsPerName))
                    {                        
                        $feedsPerName[$name]++;                        
                    }
                    else{
                        $feedsPerName[$name]=1;
                    }                    
                    print_r($feedsPerName);
                }                                
            }
        }

Yes, but you can move line 17 after line 18, so it is outside of the foreach loop.

$feedsPerName=array();
            $request=(new FacebookRequest($data["session"],"GET","/".$data["idfeed"]."/comments"))->execute()->getGraphObject()->asArray();
            if(isset($request["data"]))
            {          
                foreach($request["data"] as $key=>$result)
                {                    
                    $name=array($key=>$result->from->name);
                    if(in_array($name,$feedsPerName))
                    {                        
                        $feedsPerName[$name]++;                        
                    }
                    else{
                        $feedsPerName[$name]=1;
                    }                 
                }                
                print_r($feedsPerName);
            }

After running it I get the following, see the attachment

That error on line 62 is equivalent to line 13 on the codes above

Line 7 should be:

$name = $result->from->name;

Line 7 should be:

$name = $result->from->name;

I get the same results after changing the codes

if(in_array($name,$feedsPerName))

should be

if(isset($feedsPerName[$name]))

I hate the trial and error, but am nowhere near a computer running PHP atm.

I get this error

Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in F:\Webserver\htdocs\feedreader\creader.php on line 57

Show your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.