将照片发布到FB导致需要上传文件错误

时间:2011-12-29 18:40:22

标签: php facebook facebook-graph-api upload photo

好的,我在这里结束了大声笑。我有以下代码,我知道只要我把它放在我网站的tmp文件夹中就读jpg文件。但是,我仍然遇到了令人讨厌的错误:

{"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 个答案:

答案 0 :(得分: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);