2012-04-20 53 views
2

以下是我的腳本,我想用它來更新我的Facebook粉絲頁面的狀態,但它不能以上傳的圖片無法在Facebook畫廊中查看的方式工作fanpage請親切看到這個鏈接(http://radiations3.com/damn.jpg)其詳細的屏幕截圖,究竟是由我的腳本發生了什麼,以及我想要做什麼/從我的腳本想要什麼。請讓我知道我的腳本有什麼問題,我該怎麼做?PHP SCRIPT應該上傳圖片及其描述

<?php 

require 'src/facebook.php'; 

$app_id = "332267477347"; 
$app_secret = "xxxxxxxx"; 

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

)); 

$facebook->setFileUploadSupport(true); 


$user = $facebook->getUser(); 
//echo $user; 

if(($facebook->getUser())==0) 
{ 
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}"); 
exit; 
} 
else { 
$accounts_list = $facebook->api('/me/accounts'); 
echo "i am connected"; 
} 
    $valid_files = array('image/jpeg', 'image/png', 'image/gif'); 


//to get the page access token to post as a page 
foreach($accounts_list['data'] as $account){ 
     if($account['id'] == 194458563914948){  // my page id =123456789 
     $access_token = $account['access_token']; 
     echo "<p>Page -- Access Token: $access_token</p>"; 
     } 
    } 

//posting to the page wall 

if (isset($_FILES) && !empty($_FILES)) 
{ 
$folder = "pak/".$_FILES['pic']['name']; 
$fold = 'http://snowdrop.com.pk/fb/'.$folder; 
echo $fold."<br>"; 
if(move_uploaded_file($_FILES['pic']['tmp_name'], $folder)) 
{ 
#Upload photo here 
    $img = realpath($_FILES["pic"]["tmp_name"]); 
$attachment = array('message' => $_POST['textfield'], 
          'picture' => $fold, 
           'access_token' => $access_token, 


       ); 
$status = $facebook->api("/194458563914948/feed", 'post', $attachment); 

echo $status; 
var_dump($status); 

} 
else{ 
    echo 'Only jpg, png and gif image types are supported!'; 

} 
} 
?> 
<body> 
<!-- Form for uploading the photo --> 
<div class="main"> 
    <p>Select a photo to upload on Facebook Fan Page</p> 
    <form method="post" action="" enctype="multipart/form-data"> 
    <p>Select the image: <input type="file" name="pic" /> 
    <br /> 
    <label>Description 
    <input type="text" name="textfield" id="textfield" /> 
    </label> 
    </p> 
    <p><input class="post_but" type="submit" value="Upload to my album" /></p> 
    </form> 
</div> 
</body> 

Detailed image of what my script is doing and what i want instead

回答

2

得到你正在尋找你需要上傳到相冊ID的效果。在下面的代碼中,只需添加您想要上傳的專輯ID即可。


$status = $facebook->api("/Album_ID/photos", 'post', $attachment); 

<?php 

require 'src/facebook.php'; 

$app_id = "332267477347"; 
$app_secret = "xxxxxxxx"; 

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

)); 

$facebook->setFileUploadSupport(true); 


$user = $facebook->getUser(); 
//echo $user; 

if(($facebook->getUser())==0) 
{ 
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}"); 
exit; 
} 
else { 
$accounts_list = $facebook->api('/me/accounts'); 
echo "i am connected"; 
} 
    $valid_files = array('image/jpeg', 'image/png', 'image/gif'); 


//to get the page access token to post as a page 
foreach($accounts_list['data'] as $account){ 
     if($account['id'] == 194458563914948){  // my page id =123456789 
     $access_token = $account['access_token']; 
     echo "<p>Page -- Access Token: $access_token</p>"; 
     } 
    } 

//posting to the page wall 

if (isset($_FILES) && !empty($_FILES)) 
{ 
$folder = "pak/".$_FILES['pic']['name']; 
$fold = 'http://snowdrop.com.pk/fb/'.$folder; 
echo $fold."<br>"; 
if(move_uploaded_file($_FILES['pic']['tmp_name'], $folder)) 
{ 
#Upload photo here 
    $img = realpath($_FILES["pic"]["tmp_name"]); 
$attachment = array('message' => $_POST['textfield'], 
          'source' => $fold, 
           'access_token' => $access_token, 


       ); 
$status = $facebook->api("/Album_ID/photos", 'post', $attachment); 

echo $status; 
var_dump($status); 

} 
else{ 
    echo 'Only jpg, png and gif image types are supported!'; 

} 
} 
?> 
<body> 
<!-- Form for uploading the photo --> 
<div class="main"> 
    <p>Select a photo to upload on Facebook Fan Page</p> 
    <form method="post" action="" enctype="multipart/form-data"> 
    <p>Select the image: <input type="file" name="pic" /> 
    <br /> 
    <label>Description 
    <input type="text" name="textfield" id="textfield" /> 
    </label> 
    </p> 
    <p><input class="post_but" type="submit" value="Upload to my album" /></p> 
    </form> 
</div> 
</body> 
+1

我怎麼能發出HTTP POST請求PAGE_ID與publish_stream和manage_pages權限/照片。 (($ facebook-> getUser())== 0) {012 - - - - - - - - - manage_pages'))}「); 退出; } – 2012-04-20 07:39:40

+1

問題現在正在解決,這行代碼'source'=>'@'有問題。 $ img,當我將其更改爲'source'=>'@'時。 realpath($文件夾),然後代碼工作就像一個魅力.... – 2012-04-20 10:46:19