简体   繁体   English

在Facebook上上传照片并创建相册。 下载后,将用户重定向到显示了照片的另一页

[英]Upload photo on Facebook and create an album. After the download redirect the user to another page with the photo presented

The bellow code works fine. 波纹管代码工作正常。 It uploads a photo and creates a new photo. 它上传照片并创建新照片。 Apparently i don't know how to make it redirect somewhere and it end up with a blank page with the albums id. 显然我不知道如何使它重定向到某个地方,并且最终得到一个带有专辑ID的空白页面。 I want to put a redirect link and on that redirect link the photo should be presented on. 我想放置一个重定向链接,并且应该在该重定向链接上显示照片。 I want the user to be redirected after the photo is uploaded. 我希望用户在照片上传后被重定向。 The photo is uploaded when the user presses the upload button. 当用户按下上传按钮时,照片被上传。

 $post_login_url = "http://apps.facebook.com/firstestt/upload.processor.php";
   $album_name = 'Facepic Album';
   $album_description = 'http://apps.facebook.com/firstestt/';


   $code = $_REQUEST["code"];
$facebook->setFileUploadSupport(true);
   //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 = $facebook->getAccessToken();

     // 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/>';
 //When this button is pressed the page should upload the photo and redirect the user to another link  
     echo '<input type="submit" value="Upload"  /><br/>'; 
     echo '</form>';
     echo '</body></html>';
  }

I think this article suits your needs exactly. 我认为本文完全适合您的需求。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>WebSpeaks.in | Upload images to Facebook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
html{
    font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
}
.main{
    width:400px;
    margin:auto;
    border:2px solid #0066CC;
    color:#3B5998;
    padding:20px;
    font-size: 11px;
    -moz-border-radius: 4px 4px 4px 4px;
    border-radius: 4px 4px 4px 4px;
    -moz-box-shadow: 1px 1px 0 #d5d5d5;
    background: none repeat scroll 0 0 #F2F2F2;
}
.post_but {
    background: none repeat scroll 0 0 #EEEEEE;
    border-color: #999999 #999999 #888888;
    border-style: solid;
    border-width: 1px;
    color: #333333;
    cursor: pointer;
    display: inline-block;
    font-size: 11px;
    font-weight: bold;
    padding: 2px 6px;
    text-align: center;
    text-decoration: none;
}
a{
    color:#3B5998;
}
</style>
</head>

<body>
<?php
/******************Configuration options***********************/

require_once 'library/facebook.php';
$facebook = new Facebook(array(
  'appId'  => $appId,   //your facebook application id
  'secret' => $secret,  //your facebook secret code
  'cookie' => true
));

$user = $facebook->getUser();

if(is_null($facebook->getUser()))
{
    header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
    exit;
}
/******************Configuration options***********************/

if($_SERVER['REQUEST_METHOD'] =='POST'){
    $img = realpath($_FILES["pic"]["tmp_name"]);
    // allow uploads
    $facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
    // add a status message
    $photo = $facebook->api('/me/photos', 'POST', 
        array(
            'source' => '@' . $img,
            'message' => 'This photo was uploaded via www.WebSpeaks.in'
        )
    );

    echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
}
?>
<div class="main">
    <p>Select a photo to upload on Facebook.</p>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <p>Select the image: <input type="file" name="pic" /></p>
    <p><input class="post_but" type="submit" value="Upload to my album" /></p>
    </form>
</div>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM