Stan
Stan

Reputation: 26511

Facebook API - Get image URL after I upload it

I would like to know how to get image url or any other data besides id when I upload it to facebook via graph API. I tried to get image data from Graph API Explorer but it gives me false. When I put my access_token with user_photos it gives me details. However, default access token that I can get from $facebook->getAccessToken(); still gives me false.

So my question is - How can I get image data after I upload it to facebook?

Here is my code:

<?php

require 'fb/src/facebook.php';

if ($_FILES["file"]["error"] == 0)
{
    // Getting
    $file = $_FILES["file"];
    $fileExtension = $imgExtension = end(explode(".", $file["name"]));
    $upload_dir = "upload/";

    // Modifying
    $newName = md5(uniqid()) . "." . $fileExtension;

    // Checking if this name already exists
    while (file_exists($upload_dir . $newName) == true)
    {
        $newName = md5(uniqid()) . "." . $fileExtension;
    }

    // Uploading
    if (move_uploaded_file($file['tmp_name'], $upload_dir . $newName) == true)
    {
        $facebook = new Facebook(array(
                    'appId' => 'app id',
                    'secret' => 'app secret',
                ));

        $facebook->setFileUploadSupport(true);
        $args = array(
            "message" => "test caption"
        );

        $args['image'] = '@' . realpath($upload_dir . $newName);

        $data = $facebook->api('/me/photos', 'post', $args);
        print_r($data);
    }
    else
    {
        echo "Error.";
    }
}

Upvotes: 3

Views: 4264

Answers (1)

Juicy Scripter
Juicy Scripter

Reputation: 25938

You should get the id of uploaded photo in the response ($data in your case), simple call to Graph API with that id should return you all the desired info.

$data = $facebook->api('/me/photos', 'post', $args);
$photo_data = json_decode($data, true);
$photo = $facebook->api('/'.$photo_data['id']);

$thumbnail = $photo['picture'];
$large_photo = $photo['source'];
$photo_page = $photo['link'];

Update: You should require user_photos to read photo object details.

Upvotes: 2

Related Questions