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

标签 php facebook facebook-graph-api upload photo

我有以下代码,我认为它非常接近工作,但我不断收到以下错误:

{"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>

最佳答案

在创建 Facebook 类的新实例时,您需要将配置变量 fileUpload 设置为 true,如 documentation here 中所述和 here .而且您还需要 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);

关于php - 将照片发布到 FB 导致需要上传文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8639499/

相关文章:

php - Chrome 中的 JavaScript 问题

android - Google Play URL 弄乱了 Facebook 的分享对话框

android - "shared a link via"使用 Android FacebookShareDialog 时为空白

javascript - Node JS 忽略未定义的检查

php - Facebook PHP API 在登录时抛出异常

PHP call_user_func & $class->$func() 函数

php - 如何在 Symfony2/Silex 中捕获错误和异常?

php - $var 而不是 $_GET ['var' ] 在 PHP 中?

Facebook 连接多个 FB 帐户

ios - 在后台获取一组 friend 的 Facebook 个人资料图片