2015-01-04 90 views
1

我只是试图从计算机上传文件到Facebook通过JavaScript SDK beow是我已经写在Facebook上上传图像的代码在这里我已经Facebook的JavaScript SDK以及PHP的服务器端脚本语言来处理mutipart表单数据上传图片,但我不知道完全以了解如何通过图像作为论据的JavaScript SDK Facebook的如何在Facebook上通过JavaScript SDK上传照片

\t <script> 
 
     // Wait until the DOM has loaded before querying the document 
 

 
      var messageToPost; 
 
\t \t $(document).ready(function(){ 
 

 
\t \t \t   \t var isLoaded=false; 
 
\t window.fbAsyncInit = function() { 
 
     FB.init({ 
 
      appId  : '455772327890425', 
 
      xfbml  : true, 
 
      version : 'v2.1' 
 
     }); 
 
\t \t 
 
\t \t isLoaded=true; 
 
     }; 
 
\t 
 
function checkIfLoaded() { 
 
    if(isLoaded) console.log("LOADED!"); 
 
    else console.log("NOT YET!"); 
 

 
    return false; 
 
    } 
 
\t \t 
 
     (function(d, s, id){ 
 
     var js, fjs = d.getElementsByTagName(s)[0]; 
 
     if (d.getElementById(id)) {return;} 
 
     js = d.createElement(s); js.id = id; 
 
     js.src = "//connect.facebook.net/en_US/sdk.js"; 
 
     fjs.parentNode.insertBefore(js, fjs); 
 
     }(document, 'script', 'facebook-jssdk')); 
 
     
 
\t  
 

 

 

 
\t \t \t 
 
      
 
\t \t \t 
 
\t \t \t setTimeout(function(){alert('after set time out');login(); },2000); 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
      
 
\t \t \t 
 
\t \t \t \t \t \t 
 
\t \t \t 
 
\t \t \t function login() 
 
\t \t \t { 
 
\t \t \t \t console.log("login called"); 
 
       
 
       FB.login(function (response) 
 
       { 
 
        accessToken = response.authResponse.accessToken; 
 
        console.log("inside real login"); 
 
       
 
        if (response.authResponse) { 
 
         //fileUpload(); 
 
\t \t \t \t \t 
 
       \t 
 
\t \t \t \t \t postFB(); 
 
         //Logout();   
 
         //alert("Back to Login"); 
 
       \t } 
 
        
 
        else { alert("Login attempt failed!");} 
 
       } , { scope: 'email,user_photos,photo_upload,publish_actions,publish_stream' }); 
 
       
 
\t \t \t }; 
 
\t \t \t 
 
\t \t \t function postFB() 
 
\t \t \t { 
 
        
 
        //alert("message "+ messageToPost); 
 
\t \t \t \t var images={} 
 
\t \t \t \t 
 
\t \t \t \t var wallPost = { 
 
             message:  '<?php echo $messageToPost ?>', 
 
             name  : 'SBIINTOUCH', 
 
             description : 'SBIINTOUCH experience', 
 
\t \t \t \t \t \t \t \t \t \t height  : 70, 
 
\t \t \t \t \t \t \t \t \t \t width  : 60, 
 
\t \t \t \t \t \t \t \t   source  : '<?php echo (basename($_FILES["fileToUpload"]["name"])); ?>' 
 
\t \t \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t   
 
            }; 
 
\t \t \t \t  
 
\t \t \t \t  
 
\t \t \t \t alert(wallPost['message'] +"\n"+wallPost['source']); 
 
\t \t \t \t  
 
       //posting in time line 
 
        FB.api('/me/photos', 'post', wallPost, function(response) { 
 
        if (!response || response.error) { 
 
         alert(JSON.stringify(response.error)+'Error occured while posting in personal feed'); 
 
        } else { 
 
         alert('Post ID: personal feed ' + response.id); 
 
        } 
 
        }); 
 
        
 
       //posting in page 
 
        FB.api('/512538375541360/photos', 'post', 
 
          wallPost, 
 
         function(response) { 
 

 
          if (!response || response.error) { 
 
           alert(JSON.stringify(response.error)+ "while posting in page feed"); 
 
          } else { 
 
           alert('Post ID : SBI Intouch ' + response.id); 
 
          } 
 
         }); 
 
\t \t \t \t FB.logout(); 
 
\t \t \t  }; 
 
\t \t \t 
 
       
 
\t \t \t 
 
\t \t \t 
 
      
 
\t \t \t }); 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t </script>
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t <script src="js/jquery-v1.js"></script> 
 
