单击按钮将图像上传到Facebook

时间:2012-08-02 13:33:13

标签: php mysql facebook facebook-graph-api

我正在尝试使用PHP按钮点击将图片上传到Facebook。它似乎没有用。

HTML按钮代码

<a class="btn" href="test.php?upload=true">Click here to upload this image on your Facebook wall</a>

PHP代码

    <?php
        $act=isset($_GET['upload'])?$_GET['upload']:"";

        if($upload=='true'){

        set_time_limit(50);
        require 'facebook.php';

        // Create our Application instance (replace this with your appId and secret).
        $facebook = new Facebook(array(
                    'appId' => 'FB_APP_ID',
                    'secret' => 'FB_SECRET_KEY',
                ));

        // Get User ID
        $user = $facebook->getUser();
        if ($user) {

        } else {
            $loginUrl = $facebook->getLoginUrl();
            header('Location:' . $loginUrl . '&scope=user_photos,publish_stream');
        }
        ?>

        <?php
        // Login or logout url will be needed depending on current user state.
                if ($user) {
                    if (isset($_GET['upload'])) {
                ?>

                <?php
                        $facebook->setFileUploadSupport(true);
                        $args = array('message' => 'test');
                        copy('http://mysite.com/test.png', 'tmp/file.jpeg');
                        $args['image'] = '@' . realpath('tmp/file.jpeg');
                        $data = $facebook->api('/me/photos', 'post', $args);
                        unlink('tmp/file.jpeg');
                        //assigning users to tag and cordinates
                        $argstag = array('to' => $user);
                        $argstag['x'] = 40;
                        $argstag['y'] = 40;
                        $datatag = $facebook->api('/' . $data['id'] . '/tags', 'post', $argstag);
                        echo 'Success! Check your facebook wall now';
                    } else {
                ?>
                        <a href="test.php?upload=true">Click here to upload this image on your facebook wall</a><br/><br/>

        <?php
                    }
                }


        }
?>

注意:facebook.php是facebook PHP SDK附带的那个

1 个答案:

答案 0 :(得分:0)

将它放在try&amp; catch块中以查看会发生什么:

 // ...
 try {
    $facebook->setFileUploadSupport(true);
    $response = $facebook->api(
      '/me/photos/',
      'post',
      array(
        'message' => 'This is my image caption',
        'source' => '@/absolute/path/to/image' // @-sign must be the first character
      )
    );
  }
  catch (FacebookApiException $e) {
    echo "Error: ".$e->getMessage();
  }
  // ...

确保在“tmp /”(你的意思是“/ tmp /”?)中的呼叫复制后有一个有效的图像,并确保你拥有photo_upload权限。

https://developers.facebook.com/docs/reference/php/facebook-setFileUploadSupport/