将图像发布到我的墙后重定向回我的网页

时间:2012-06-27 16:47:30

标签: php facebook facebook-graph-api facebook-php-sdk

我有一个拥有Facebook功能的网站(发送个人信息,张贴到墙上)。我有一个页面,其中包含将图像发布到用户wall photos相册的表单。

我的问题是,在发布图片(发布表单)后,我被重定向到此网址: https://graph.facebook.com/'ALBUM_ID'/photos?access_token=AAAEzcP64ySABAA3YYxBzRlrtUn..............

在浏览器中我看到了这一点:(为了安全起见,我删除了数字)

{
   "id": "ID NUMBER HERE", 
   "post_id": "POST ID NUMBER HERE"
}

如何告诉Facebook重新回到我的页面。

我的代码是:

$app_id = APP_ID;
$app_secret = APP_SECRET;
$my_url = "MY_URL";
$page_id = "PAGE_ID"; // Set this to your APP_ID for Applications

$code = $_REQUEST["code"];

if(empty($code)) {
  // Get permission from the user to publish to their page. 
  $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=publish_stream,manage_pages";
  echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {

  // Get access token for the user, so we can GET /me/accounts
  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
      . $app_id . "&redirect_uri=" . urlencode($my_url)
      . "&client_secret=" . $app_secret
      . "&code=" . $code;
  $access_token = file_get_contents($token_url);

  $accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
  $response = file_get_contents($accounts_url);

  // Parse the return value and get the array of accounts we have
  // access to. This is returned in the data[] array. 
  $resp_obj = json_decode($response,true);
  $accounts = $resp_obj['data'];



  // Find the access token for the page to which we want to post the video.
  foreach($accounts as $account) {
       if($account['id'] == $page_id) {
         $access_token = $account['access_token'];
         break;
       }
  }

  $ALBUM_ID = "ALBUM_ID";

      // Show photo upload form to user and post to the Graph URL
      $image_post = "https://graph.facebook.com/" . $ALBUM_ID . "/photos?"
      . "access_token=" .$access_token;

        $video_post = "https://graph-video.facebook.com/" . $page_id . "/videos?"
      . "title=testTitle" . "&description=testDescription"
      . "&access_token=". $access_token;

?>
  <table align="center">
             <tr>                                                                                                 
                 <td>

                     <?php
         echo '<html><body>';
         echo '<form enctype="multipart/form-data" action="'
         .image_post.' "method="POST">';
         echo 'Please choose a photo: ';
         echo '<input name="source" type="file"><br/>';
         echo 'Say something about this photo: <br/>';
         echo '<textarea id="fbText" name="message" 
             rows="4" cols="47">';
         echo '</textarea><br/><br/>';
         echo '<input type="submit" value="Upload"/><br/>';
         echo '</form>';
         echo '</body></html>';
      }
          ?>
                 </td>

             </tr>
  <?php

我已尝试将&redirect_uri=" . urlencode($my_url)添加到$image_post,但这并未改变行为中的任何内容,并将我重定向到同一页面而不返回。

感谢。

1 个答案:

答案 0 :(得分:1)

我看到你有3个选项:

1。这只有在您不关心从Facebook回复的响应时才有效:

<iframe name="uploader" onLoad="locationChange(this)"></iframe>

<form method="POST" action="..." target="uploader">
...
</form>

funnction locationChange(ifrm) {
    console.log("iframe location has changed to: ", ifrm.src);
}

问题在于,如果上传失败,您将无法识别,这意味着您无法区分成功上传和失败上传。
您无法从iframe中读取数据的原因是您的网页与iframe具有不同的域名,并且由于相同的原始政策,浏览器在这种情况下会阻止通信。

2。将图像上传到您的服务器,然后从php将图像上传到Facebook 您可以使用此:Upload Photo To Album with Facebook's Graph API

3。您可以使用ajax上传图像,有一些技巧,例如:Ajax Image Upload without Refreshing Page using Jquery