0

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....

Edited by Hilal2009

Attachments Capture.JPG 76.16 KB
2
Contributors
20
Replies
64
Views
2 Years
Discussion Span
Last Post by pritaeas
0

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

0

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?

0

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))));
0

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

Attachments Capture.JPG 40.73 KB
0

In your foreach you can now do something like this:

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

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()

Edited by Hilal2009

Attachments Capture.JPG 39.62 KB Capture.JPG 25.33 KB
0

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

0

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.

0

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);
                }
            }
        }
0

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);
                }                                
            }
        }
0
$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

Edited by Hilal2009

Attachments Capture.JPG 53.09 KB
0

Line 7 should be:

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

I get the same results after changing the codes

0
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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.