Facebook PHP SDK上传照片 [英] Facebook PHP SDK Upload Photos
本文介绍了Facebook PHP SDK上传照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试将在我的服务器上渲染的照片即时上传到用户的Facebook相册.由于FB Docs非常非常非常糟糕(至少可以这样说),我希望有人向我展示一些使用PHP SDK进行Graph API调用的良好代码示例?
解决方案
类似的东西.
try {
$facebook->setFileUploadSupport('http://www.example.com/');
$response = $facebook->api(
'/me/photos/',
'post',
array(
'message' => 'This is my image caption',
'source' => '@/path/to/image' // @-sign must be the first character
)
);
}
catch (FacebookApiException $e) {
error_log('Could not post image to Facebook.');
}
首先,您需要使用此代码进行身份验证.
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'cookie' => TRUE,
'domain' => $_SERVER['SERVER_NAME']
));
$facebook->getSession();
try {
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
$me = NULL;
}
if ( is_null($me) ) {
$auth_url = $facebook->getLoginUrl(array(
'req_perms' => 'read_stream,publish_stream,user_photos'
));
header("Location: $auth_url");
}
此处是指向您可以向用户要求的所有权限的链接. >
Trying to upload a photo rendered on my server to the facebook album of the user on the fly. Since FB Docs are very,very,very bad (to say the least) I was hoping for someone to show me some good code examples of Graph API calls with the PHP SDK?
解决方案
Something like this.
try {
$facebook->setFileUploadSupport('http://www.example.com/');
$response = $facebook->api(
'/me/photos/',
'post',
array(
'message' => 'This is my image caption',
'source' => '@/path/to/image' // @-sign must be the first character
)
);
}
catch (FacebookApiException $e) {
error_log('Could not post image to Facebook.');
}
EDIT: First you need to authenticate, by using this code.
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'cookie' => TRUE,
'domain' => $_SERVER['SERVER_NAME']
));
$facebook->getSession();
try {
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
$me = NULL;
}
if ( is_null($me) ) {
$auth_url = $facebook->getLoginUrl(array(
'req_perms' => 'read_stream,publish_stream,user_photos'
));
header("Location: $auth_url");
}
Here is a link to all permissions you can ask the user for.
这篇关于Facebook PHP SDK上传照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文