How to display friends photo from the facebook?

May 25, 2013 | Rishabh Agrawal | General

1. Download the facebook sdk zip file from this link and extract it;

2. create a new folder “fb”.

3. Copy the file from the downloaded sdk src folder and paste it in the fb folder.

4. now add the following code to display the images of your facebook friends with pagination..

5. Just copy and paste the below code which check the facebook authentication and show the images

<?php
$appid          = "xxxx"; //facebook app id
$appsecret      = "xxxxx"; //facebook app secret
$scriptPath         = "http://example.com"; //path where script are..
$friendsPerPage     = 12; //number of friends per page

if (!class_exists("FacebookApiException")) {
    require_once("facebook.php" ); //include facebook api
}
        $facebook = new Facebook(array(
          "appId" => $appid,
          "secret" => $appsecret,
        ));

        $fbuser = $facebook->getUser(); // get the user session
        if ($fbuser) // check user exists or not

 {
          try {
            $access_token = $facebook->getAccessToken();
            $me = $facebook->api("/me");
            } catch (FacebookApiException $e) {
            die($e->getMessage());

          }
        }

        // Show login form if fresh login requires
        if (!$fbuser) //if user not exist then send to the  login page

{
         $loginUrl = $facebook->getLoginUrl(array("redirect_uri"=>$scriptPath));
         die("Login");
        }

        $offsetValue="";
        $previousurl="";
        $nexturl="";

// offset is used as a token which used to limit the no. of friends
        if(isset($_GET["offset"]) && is_numeric($_GET["offset"]))
        {
            $offsetValue="&offset=".$_GET["offset"];
        }
// limit the friends according to  the offset

        $friends = $facebook->api("/me/friends?limit=".$friendsPerPage.$offsetValue);


        if(isset($friends["paging"]["next"]))
        {
            $nexturlOffset = returnFbOffset($friends["paging"]["next"]);
            $nexturl = "Next »";
        }
        if(isset($friends["paging"]["previous"]))
        {
            $previousurlOffset = returnFbOffset($friends["paging"]["previous"]);
            $previousurl = "« Previous";
        }
?>





<?php
    echo "
“.$previousurl.” | “.$nexturl.”

“;
echo ”

      • “;
      • foreach ($friends[“data”] as $friend) {
      echo ”

    • “;
      echo “”;
      echo ”

      “.$friend[“name”].”

      “;
      echo “

“;
}
echo ”

“;
echo ”

“;

function returnFbOffset($url)
{
$str = parse_url($url); //exploded the url into array
parse_str($str[“query”], $strarray);

/*
parse_str(str,arr);

parse_str: Parses the string into variables
str : input string
arr: variables are stored in this array
*/
return $strarray[“offset”]; // retuning offset value by reading the url
}

?>

7. You can view the


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal