2011-12-26 69 views
3

我有下面的代碼,我認爲是非常接近的工作,但我不斷收到以下錯誤:發佈照片到FB結果需要上傳文件錯誤

{"error":{"message":"(#324) Requires upload file","type":"OAuthException"}} 

,但我無法弄清楚如何指定上傳文件...我已經嘗試了圖像和來源,都給出了相同的錯誤。任何想法,幫助,建議將不勝感激,因爲我一直在這個掙扎了幾天,現在看來似乎不應該這麼難......

<html> 
<head> 
<title>Photo Upload</title> 
</head> 
<body> 
<?php 

$app_id = "MY APP ID GOES HERE"; 
$app_secret = "MY APP SECRET GOES HERE"; 
$post_login_url = "MY URL GOES HERE"; 

$album_name = "Test Album"; 
$album_description = "Blah Blah event Photos"; 

$photo_source = realpath("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg"); 
$photo_message = 'Here is the lighthouse'; 

$code = $_REQUEST["code"]; 
echo "code ==>" . $code . '<br/><br/>'; 

//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,user_photos"; 
    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']; 
    echo "access_token ==>" . $access_token . '<br/><br/>'; 

    // 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; //upload photo 
    echo "album_id ==>" . $album_id . '<br/><br/>'; 


    // Upload the photo 


    $args = array('message' => $photo_message, 
      'image' => $photo_source 
    ); 

    $ch = curl_init(); 
    $url = 'https://graph.facebook.com/'.$album_id.'/photos? 
        access_token='.$access_token; 
    echo "url ==>" . $url . '<br/><br/>'; 

       curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 
    $data = curl_exec($ch); 
    echo "data ==>" . $data . "<br>"; 


} 
?> 
</body> 
</html> 

回答

1

您正在使用的文件名,而不是文件本身在你的Graph API調用中。

你應該@

$args = array('message' => $photo_message, 
    'image' => '@'.$photo_source 
); 
+0

我有點困惑,我知道@抑制錯誤消息,所以我沒有看到解決問題。 我改變的代碼: 直列'的$ args =陣列( '消息'=> $ photo_message, '圖像'=>文件($ photo_source) ) ' ,現在它說我的文件犯規存在在服務器上。我不想要一個服務器文件,我想 本地文件?我在想什麼? – Jesse57 2011-12-28 15:57:14

+0

好吧,我想我找到了我的答案。看起來,當他們聲明fopen打開一個本地文件時,它們對於腳本運行的地方來說意味着本地文件,在我的情況下,它就是go爸爸服務器。我猜測這是出於安全原因,我不能想出任何其他原因。有人可以爲我確認嗎?如果這是正確的,我想我必須從另一個角度來看待這個問題。 – Jesse57 2011-12-28 22:05:50

+1

@JesseCreamer如果你想要一個本地文件和一個來自服務器的文件,你需要首先上傳一個到服務器。是的,通過上傳文件通常來自同一個服務器的腳本位於(實際上它可能是一個很複雜的流程,這不僅是安全原因,而且這是另一個問題) – 2011-12-29 09:06:58

4

前綴文件名需要作爲documentation herehere描述創建Facebook類的新實例時,配置無功fileUpload設置爲true。而且你還需要photo_upload權限;-)

require_once("facebook.php"); 

$config = array(); 
$config[‘appId’] = 'YOUR_APP_ID'; 
$config[‘secret’] = 'YOUR_APP_SECRET'; 
$config[‘fileUpload’] = true; // <-- set this to 'true' otherwise it won't work 

$facebook = new Facebook($config); 
+0

你不必在它的設置config,你可以在初始化Facebook對象後用「$ facebook-> setFileUploadSupport = true」來設置它。您也不需要「File_upload」,但需要「publish_stream」權限。 – 2012-03-28 22:50:07

+0

你讓我的一天! – 2013-08-05 05:33:46