Thursday, February 21, 2013

[PHP] Retrieving photos after you save it using facebook graph api.

In this tutorial, I will assume that you already create a code that will upload photos from your site to user's facebook account.
But if you're not and you're looking for that part, you can visit this tutorial
[PHP] Upload picture to facebook using Graph AP


Let's begin.

Things to have.
1. We need to know the title of the album where we want to get the photos. If you are done with the code on "how to upload picture to facebook using Graph API", for sure you have tested this and you know the title of the album where your uploaded photo goes.


Here are the steps we are going through.
1. We need to get the access token.
2. We need to get the id of the specific album we want to get photos.
3. We are going to get all the photos inside that album.


<?php

$app_id = "YOUR App ID/API Key";
$app_secret = "YOUR App Secret";

//Take NOTE: this must include http://
//ex. http://text2imagestatus.com/index.php
$post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE";

$code = $_REQUEST["code"];

//Obtain the access_token with publish_stream permission
if (!$code)
{
    $dialog_url= "http://www.facebook.com/dialog/oauth?"
    . "client_id=" . $app_id
    . "&redirect_uri=" . urlencode( $post_login_url)
    . "&scope=publish_stream,user_photos";
    echo("<script>top.location.href='" . $dialog_url
    . "'</script>");
}
else
{

    $token_url="https://graph.facebook.com/oauth/access_token?"
    . "client_id=" . $app_id
    . "&client_secret=" . $app_secret
    . "&redirect_uri=" . urlencode( $post_login_url)
    . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];


    //get the albums
    $get_album = "https://graph.facebook.com/me/albums?access_token=$access_token";
    $album = file_get_contents($get_album);
    $album = json_decode($album, true);

    //count the number of albums
    $count_album = count($album['data']);


    //loop until the title of the album reach
    for($i=0; $i<$count_album; $i++)
    {
        if($album["data"][0]["name"] == 'TITLE OF THE ALBUM')
        {
            $id_album = $album["data"][0]["id"];
            break;
        }
    }


    //get photos in the album
    $get_photo = "https://graph.facebook.com/$id_album/photos?access_token=$access_token";
    $photo = file_get_contents($get_photo);
    $photo = json_decode($photo, true);

    //count the number of photos
    $count_photo = count($photo['data']);

    //loop to get all the source url of photos
    for($i=0; $i<$count_photo; $i++)
    {
        $source = $photo["data"][$i]["source"];
        $printme .= "<img src='$source' /></br>";
    }
   
   //this will print the photos that we got from the album
   echo"$printme";

    //this will printout the array of albums and photos for you to see them
    echo '<pre>';
    print_r($album);
    print_r($photo);
    echo '</pre>';

}
?>


No comments:

Post a Comment