【问题标题】:Posting Photo to FB results in Requires upload file error将照片发布到 FB 导致需要上传文件错误
【发布时间】:2012-01-30 02:41:27
【问题描述】:

好的,我已经走到尽头了,哈哈。我有以下代码,只要我把它放在我网站的 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>

【问题讨论】:

  • 这是您 2 天前发布的问题的副本(这与您在 12 月 22 日发布的问题几乎相同)。为什么?
  • 如果我发错了,我很抱歉,代码已经改变,第一个错误是不同的。所以,是的......这与第二个问题相同,但代码已更改,我不确定使用新代码重新询问的最佳方法。

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


【解决方案1】:

我检查了你的脚本。您需要进行这些更改,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);

【讨论】:

  • 哇,你在开玩笑吧!!!我以为我试过这个没有运气!我真的很感谢你的帮助,终于我有这个工作了!这开始是一个实验,最后是一个人生使命,哈哈,我知道我是如此接近!谢谢!谢谢!谢谢!
  • 您能告诉我将“@”附加到图片的真实路径有什么特别之处吗?
猜你喜欢
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 2012-09-12
相关资源
最近更新 更多