FB开发 - 将照片上传到用户的个人资料中

时间:2012-05-28 07:10:36

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

你能告诉我我做错了吗?我用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);
  }
  }
  ?>

提前致谢

3 个答案:

答案 0 :(得分:0)

您必须将属性fileUpload == true添加到Facebook-Object:

$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));

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

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

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