Php 将图像张贴到墙上后重定向回我的网页

Php 将图像张贴到墙上后重定向回我的网页,php,facebook,facebook-graph-api,facebook-php-sdk,Php,Facebook,Facebook Graph Api,Facebook Php Sdk,我有一个具有Facebook功能的网站(发送个人信息、张贴到墙上)。我有一个页面,上面有一个表单,可以向用户发布图片墙上的照片相册 我的问题是,在发布图像(发布表单)后,我被重定向到此url: https://graph.facebook.com/“相册ID”/photos?访问令牌=AAAEzcP64ySABAA3YYxBzRlrtUn………… 在浏览器中,我看到了以下内容:(为了安全起见,我删除了这些号码) 我怎样才能告诉Facebook让我重新回到我的页面 我的代码是: $app_id =

我有一个具有Facebook功能的网站(发送个人信息、张贴到墙上)。我有一个页面,上面有一个表单,可以向用户发布图片
墙上的照片
相册

我的问题是,在发布图像(发布表单)后,我被重定向到此url:
https://graph.facebook.com/“相册ID”/photos?访问令牌=AAAEzcP64ySABAA3YYxBzRlrtUn…………

在浏览器中,我看到了以下内容:(为了安全起见,我删除了这些号码)

我怎样才能告诉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
$app\u id=app\u id;
$app\u secret=app\u secret;
$my\u url=“my\u url”;
$page\u id=“page\u id”//将此设置为应用程序的应用程序\u ID
$code=$_请求[“code”];
if(空($code)){
//获取用户发布到其页面的权限。
$dialog_url=”http://www.facebook.com/dialog/oauth?client_id="
.$app\u id.&redirect\u uri=“.urlencode($my\u url)
.“&scope=publish\u stream,manage\u pages”;
echo('top.location.href=“”.$dialog_url.“;”);
}否则{
//获取用户的访问令牌,以便获取/me/accounts
$token_url=”https://graph.facebook.com/oauth/access_token?client_id="
.$app\u id.&redirect\u uri=“.urlencode($my\u url)
.“&client_secret=”.$app_secret
.“&code=”.$code;
$access\u token=file\u get\u contents($token\u url);
$accounts\u url=”https://graph.facebook.com/me/accounts?“$access_令牌;
$response=file\u get\u contents($accounts\u url);
//解析返回值并获取我们拥有的帐户数组
//对的访问。这在数据[]数组中返回。
$resp_obj=json_decode($response,true);
$accounts=$resp_obj['data'];
//查找要将视频发布到的页面的访问令牌。
foreach($accounts作为$account){
如果($account['id']=$page\u id){
$access_token=$account['access_token'];
打破
}
}
$ALBUM\u ID=“ALBUM\u ID”;
//向用户显示照片上载表单并发布到图形URL
$image_post=”https://graph.facebook.com/“$ALBUM_ID。”/photos?”
“access_token=”.$access_token;
$video_post=”https://graph-video.facebook.com/“$page\u id。”/视频?”
“title=testTitle”。&description=testDescription”
.“&access_-token=”.$access_-token;
?>

在我看来,您有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);
}

...
功能位置更改(ifrm){
log(“iframe位置已更改为:”,ifrm.src);
}
问题是,如果上传失败,您将无法察觉,这意味着您无法区分成功上传和失败上传。
无法从iframe读取数据的原因是,页面的域与iframe的域不同,在这种情况下,由于同源策略,浏览器会阻止通信

2.将图像上载到服务器,然后从php将图像上载到facebook。
您可以使用以下命令:


3.您可以使用ajax上传图像,有一些技术,例如:。

您是否尝试了
标题('Location:to_file.php')
某个地方?我所能想到的是,我不是专业人士,但我过得去。我被重定向到graph.facebook.com的URL,因此我无法向该页面添加代码。尝试将重定向URL添加到表单中作为隐藏的输入字段。如果这没有帮助,那么我想你必须将照片上载到服务器,然后从那里发布,并进行服务器端API调用。“我被重定向到graph.facebook.com URL”-你没有被重定向到那里-你自己说那是浏览器应该发送表单数据的地址。这与重定向不同。@CBroe我明白了,那么没有
action=$image\u post
标记,如何将表单数据发送到facebook URL?谢谢(Toda raba:))为了获得帮助,我想我会尝试Ajax技术。我了解示例中的情况,但如果您能提供帮助,我还有另一个问题:如何从Ajax请求中调用的PHP文件发送两个表单字段(
消息
文件
)到Facebook URL?我会通过
$\u文件['source']
$\u POST['message']
获取它们,但我如何将它们传输到
https://graph.facebook.com/“$ALBUM\u ID。”/photos?”“访问\u令牌”=“$access\u token
?谢谢!还有最后一件事,在Ajax示例中,他们在更改输入文件时启动表单提交。我只想在用户按下上载按钮时提交并启动请求,我该怎么做(我更喜欢使用onsubmit事件调用javascript,这将启动xmlhttprequest,可能吗?)再次感谢!不要听更改,甚至可以听dom中的另一个事件,例如按钮单击($(“#按钮_id”).bind(“单击”,函数…)。您可以使用此ajax技术将图像直接发布到facebook,无需通过服务器。非常感谢您的帮助,最终我制作了一个不可见的
iframe
post。这似乎是最好的解决方案。
<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);
}