2012-06-27 13 views
0

我有一個擁有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,但沒有在行爲改變任何東西,我重定向到同一頁面,而無需返回。

謝謝。

+0

你試過了一個'header('Location:to_file.php');'某處?我能想到的,我不是一個職業球員,但我確實得到了。 –

+0

我被重定向到一個graph.facebook.com網址,所以我無法將代碼添加到該頁面。 –

+0

嘗試將您的重定向網址添加到表單中作爲隱藏的輸入字段。如果這沒有幫助,那麼我想你必須將照片上傳到_your_服務器,並從那裏發佈服務器端API調用。 – CBroe

回答

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

+0

謝謝(戶田raba :))的幫助,我想我會嘗試Ajax技術。我明白這個例子裏發生了什麼,但是我還有一個問題,如果你能幫忙:如何從Ajax請求中調用的PHP文件中發送兩個表單字段('message'和'file')到Facebook的URL?我會通過'$ _FILES ['source']'和'$ _POST ['message']'得到它們,但是我將如何將它們轉移到'https://graph.facebook.com/「$ ALBUM_ID。」/照片?「 。」access_token =「。$ access_token'?謝謝! –

+0

還有一件事,在Ajax示例中,他們在輸入文件正在更改時啓動表單提交。我只想在用戶按下上傳按鈕時提交和發起請求,我該怎麼做(我喜歡用調用帶有onsubmit事件的JavaScript來完成此操作,這將啓動xmlhttprequest,這是否可能?)再次感謝! –

+0

而不是聆聽這個改變,即使是聽你的dom中的另一個事件,比如點擊按鈕($(「#button_id」)。bind(「click」,function ...)。你可以使用這個ajax技術來發布圖片直接到facebook,無需通過你的服務器 –

相關問題