Facebook API从URL上传照片

时间:2012-07-03 14:46:50

标签: facebook facebook-graph-api image-uploading

我有一个带照片库的网站,我想将每张照片(一张一张)上传到我的脸书页面(不是墙)。我设法发布了一条消息,但现在我想通过从服务器特定的URL(我不想再在本地上传)上传现有图像,将照片上传到FB页面墙。这可能吗?

4 个答案:

答案 0 :(得分:12)

是的,你可以做到这一点 例 在 Graph Api Explorer 中 拨打电话,将网址设置为https://graph.facebook.com/me/photos
使用键message添加字段并输入值“任何自定义消息”
添加另一个包含键url和值https://appharbor.com/assets/images/stackoverflow-logo.png的字段 点击提交

答案 1 :(得分:1)

您需要知道相册ID并拨打POST电话:

https://graph.facebook.com/albumid/photos?access_token=$access_token

您会发现相册ID进入相册并查看该网址。会像https://www.facebook.com/media/set/?set=a.XXXXXXXXXXX.YYYY.ZZZZZZZZZZ&type=3

您的相册ID是XXXX。

答案 2 :(得分:1)

这就是我使用的:

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true,
    'fileUpload' => true,
));

$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.

$facebook->setFileUploadSupport(true);

if($user == 0) {
    // If the user is not connected to your application, redirect the user to authentication page
    /**
     * Get a Login URL for use with redirects. By default, full page redirect is
     * assumed. If you are using the generated URL with a window.open() call in
     * JavaScript, you can pass in display=popup as part of the $params.
     * 
     * The parameters:
     * - redirect_uri: the url to go to after a successful login
     * - scope: comma separated list of requested extended perms
     */

    $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));

    echo ("<script> top.location.href='".$login_url."'</script>");

} else {
    // if the user is already connected, then fetch access_token and user's information or show some content to logged in user.
    try
    {
        $access_token = $facebook->getAccessToken(); // Gives you current user's access_token

        $user = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application.

    } catch(FacebookApiException $e){
        $results = $e->getResult();
        // Print results if you want to debug.
    }

}

$img = './upload/'.$image_path;
    $args = array(
       'message' => 'Some Message',
        'access_token'=>urlencode($access_token),
    );
    $args[basename($img)] = '@'.realpath($img);

    $ch = curl_init();
    $url = 'https://graph.facebook.com/me/photos';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);
    $response = json_decode($data,true);

答案 3 :(得分:0)

            $config = array('appId' => $config['App_ID'],'secret' => $config['App_Secret']);

            $facebook = new Facebook($config);

            // sets our access token as the access token when we call 
            // something using the SDK, which we are going to do now.
            $facebook->setAccessToken($access_token); 

            $page_id = "XXXXXXXXXXXXXXX";

            $page_access_token = "";

            $result = $facebook->api("/me/accounts");

            foreach($result["data"] as $page) {
                if($page["id"] == $page_id) {
                    $page_access_token = $page["access_token"];
                    break;
                }
            }

            $facebook->setFileUploadSupport(true);
            $photo = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
            $args = array(
                'access_token'  => $page_access_token,
                'message'       => "message here",
                'url' => $photo,
            );
            $post = $facebook->api("/$page_id/photos","post",$args);