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>
可能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