如何修改相册/照片上传流程?

时间:2012-09-07 01:54:06

标签: php facebook-graph-api

我正在创建一个FaceBook应用,其中用户选择多个图像,应用程序会从中生成单个图像(PHP)。我正在为生成的图像提供semi-random name - $storage_url = $rack_directory . "rack_" . mt_rand() . mt_rand() . ".png"; (e.g. rack_2128639756968968165.png)并暂时存储它。

我希望用户能够直接从页面上传生成的图片到Facebook个人资料,而不是downloading然后uploading

在查看本网站以及其他网站的答案时,看起来我可以使用此方法:    

   $app_id = "YOUR_APP_ID";
   $app_secret = "YOUR_APP_SECRET";
   $post_login_url = "YOUR_POST-LOGIN_URL";
   $album_name = 'YOUR_ALBUM_NAME';
   $album_description = 'YOUR_ALBUM_DESCRIPTION';

   $code = $_REQUEST["code"];

   //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";
       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'];

     // 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;

     //Show photo upload form and post to the Graph URL
     $graph_url = "https://graph.facebook.com/". $album_id
       . "/photos?access_token=" . $access_token;
     echo '<html><body>';
     echo '<form enctype="multipart/form-data" action="'
     .$graph_url. ' "method="POST">';
     echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
     echo 'Please choose a photo: ';
     echo '<input name="source" type="file"><br/><br/>';
     echo 'Say something about this photo: ';
     echo '<input name="message" type="text"
        value=""><br/><br/>';
     echo '<input type="submit" value="Upload" /><br/>';
     echo '</form>';
     echo '</body></html>';
  }

...生成一张独特的专辑并上传图片...... 但是 - 我不想显示一个用户浏览并上传的表单(在他们右键/控制点击并从页面保存图像之后)。只需获取页面上显示的现有生成图像并提交即可。

是否有可以使用的功能,以便发生此过程,并且唯一的表单要求是提交和上传?

感谢。

1 个答案:

答案 0 :(得分:1)

您需要的是Facebook SDK:https://developers.facebook.com/docs/reference/php/

这使得所有这些“图形”调用变得更加简单。

从该页面粘贴:

// Load the facebook SDK
require_once("facebook.php");

$config = array();
$config['appId'] = 'YOUR_APP_ID';
$config['secret'] = 'YOUR_APP_SECRET';
$config['fileUpload'] = false; // optional

$facebook = new Facebook($config);

try { 
   $uid = $facebook->getUser();
catch (FacebookApiException $e) { 
   // Not logged on - you should log them on. Various methods, but redirect to $facebook->getLoginURL() is simplest. Docs: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
} 

用户登录后,您也可以通过SDK上传:

// Now upload the file
try { 
    $facebook->setFileUploadSupport('http://MyDomain.com/'); 
    $response = $facebook->api( 
      '/me/photos/', 
      'post', 
      array( 
        'message' => 'Image Cpation', 
        'source' => '@/path/to/image' // @-sign must be the first character 
      ) 
    ); 
  } 
  catch (FacebookApiException $e) { 
    error_log('Could not post image to Facebook.'); 
  } 

您可以使用相册ID代替“我”直接上传到特定相册。但是,通过SDK进行操作将比使用图形调用手动完成所有操作更有帮助!

抱歉,这不是一个完整的“这里是代码”的答案(还有一些工作要做),但这应该可以帮助你上传而不会提示用户。