2011-03-02 82 views
1

新的PHP請原諒我愚蠢的問題。Facebook:php上傳照片和張貼在牆上

我正在創建我的第一個fb應用程序。它允許用戶瀏覽他們的本地驅動器並選擇一張照片。一旦提交,它將重定向到下一頁並處理到存儲到我的服務器上,然後張貼到用戶的牆上。

這個應用程序並沒有太多的工作。用戶瀏覽和將照片存儲到我的服務器上的部分正在工作,但無法從我的服務器中取回照片並將其張貼到用戶的牆上。

的config.php:

<?php 
require_once 'facebook.php'; 

$app_id = ""; 
$app_key = ""; 
$app_secret = ""; 
$canvas_url = ""; 

$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
'cookie' => true 
)); 

$session = $facebook->getSession(); 

if (!$session) { 

     $url = $facebook->getLoginUrl(array(
     'canvas' => 1, 
     'fbconnect' => 0, 
     'req_perms' => 'publish_stream, user_photos, read_stream, read_friendlists' 
     )); 

     echo "<script type='text/javascript'>top.location.href = '$url';</script>"; 

    }//end if session user 
else 
{ 

     try { 

     $uid = $facebook->getUser(); 
     $me = $facebook->api('/me'); 

     $updated = date("l, F j, Y", strtotime($me['updated_time'])); 

     echo "Hello " . $me['name'] . "<br />"; 
     echo "You last updated your profile on " . $updated . "<br />" ; 
     echo "<img src='https://graph.facebook.com/".$uid."/picture'/>"; 
     }//end try getUser 
     catch (FacebookApiException $e) { 

     echo "Error:" . print_r($e, true); 

     }//end catch getUser 
}//end else user 

index.php文件包含表單:

<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 
</form> 

uploader.php運行過程

$target_path = "uploads/"; 
     $target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

     if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
      echo "The file ". basename($_FILES['uploadedfile']['name']). 
      " has been uploaded" . "<br />"; 
     } else{ 
      echo "There was an error uploading the file, please try again!" . "<br />"; 
     } 
     try { 
    $post_id = $facebook->api("/".$uid."/feed", "post", array("picture"=>$target_path)); 
    if(isset($post_id)) 
     echo "A new post to your wall has been posted with id: $post_id"; 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    } 

我一直在嘗試許多不同的方法,其我可以在網上找到,但它不起作用。我曾嘗試添加$ facebook-> setFileUploadSupport(true);但收到錯誤。

請指教我如何才能將照片上傳到用戶牆上。 非常感謝您

+0

難道不是Facebook已經讓你這樣做? – Benubird 2011-03-02 17:07:03

+0

我想創建一個應用程序,他們將能夠通過它上傳照片。然後,照片將張貼到牆上,併發送到我的服務器。我會抓住這些照片,然後將它們顯示爲拼貼畫。 – meAtStackOverflow 2011-03-03 04:11:40

回答

1

認爲這應該工作:

$target_folder = "uploads/"; 
$target_path = $target_folder . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded" . "<br />"; 

    $file_path = $target_folder . $_FILES['uploadedfile']['name']; 
    $arr = array(); 
    $arr["image"] = '@' . realpath($file_path); 
    try { 
     $post_id = $facebook->api("/".$uid."/feed", "post", $arr); 
     if(isset($post_id)) 
     echo "A new post to your wall has been posted with id: $post_id"; 
    } catch (FacebookApiException $e) { 
     error_log($e); 
    } 
} else{ 
    echo "There was an error uploading the file, please try again!" . "<br />"; 
} 
+0

感謝您的幫助,我試圖實現您的方法,但它仍然無法正常工作。 – meAtStackOverflow 2011-03-03 04:08:58

+0

感謝您的幫助! – meAtStackOverflow 2011-03-04 07:15:14

+0

@meAtStackOverflow:歡迎您!介意分享您的工作方式,以便其他用戶從您的經驗中學習? – ifaour 2011-03-04 08:02:30

2

你好老兄此代碼對我的作品完全一樣。你的代碼是什麼,它會將帖子發佈到你的相冊或應用程序相冊中。

 
if(isset($_POST['upload'])) 
{ 
    if (isset($_FILES["file"]) && $_FILES["file"]["error"]==0) 
    { 
     $file='images/'.$_FILES["file"]['name']; 
     if(move_uploaded_file($_FILES["file"]["tmp_name"],$file)) 
     { 
      $facebook->setFileUploadSupport(true); 
      $post_data = array(
      'name'=>$_POST['album'], 
      'description'=>$_POST['album'] 
      ); 
      $data['album'] = $facebook->api("/me/albums", 'post', $post_data); 
      //$file = $file_name; 
      $post_data = array(
      "message" => $_POST['message'], 
      "source" => '@' . realpath($file) 
      ); 
      $album_id = $data['album']['id']; 
      $data['photo'] = $facebook->api("/$album_id/photos", 'post', $post_data); 
     } 
    } 
    /**/ 

} 

當您從應用程序上傳任何圖片時,facebook會在您的應用程序配置文件中創建一個相冊。但是這段代碼會將照片發佈到你的相冊中。

$_POST['album']
是我在文本框中輸入的專輯名稱。然後,我只需發佈表單並上傳照片。我希望這會幫助你

+0

感謝您的協助。我相信我的設置有問題。不太清楚爲什麼我的工作不能繼續。但非常感謝你的幫助。 – meAtStackOverflow 2011-03-04 07:14:43

0
$session = $facebook->getSession(); 
use getUser(); 
+1

請添加一些細節以使您的答案更有用。 – akjoshi 2013-01-08 12:04:41

1

更換getSession()getUser() 因爲舊的PHP版本不識別getSession()功能。

0

使用此代碼和它的工作對你罰款如Facebook文檔這裏How-To: Use the Graph API to Upload Photos to a user’s profile

<?php 

     $app_id = "YOUR_APP_ID"; 
     $app_secret = "YOUR_APP_SECRET"; 
     $post_login_url = "YOUR_POST-LOGIN_URL"; 
     $album_name = 'YOUR_ALBUM_NAME'; 
     $album_description = 'YOUR_ALBUM_DESCRIPTION'; 

     $code = $_REQUEST["code"]; 

     //Obtain the access_token with publish_stream permission 
     if(empty($code)) 
     { 
      $dialog_url= "http://www.facebook.com/dialog/oauth?" 
      . "client_id=" . $app_id 
      . "&redirect_uri=" . urlencode($post_login_url) 
      . "&scope=publish_stream"; 
      echo("<script>top.location.href='" . $dialog_url . 
      "'</script>"); 
     } 
     else { 
     $token_url= "https://graph.facebook.com/oauth/" 
     . "access_token?" 
     . "client_id=" . $app_id 
     . "&redirect_uri=" . urlencode($post_login_url) 
     . "&client_secret=" . $app_secret 
     . "&code=" . $code; 
     $response = file_get_contents($token_url); 
     $params = null; 
     parse_str($response, $params); 
     $access_token = $params['access_token']; 

     // Create a new album 
     $graph_url = "https://graph.facebook.com/me/albums?" 
     . "access_token=". $access_token; 

     $postdata = http_build_query(
     array(
      'name' => $album_name, 
      'message' => $album_description 
      ) 
     ); 
     $opts = array('http' => 
     array(
      'method'=> 'POST', 
      'header'=> 
      'Content-type: application/x-www-form-urlencoded', 
      'content' => $postdata 
     ) 
     ); 
     $context = stream_context_create($opts); 
     $result = json_decode(file_get_contents($graph_url, false, 
      $context)); 

     // Get the new album ID 
     $album_id = $result->id; 

     //Show photo upload form and post to the Graph URL 
     $graph_url = "https://graph.facebook.com/". $album_id 
      . "/photos?access_token=" . $access_token; 
     echo '<html><body>'; 
     echo '<form enctype="multipart/form-data" action="' 
     .$graph_url. ' "method="POST">'; 
     echo 'Adding photo to album: ' . $album_name .'<br/><br/>'; 
     echo 'Please choose a photo: '; 
     echo '<input name="source" type="file"><br/><br/>'; 
     echo 'Say something about this photo: '; 
     echo '<input name="message" type="text" 
      value=""><br/><br/>'; 
     echo '<input type="submit" value="Upload" /><br/>'; 
     echo '</form>'; 
     echo '</body></html>'; 
     } 
?> 

示例響應

{ 
    "id": "1001207389476" 
}