上传照片的帖子到Facebook的相册 [英] uploaded photo post to facebook album
问题描述
当下面的表单提交表单操作设置为$ graph_url在我的情况下当相册名称,描述,图像数据库插入操作完成后下面的代码执行我不知道如何设置$ graph_url在数据库插入操作之前地点?
下面的代码发布照片在Facebook alubm成功.....任何帮助是赞赏....它的紧急!!!! plz .....以下代码来自 https://developers.facebook.com/blog/ post / 498 /
when below form submitted the form action is set to $graph_url in my case when the album name, description, image database insertion operation completed after below code is executed i don't know how to set $graph_url before database insertion operation took place? below code post photo in facebook alubm successfully..... Any help is appreciated.... it's urgent!!!!! plz..... below code took from https://developers.facebook.com/blog/post/498/
$app_id = "391620680862988";
$app_secret = "62133e5c158218fc4339bf951252e1fa";
$album_name = 'My Album';
$album_description = 'My Album Desc';
$code = '';
//Obtain the access_token with publish_stream permission
if(!empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret;
$access_token = 'AAAFkLUuRzQwBAJ3BmabJCXvaYdag9TZBiDUEHf7XqThFSIZAUCHrFFnQrKu96lOoSPw21TNVvANMlM8kr3yzEv6IT8ZC1XedI9RNEnuxAZDZD';
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
推荐答案
使用Graph API上传照片。示例假设您已实例化$ facebook对象并拥有有效的会话。
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session.
1 - 当前用户的默认应用程序相册
此示例将照片上传到当前用户的默认应用程序相册。如果相册尚不存在,则会创建它。
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - 目标相簿
此示例将照片上传到特定相册。
This example will upload the photo to a specific album.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
您还可以在Facebook构造函数中指定:
you can also specify this in the Facebook constructor:
$facebook = new Facebook(array( 'appId' => 'ID', 'secret' => 'SECRET', 'fileUpload' => true, 'cookie' => true));
另一个选项 / p>
Another Option
$file = 'image.jpg';
$args = array(
'message' => 'Photo from application',
'access_token'=>urlencode('Your Access token'),
);
$args[basename($file)] = '@'.realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
curl_close ($ch);
EDIT:
这是我的网络服务功能。 / p>
This is my web service function.
function facebook_upload($fb_oauth_token,$item_image)
{
$file = $item_image;
$args = array(
'access_token'=>urlencode($fb_oauth_token)
);
$args[basename($file)] = '@'.($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
$response = json_decode($data,true);
//print_r(curl_error($ch));
//to check response
//if($response[id]>0){
// yes
//}
json_decode($data,true);
}
返回您的Web服务中的函数,如下所示:
Return function in your web service like this:
$fb_upload = $this->facebook_upload($fb_oauth_token,$item_image);
也许可以帮助你。
这篇关于上传照片的帖子到Facebook的相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!