7

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

4

4 回答 4

12

是的,您可以做到
示例 在Graph Api Explorer 中进行呼叫发布,将 url 设置为https://graph.facebook.com/me/photos, 添加带有键和值的字段“任何自定义消息” 添加另一个带有键和值的字段https://appharbor.com/assets/images/stackoverflow-logo.png 点击提交

message
url

于 2012-07-03T14:49:57.767 回答
1

您需要知道专辑 ID 并调用 POST 到:

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

您会发现进入相册并查看 URL 的相册 ID。将类似于https://www.facebook.com/media/set/?set=a.XXXXXXXXXXX.YYYY.ZZZZZZZZZZ&type=3

您的专辑 ID 是 XXXX。

于 2012-07-03T15:45:42.410 回答
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);
于 2013-03-03T07:38:19.613 回答
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);
于 2014-05-09T20:59:23.050 回答