Facebook:php上传照片和张贴在墙上
新的PHP请原谅我愚蠢的问题。Facebook:php上传照片和张贴在墙上
我正在创建我的第一个fb应用程序。它允许用户浏览他们的本地驱动器并选择一张照片。一旦提交,它将重定向到下一页并处理到存储到我的服务器上,然后张贴到用户的墙上。
这个应用程序并没有太多的工作。用户浏览和将照片存储到我的服务器上的部分正在工作,但无法从我的服务器中取回照片并将其张贴到用户的墙上。
的config.php:
<?php
require_once 'facebook.php';
$app_id = "";
$app_key = "";
$app_secret = "";
$canvas_url = "";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$session = $facebook->getSession();
if (!$session) {
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'publish_stream, user_photos, read_stream, read_friendlists'
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
}//end if session user
else
{
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated . "<br />" ;
echo "<img src='https://graph.facebook.com/".$uid."/picture'/>";
}//end try getUser
catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}//end catch getUser
}//end else user
index.php文件包含表单:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
uploader.php运行过程
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['uploadedfile']['name']).
" has been uploaded" . "<br />";
} else{
echo "There was an error uploading the file, please try again!" . "<br />";
}
try {
$post_id = $facebook->api("/".$uid."/feed", "post", array("picture"=>$target_path));
if(isset($post_id))
echo "A new post to your wall has been posted with id: $post_id";
} catch (FacebookApiException $e) {
error_log($e);
}
我一直在尝试许多不同的方法,其我可以在网上找到,但它不起作用。我曾尝试添加$ facebook-> setFileUploadSupport(true);但收到错误。
请指教我如何才能将照片上传到用户墙上。 非常感谢您
我认为这应该工作:
$target_folder = "uploads/";
$target_path = $target_folder . basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['uploadedfile']['name']).
" has been uploaded" . "<br />";
$file_path = $target_folder . $_FILES['uploadedfile']['name'];
$arr = array();
$arr["image"] = '@' . realpath($file_path);
try {
$post_id = $facebook->api("/".$uid."/feed", "post", $arr);
if(isset($post_id))
echo "A new post to your wall has been posted with id: $post_id";
} catch (FacebookApiException $e) {
error_log($e);
}
} else{
echo "There was an error uploading the file, please try again!" . "<br />";
}
感谢您的帮助,我试图实现您的方法,但它仍然无法正常工作。 – meAtStackOverflow 2011-03-03 04:08:58
感谢您的帮助! – meAtStackOverflow 2011-03-04 07:15:14
@meAtStackOverflow:欢迎您!介意分享您的工作方式,以便其他用户从您的经验中学习? – ifaour 2011-03-04 08:02:30
你好老兄此代码对我的作品完全一样。你的代码是什么,它会将帖子发布到你的相册或应用程序相册中。
if(isset($_POST['upload'])) { if (isset($_FILES["file"]) && $_FILES["file"]["error"]==0) { $file='images/'.$_FILES["file"]['name']; if(move_uploaded_file($_FILES["file"]["tmp_name"],$file)) { $facebook->setFileUploadSupport(true); $post_data = array( 'name'=>$_POST['album'], 'description'=>$_POST['album'] ); $data['album'] = $facebook->api("/me/albums", 'post', $post_data); //$file = $file_name; $post_data = array( "message" => $_POST['message'], "source" => '@' . realpath($file) ); $album_id = $data['album']['id']; $data['photo'] = $facebook->api("/$album_id/photos", 'post', $post_data); } } /**/ }
当您从应用程序上传任何图片时,facebook会在您的应用程序配置文件中创建一个相册。但是这段代码会将照片发布到你的相册中。
$_POST['album']是我在文本框中输入的专辑名称。然后,我只需发布表单并上传照片。我希望这会帮助你
感谢您的协助。我相信我的设置有问题。不太清楚为什么我的工作不能继续。但非常感谢你的帮助。 – meAtStackOverflow 2011-03-04 07:14:43
$session = $facebook->getSession();
use getUser();
请添加一些细节以使您的答案更有用。 – akjoshi 2013-01-08 12:04:41
更换getSession()
与getUser()
因为旧的PHP版本不识别getSession()
功能。
使用此代码和它的工作对你罚款如Facebook文档这里How-To: Use the Graph API to Upload Photos to a user’s profile说
<?php
$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>';
}
?>
示例响应
{
"id": "1001207389476"
}
难道不是Facebook已经让你这样做? – Benubird 2011-03-02 17:07:03
我想创建一个应用程序,他们将能够通过它上传照片。然后,照片将张贴到墙上,并发送到我的服务器。我会抓住这些照片,然后将它们显示为拼贴画。 – meAtStackOverflow 2011-03-03 04:11:40