PHP - 无法使用Facebook API在Facebook上传照片 [英] PHP - Can't Upload photo on Facebook using Facebook API
问题描述
Hello Everyone,
我正试图在Facebook开发者中执行此代码
upload.php
<?php
// 请记住将文件从SDK的src /目录复制到
// 服务器上应用程序中的目录,例如php-sdk /
require_once(' php-sdk / facebook.php');
$ config = array(
' appId' => ' appId',
' secret' => ' secret',
' fileUpload' => true,
' allowSignedRequest' => false // 可选,但非画布应用应设为false
);
$ facebook = new Facebook($ config);
$ user_id = $ facebook-> getUser();
$ photo = ' facebook_logo.jpg'跨度>; // 本地文件系统上照片的路径
$ message = ' 通过PHP SDK上传照片!';
?>
< html >
< head > < / head >
< 正文 >
<? php
if( $ user_id){
// 我们有一个用户ID,所以可能是登录用户。
// 如果没有,我们将得到一个异常,我们将在下面处理。
尝试 {
// 上传到用户的个人资料。照片将位于
// 个人资料中的第一张专辑中。您还可以使用/ ALBUM_ID作为路径<上传到
// 特定相册/ span>
// 'source'=>新CURLFile($ photo,'image / png'),
$ ret_obj = $ facebook-> api(' / me / photos',' POST',数组(
' source' = > $ photo,
' message' => $ message,
)
);
echo ' 照片ID:'。 $ ret_obj [' id'];
echo ' < a href = '。$ facebook-> getLogoutUrl()。' > logout< / a>'< /跨度>;
} catch(FacebookApiException $ e){
// 如果用户已注销,则为即使访问令牌无效,也可以
// 用户ID。
// 在这种情况下,我们会得到一个例外,所以我们
// 只需要求用户再次登录。
$ login_url = $ facebook-> getLoginUrl(array(
' scope' => ' photo_upload'
));
echo ' 请< a href = '。 $ login_url 。' >登录< / A>'跨度>;
error_log($ e-> getType());
error_log($ e-> getMessage());
}
} 其他 {
// 没有用户,打印用户登录的链接
// 要将照片上传到用户的墙上,我们需要photo_upload权限
// 我们将使用当前网址作为redirect_uri,因此我们不会
// 需要在此处指定。
$ login_url = $ facebook-> getLoginUrl(array(' scope' => ' photo_upload'));
echo ' 请< a href = '。 $ login_url 。' >登录< / A>'跨度>;
}
?>
当我点击登录(在Facebook中)<br />
错误
应用程序未设置:此应用程序的开发人员尚未设置此应用程序适合Facebook登录。
我已经尝试获取用户信息的代码并发布在Facebook开发者发布的链接,它工作正常。
在我的 Facebook开发者设置
Facebook上的应用程序
Canvas URL:
http:// localhost / sites / folder /
Canvas URL:
https:// localhost / sites / folder /
网站
网站网址:
http:// localhost / sites / folder /
这意味着什么
上传照片到用户的墙上,我们需要photo_upload权限
在设置中找不到这个..不知道这个权限是否在用户配置文件中,如果有,我在哪里可以找到它?
config = array(
' appId' => ' appId',
' secret' => ' secret',
' fileUpload' => true,
' allowSignedRequest' => false // 可选,但非画布应用应设为false
);
facebook = new Facebook的(
配置);
Hello Everyone,
I'm trying to execute this code in Facebook developer
upload.php
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'appId',
'secret' => 'secret',
'fileUpload' => true,
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = 'facebook_logo.jpg'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
//'source' => new CURLFile($photo, 'image/png'),
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => $photo,
'message' => $message,
)
);
echo 'Photo ID: ' . $ret_obj['id'];
echo '<a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
When I clicked login (in facebook)
Error
App Not Setup: The developers of this app have not set up this app properly for Facebook Login.
I already try getting the code for getting user info and posting a link posted in Facebook developer and it works fine.
In my Facebook Developer Settings
App on Facebook
Canvas URL:
http://localhost/sites/folder/
Canvas URL:
https://localhost/sites/folder/
Website
Site URL:
http://localhost/sites/folder/
And what does this means
To upload a photo to a user's wall, we need photo_upload permission
Can't find this in Settings.. Don't know if this permission is in Users Profile, If so where can i find it?
config = array( 'appId' => 'appId', 'secret' => 'secret', 'fileUpload' => true, 'allowSignedRequest' => false // optional but should be set to false for non-canvas apps );
facebook = new Facebook(
config);
这篇关于PHP - 无法使用Facebook API在Facebook上传照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!