我正在尝试开发一个网站,我可以使用登录进行注册...很高兴我成功地检索了用户的信息。当我使用下面的链接时,用户个人资料图片也会显示。
echo '<img src="https://graph.facebook.com/'.$userdata['id'].'/picture">';
但我需要将此图像存储到我的数据库中。我的数据库名称是fbtest,表名是用户。 要存储的图片所在的列名称是图片,类型是blob .. 我怎么能把这张图片插入我的数据库。 验证页面的代码显示在下面..
<?php
require 'lib/db.php';
require 'lib/facebook.php';
require 'lib/fbconfig.php';
session_start();
$facebook=$_SESSION['facebook'];
$userdata=$_SESSION['userdata'];
$logoutUrl=$_SESSION['logout'];
$access_token_title='fb_'.$facebook_appid.'_access_token';
$access_token=$facebook[$access_token_title];
if(!empty($userdata))
{
echo '<h1>Login User Details</h1>';
echo '<img src="https://graph.facebook.com/'.$userdata['id'].'/picture">';
$img = file_get_contents('https://graph.facebook.com/'.$userdata['id'].'/picture');
echo "<br/>";
echo '<b>Access Token: </b>'.$access_token;
echo "<br/>";
echo '<b>User ID: </b>'.$userdata['id'];
echo "<br/>";
echo '<b>Name: </b>'.$userdata['name'];
echo "<br/>";
echo '<b>First Name: </b>'.$userdata['first_name'];
echo "<br/>";
echo '<b>Last Name: </b>'.$userdata['last_name'];
echo "<br/>";
echo '<b>Email: </b>'.$userdata['email'];
echo "<br/>";
echo '<b>Gender: </b>'.$userdata['gender'];
echo "<br/>";
echo '<b>Birthday: </b>'.$userdata['birthday'];
echo "<br/>";
echo '<b>Location: </b>'.$userdata['location']['name'];
echo "<br/>";
echo '<b>Hometown: </b>'.$userdata['hometown']['name'];
echo "<br/>";
echo '<b>Bio :</b>'.$userdata['bio'];
echo "<br/>";
echo '<b>Relationship Status: </b>'.$userdata['relationship_status'];
echo "<br/>";
echo '<b>Time Zone: </b>'.$userdata['timezone'];
echo "<br/>";
echo "<br/>";
$facebook_id=$userdata['id'];
$name=$userdata['name'];
$email=$userdata['email'];
$gender=$userdata['gender'];
$birthday=$userdata['birthday'];
$location=mysql_real_escape_string($userdata['location']['name']);
$hometown=mysql_real_escape_string($userdata['hometown']['name']);
$bio=mysql_real_escape_string($userdata['bio']);
$relationship=$userdata['relationship_status'];
$timezone=$userdata['timezone'];
mysql_query("INSERT INTO `users` (`id`, `name`, `email`, `gender`, `birthday`, `location`, `hometown`, `bio`, `relationship`, `picture`)
VALUES ('$facebook_id','$name','$email','$gender','$birthday','$location','$hometown','$bio','$relationship','$img')") ;
echo "<br/>";
echo '<a href="'.$logoutUrl.'">Logout Facebook</a>';
}
else
{
header("Location: fblogin.php");
}
?>
请帮忙???
答案 0 :(得分:1)
这里有两个适合您将图片存储在数据库中。
在调用facebook graph api之后,您将能够获得个人资料图片网址,之后您可以继续以下两种方式。
OR
以下是将图像转换为base64的代码。
<?php
$imgPath = 'http://abcd.com/profilepicture/mypic.png'; // Facebook profile picture will replace here.
$imgdata = file_get_contents($imgPath);
$imgBase64Data = base64_encode($imgdata);
mysql_query("INSERT INTO `users` (`id`, `name`, `email`, `gender`, `birthday`, `location`, `hometown`, `bio`, `relationship`, `picture`)
VALUES ('$facebook_id','$name','$email','$gender','$birthday','$location','$hometown','$bio','$relationship','$imgBase64Data')") ;
&GT;
希望这会对你有所帮助。
由于 Dinesh Pal
&GT;