2011-12-29 12 views
0

好的,我在這裏我的繩索末端LOL。只要我把它放在我的網站的tmp文件夾中,我就知道下面的代碼是閱讀jpg文件的。不過,我仍然得到了煩人的錯誤:將照片發佈到FB導致需要上傳文件錯誤

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

我在想什麼????它必須是簡單的東西,但如果我能弄明白的話,我會感到厭煩。任何幫助是極大的讚賞!!!

這裏是我的代碼:

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

$app_id = "APP ID HERE"; 
$app_secret = "APP SECRET HERE"; 
$post_login_url = "LOGIN IN URL HERE"; 

$album_name = "My Silly Album"; 
$album_description = "Blah Blah Photos"; 

$photo_source = "tmp/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)){ 
    login($app_id, $post_login_url); 
} 
else { 
    $access_token = getAccessToken($app_id, $post_login_url, $app_secret, $code); 
    echo "access_token ==>" . $access_token . '<br/><br/>'; 

    $album_id = createAlbum($access_token, $album_name, $album_description); 

    echo "album_id ==>" . $album_id . '<br/><br/>'; 

    uploadPhoto($access_token, $album_id, $photo_source, $photo_message); 


} 

function login($app_id, $post_login_url){ 

    echo '***** login' . '<br/><br/>'; 

    $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>"); 
} 

function getAccessToken($app_id, $post_login_url, $app_secret, $code){ 

    echo '***** getAccessToken' . '<br/><br/>'; 

    $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); 
    return($params['access_token']); 

} 

function createAlbum($access_token, $album_name, $album_description){ 
    // Create a new album 

    echo '***** createAlbum' . '<br/><br/>'; 

    $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)); 

    // Return the new album ID 
    return($result->id); 
} 

function uploadPhoto($access_token, $album_id, $photo_source, $photo_message){ 
    // Upload the photo 

    echo '***** uploadPhoto' . '<br/><br/>'; 
    echo 'photo_source ==> ' . $photo_source . '<br/>photo_message ==> ' . $photo_message . '<br/>'; 

    $fh = fopen("$photo_source","r") or die("can't open $photo_source: $php_errormsg");; 
    while (!feof ($fh)) 
    { 
     $buffer = fgets($fh, 4096); 
     $file[] = $buffer; 
    } 

    fclose ($fh); 

    $args = array('message' => $photo_message, 
        'source' => $file 
    ); 

    $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_VERBOSE, true); 
    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>"; 

    return($data); 

} 

?> 
</body> 
</html> 
+1

這是你2天前你的問題的副本(這又幾乎相同,從你的問題22dec)。爲什麼? – 2011-12-29 20:33:17

+0

我很抱歉,如果我發佈錯誤,代碼已經改變,第一個錯誤是不同的。所以,是的......這與第二個問題是一樣的,但代碼已經改變,我不能確定用新代碼重新分配的最佳方式。 – Jesse57 2011-12-30 14:37:35

+0

[將照片發佈到FB導致需要上傳文件錯誤](http://stackoverflow.com/questions/8639499/posting-photo-to-fb-results-in-requires-upload-file-error) – 2011-12-30 21:34:01

回答

5

我檢查你的腳本。您需要進行這些更改並且cde將正常工作! 100%它爲我工作!

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

,當然我刪除了此節:

$fh = fopen("$photo_source","r") or die("can't open $photo_source: $php_errormsg");; 
    while (!feof ($fh)) 
    { 
     $buffer = fgets($fh, 4096); 
     $file[] = $buffer; 
    } 

fclose ($fh); 
+0

哇,你在開玩笑吧!我以爲我沒有嘗試過這樣的運氣!我非常感謝你的幫助,最後我有這個工作!這開始作爲一個實驗,並最終成爲一個生活使命大聲笑我知道我是如此接近!謝謝!謝謝!謝謝! – Jesse57 2011-12-30 14:43:28

+0

你很好! – 2012-01-01 16:26:07

+0

你能否告訴我們關於在圖片的真實路徑上添加'@'的具體細節? – Snehasish 2014-08-15 13:44:45