0

你能告诉我我做错了什么吗?我使用 PHP。尝试将照片上传到用户个人资料中。我获得了权限并且它有效。但是上传不起作用,我尝试了很多方法。那么我做错了什么?

  <?php

   include_once 'facebook.php';

       $facebook = new Facebook(array(
       'appId'  => 'ID',
       'secret' => 'SECRET',
       'cookie' => true,
       'domain' => 'DOMAIN',
       'fileUpload' => 'true'
       ));



      $session = $facebook->getSession();

      if (!$session) {

       $loginUrl = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 1,
        'display' => 'page',
        'req_perms' => 'user_likes, publish_stream',
        'next' => 'NEXTURL'
   ));

      echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";

    } else{


     try {

       $uid = $facebook->getUser();
       $me = $facebook->api('/me');
       $token = $session['access_token'];//here I get the token from the $session array
       $album_id = 'ALBUM ID';  /// what should I write here?

       //upload your photo
     $file= 'logo.png';
    $args = array(
       'message' => 'Photo from app',
    );
    $args[basename($file)] = '@' . realpath($file);

   $ch = curl_init();
  $url = 'https://graph.facebook.com/'.$album_id.'/photos?access_token='.$token;
   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);

  //returns the id of the photo you just uploaded
  print_r(json_decode($data,true));



  } catch(FacebookApiException $e){
  echo "Error:" . print_r($e, true);
  }
  }
  ?>

提前致谢

4

3 回答 3

0

您必须将属性 fileUpload==true 添加到 Facebook 对象:

$facebook = new Facebook(array(
    'appId'  => 'ID',
    'secret' => 'SECRET',
    'cookie' => true,
    'domain' => 'DOMAIN',
    'fileUpload' => true
));

更新:

我总是使用此代码上传照片:

$photo= $facebook->api(array('method'=>'photos.upload',
            'caption'=>SOME_TEXT,
            'file'=>'@'.$file));
于 2012-05-28T07:31:18.563 回答
0

你可以试试这个:

$args = array('message' => $mess,
  );
  $args['image'] = '@' . realpath($file.".jpeg");


  $facebook->setFileUploadSupport(true);
     try
     {
        if(!isset($_GET['a']))
            {
            $data = $facebook->api('/me/photos', 'post', $args);
            }
          //  echo "<br/>Image posted. <a href=\"http://www.facebook.com/photo.php?fbid=".$data['id']."\" target=\"_blank\">Click here to view</a>. Thanks for using this application.";
      }
      catch(FacebookApiException $e)
      {
        var_dump($e);
      }

这是工作代码的直接摘录。

于 2012-05-28T08:05:12.280 回答
0

试试这个

$args = array('message' => 'Photo Upload');
$args['image'] = '@' . realpath($FILE_PATH);
$data = $facebook->api("/{$album_id}/photos", 'post', $args);

注意:如果您想创建一个与应用名称相同的新专辑名称并在那里上传,那么您必须使用

$facebook->api("/me/photos", 'post', $args);
于 2012-05-28T08:32:45.137 回答