使用脸谱图API在应用个人资料页面上传图片

时间:2011-11-16 07:42:20

标签: php facebook facebook-graph-api

我正在创建一个应用,用户可以上传图片,该图片将存储在应用配置文件页面和用户个人资料中。我使用下面的代码来完成这项工作

<?php
       $app_id = "XXXXXXXXXXXXXXXX";
       $app_secret = "XXXXXXXXXXXXXXXXXXXXXXXX";
     $post_login_url ="XXXXXXXXXXXXXXXXXXXXXXX";

       $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'];
        // first get your album id, let's assume you need to create it
        // create this before hand and you can just reference the id
        $attachment = array('access_token'=> ACCESS_TOKEN, 'name'=>$ablum_name);
        try{
        $album_resp = $facebook->api("/{$this->page_id}/albums", 'POST', $attachment);
        }catch(Exception $e){
        throw new Exception("Failed to create album: ". $e->getMessage());
        }
        $album_id = $album_resp['id'];
         // Show photo upload form to user and post to the Graph URL

        $graph_url= "https://graph.facebook.com/photos?"
        . "access_token=" .$access_token;


         echo '<form enctype="multipart/form-data" action="'
         .$graph_url .' "method="POST">';
         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>';

      }
?>

使用此代码,它将转到用户的个人资料,但不会转到应用的个人资料页面。任何人都可以告诉它有什么问题吗?或者还有另一种方法吗?

0 个答案:

没有答案