Facebook:php上传照片并张贴在墙上 [英] Facebook: php upload photo and post on wall

查看:59
本文介绍了Facebook:php上传照片并张贴在墙上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php的新手,请原谅我的愚蠢问题.

new to php please forgive my silly questions.

我正在创建我的第一个fb应用程序.它允许用户浏览其本地驱动器并选择照片.提交后,它将重定向到下一页并首先处理存储到我的服务器上,然后发布到用户的墙上.

I am creating my first fb app. It allows user to browse through their local drive and select a photo. Once it is submit, it will redirect to the next page and process to the storing onto my server first then posting to user's wall.

该应用程序无法真正正常工作.用户浏览和应用程序将照片存储到我的服务器上的部分正在工作,但是无法从我的服务器上抓回照片并将其发布到用户的墙上.

The application is not working really that much. The part where user browse and app storing the photo on to my server is working, but it fails to grab the photo back from my server and post it on the user's wall.

config.php:

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包含以下形式:

index.php contains the form:

<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运行该过程

uploader.php run the process

$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);但收到错误.

I have been trying many different ways which i could find online but it does not work. i have tried adding $facebook->setFileUploadSupport(true); but receive errors.

请告诉我如何将照片上传到用户墙上. 非常感谢

Please advice me how i could go about getting to upload the photo onto the user wall. Thank you very much

推荐答案

认为这应该有效:

$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 />";
}

这篇关于Facebook:php上传照片并张贴在墙上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