\t \t <script src="js/jquery-v1-8.js"></script> 
 
     
 

 
\t </head> 
 
\t <body> 
 
     <div id="fb-root"></div> 
 
     <script> 
 
     
 
    
 
\t  
 
\t </script> 
 

 
\t <?php 
 
\t echo $_POST['txta']; 
 
\t echo $_POST['loc1']; 
 
\t echo $_POST['Feedback']; 
 
\t  
 
\t 
 
\t $target_dir = "uploads/"; 
 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
 
$uploadOk = 1; 
 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
 
// Check if image file is a actual image or fake image 
 
if(isset($_POST["submit"])) { 
 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
 
    if($check !== false) { 
 
     echo "File is an image - " . $check["mime"] . "."; 
 
     $uploadOk = 1; 
 
    } else { 
 
     echo "File is not an image."; 
 
     $uploadOk = 0; 
 
    } 
 
} 
 
// Check if file already exists 
 
if (file_exists($target_file)) { 
 
    echo "Sorry, file already exists."; 
 
    $uploadOk = 0; 
 
} 
 
// Check file size 
 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
 
    echo "Sorry, your file is too large."; 
 
    $uploadOk = 0; 
 
} 
 
// Allow certain file formats 
 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
 
&& $imageFileType != "gif") { 
 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
 
    $uploadOk = 0; 
 
} 
 
// Check if $uploadOk is set to 0 by an error 
 
if ($uploadOk == 0) { 
 
    echo "Sorry, your file was not uploaded."; 
 
// if everything is ok, try to upload file 
 
} else { 
 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
 
    } else { 
 
     echo "Sorry, there was an error uploading your file."; 
 
    } 
 
} 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t //msg formation 
 
\t \t \t \t $messageToPost = $_POST['txta']; 
 
\t \t \t \t 
 
        $city   = $_POST['loc1']; 
 
\t \t \t \t echo \t 'city is '.$city; 
 
\t \t \t \t 
 
        $sentiment= $_POST['Feedback']; 
 
        
 
       if($sentiment==='Good') 
 
       \t $messageToPost=$messageToPost." :) "." #SBIInTouch".$city; 
 
       if($sentiment==='Neutral') 
 
       \t $messageToPost=$messageToPost." :| "." #SBIInTouch".$city; 
 
       if( $sentiment==='Bad') 
 
       \t $messageToPost=$messageToPost." :("." #SBIInTouch".$city; 
 
\t 
 
     echo \t 'message is '.$messageToPost; 
 
\t ?> 
 

 
\t \t 
 
\t \t 
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
\t \t 
 
\t </body> 
 
</html>

+0

可能dupl [如何使用Faoples PHP SDK从计算机上传图片](http://stackoverflow.com/questions/27763148/how-to-upload-picture-from-computer-using-faatest-php-sdk) – WizKid 2015-01-05 06:28:22

回答

1

好,我不太确定,但我得到的文件:3 https://developers.facebook.com/docs/graph-api/reference/v2.4/album/photos#publish

您可以直接发送图像字节,或者使用URL使Facebook服务器加载它。 :)

使用JS-SDK上传图像的字节数:

FB.api(
 
    "/{album-id}/photos", 
 
    "POST", 
 
    { 
 
     "source": "{image-data}" 
 
    }, 
 
    function (response) { 
 
     if (response && !response.error) { 
 
     /* handle the result */ 
 
     } 
 
    } 
 
);

,或者使用url代替上传的图片字节:

FB.api(
 
    "/{album-id}/photos", 
 
    "POST", 
 
    { 
 
     "url": "{image-url}" 
 
    }, 
 
    function (response) { 
 
     if (response && !response.error) { 
 
     /* handle the result */ 
 
     } 
 
    } 
 
);