2011-12-26 71 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