如何使用 Facebook 在我的应用程序中上传活动创建照片?

发布于 2024-11-05 10:51:23 字数 1349 浏览 0 评论 0原文


我想在我的应用程序(即事件创建)中上传一张活动照片,并且在我的数据库中,以下是我的代码:
HTML 文件:

<tr>
    <td class="style6 style1 style8"><span class="style30">Add Event Photo </span></td>
    <td colspan="4"><input name="userfile" type="file" class="style28" id="userfile"  size="50" height="25" /></td>
</tr>
<tr>
    <td><input name="image" type="image" src="img_2.gif" alt="submit form" border="0" /></td>
</tr>

现在是要上传的 php 代码:

if (isset($_POST['eventtitle'])&& $_FILES['userfile']['size'] > 0){
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
mysql_query("INSERT INTO  event values('$uid','$eventID','$newDate','$newtime','$newDate2','$newtime1','$name','$location','$street','$city','$fileName', '$fileSize', '$fileType','$content','$description')");
mysql_close($con);
?>

namesizetypecontent 是图像的字段。

Hi
I want to upload a event photo in my app ie event create and also in my database the following is my code:
HTML file:

<tr>
    <td class="style6 style1 style8"><span class="style30">Add Event Photo </span></td>
    <td colspan="4"><input name="userfile" type="file" class="style28" id="userfile"  size="50" height="25" /></td>
</tr>
<tr>
    <td><input name="image" type="image" src="img_2.gif" alt="submit form" border="0" /></td>
</tr>

now the php code to upload:

if (isset($_POST['eventtitle'])&& $_FILES['userfile']['size'] > 0){
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
mysql_query("INSERT INTO  event values('$uid','$eventID','$newDate','$newtime','$newDate2','$newtime1','$name','$location','$street','$city','$fileName', '$fileSize', '$fileType','$content','$description')");
mysql_close($con);
?>

name, size, type and content are fields for the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-11-12 10:51:23

好吧,我两天前写了一篇关于此的教程: 如何:使用 Graph API 创建 Facebook 事件 – 高级

示例代码:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URL"; // mainly this should be the same URL to THIS page

$code = $_REQUEST["code"];

if(empty($code)) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=create_event";
    echo("<script>top.location.href='" . $auth_url . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);

if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
    $msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
    $url = "https://graph.facebook.com/me/events?" . $access_token;
    $params = array();
    // Prepare Event fields
    foreach($_POST as $key=>$value)
        if(strlen($value))
            $params[$key] = $value;

    // Check if we have an image
    if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
        $uploaddir = './upload/';
        $uploadfile = $uploaddir . basename($_FILES['picture']['name']);
        if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
            $params['picture'] = "@" . realpath($uploadfile);
        }
    }
    $params['method'] = "post";

    // Start the Graph API call
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
    curl_close($ch);
    if(is_array($decoded) && isset($decoded['id'])) {
        // Event created successfully, now we can
        // a) save event id to DB AND/OR
        // b) show success message AND/OR
        // c) optionally, delete image from our server (if any)
        $msg = "Event created successfully: {$decoded['id']}";
    }
}
?>
<!doctype html>
<html>
<head>
<title>Create An Event</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
</style>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form enctype="multipart/form-data" action="" method="post">
    <p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
    <p><label for="description">Event Description</label><textarea name="description"></textarea></p>
    <p><label for="location">Location</label><input type="text" name="location" value="" /></p>
    <p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
    <p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>
    <p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
    <p>
        <label for="privacy_type">Privacy</label>
        <input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open   
        <input type="radio" name="privacy_type" value="CLOSED" />Closed   
        <input type="radio" name="privacy_type" value="SECRET" />Secret   
    </p>
    <p><input type="submit" value="Create Event" /></p>
</form>
</body>
</html>

我建议您阅读本教程以及同一篇文章中链接的上一篇教程以获取更多信息。

Well, I've written a tutorial about this two days ago: How To: Create Facebook Events Using Graph API – Advanced

Sample code:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URL"; // mainly this should be the same URL to THIS page

$code = $_REQUEST["code"];

if(empty($code)) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=create_event";
    echo("<script>top.location.href='" . $auth_url . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);

if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
    $msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
    $url = "https://graph.facebook.com/me/events?" . $access_token;
    $params = array();
    // Prepare Event fields
    foreach($_POST as $key=>$value)
        if(strlen($value))
            $params[$key] = $value;

    // Check if we have an image
    if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
        $uploaddir = './upload/';
        $uploadfile = $uploaddir . basename($_FILES['picture']['name']);
        if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
            $params['picture'] = "@" . realpath($uploadfile);
        }
    }
    $params['method'] = "post";

    // Start the Graph API call
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
    curl_close($ch);
    if(is_array($decoded) && isset($decoded['id'])) {
        // Event created successfully, now we can
        // a) save event id to DB AND/OR
        // b) show success message AND/OR
        // c) optionally, delete image from our server (if any)
        $msg = "Event created successfully: {$decoded['id']}";
    }
}
?>
<!doctype html>
<html>
<head>
<title>Create An Event</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
</style>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form enctype="multipart/form-data" action="" method="post">
    <p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
    <p><label for="description">Event Description</label><textarea name="description"></textarea></p>
    <p><label for="location">Location</label><input type="text" name="location" value="" /></p>
    <p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
    <p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>
    <p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
    <p>
        <label for="privacy_type">Privacy</label>
        <input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open   
        <input type="radio" name="privacy_type" value="CLOSED" />Closed   
        <input type="radio" name="privacy_type" value="SECRET" />Secret   
    </p>
    <p><input type="submit" value="Create Event" /></p>
</form>
</body>
</html>

I recommend you read the tutorial and the previous one linked in the same article for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文