将照片从通过其他网站的http帖子发送的网址上传到Facebook相册

时间:2012-09-21 23:39:02

标签: facebook facebook-graph-api

我在一个网站上工作,让用户查看照片..我希望他们能够按下照片,照片被发送到他们的Facebook帐户...到目前为止,我发现这个文件,允许用户上传来自本地mashine How-To: Use the Graph API to Upload Photos to a user’s profile的照片..是否有一种方法可以修改此代码,以便从其他网站的http帖子中获取网址?

   $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>';
  }

&GT?;

1 个答案:

答案 0 :(得分:0)

https://developers.facebook.com/docs/reference/api/photo/

  

您还可以通过提供带有照片网址的url参数来发布照片。

您需要做的就是post to /PROFILE_ID/photos,并将source参数替换为名为url的参数,其中包含可通过HTTP公开访问的照片地址。