通过提供文件源,使用Graph API将图像发布到Facebook [英] Post Image to the Facebook using Graph API by Giving the source of the file
问题描述
我可以使用以下代码将图像上传到facebook acoount:
I can upload the image to the facebook acoount using below code:
<?php
include_once "facebook.php";
ini_set("display_errors",0);
//configuring application to post.
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_REDIRECT_URL";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_actions";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
但是,正如您所看到的,我正在使用表单标签和提交按钮以使用输入类型作为文件来发布图像. 但是我不想浏览图像. 我想删除所有用于浏览的代码,只想提供图像源,以将图像上传到Facebook帐户.
But as you can see, I am using form tag and submit button to post an image using the input type as File. But I don't want to browse for the image. I want to remove all the code for browsing and just want to give the source of the image to upload that image to facebook account.
类似以下内容:
<?php
include_once "facebook.php";
ini_set("display_errors",0);
//configuring application to post.
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_REDIRECT_URL";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_actions";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
//echo "Response: $response<br>";
parse_str($response, $params);
$access_token = $params['access_token'];
// echo "Access Token: $access_token<br>";
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?access_token=" .$access_token;
echo '<html><body>';
echo '<form action="'.$graph_url .'"method="POST">';
//echo '<input name="source" type="file"><br/><br/>'; Putting img tag instead of file
echo '<img src="'.get_template_directory_uri().'/download.php?f='.apply_filters('filter_if_add_to_cart',$image_link).'"><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
但是我不知道该怎么做. 你能帮我吗? 谢谢
But I don't know how to do that. Can you please help me with this? Thank You,
更新:我已经更新了我的代码.请参阅下面的更新代码:
Update: I have updated my code. Please see below for the updated Code:
<?php
include_once "facebook.php";
ini_set("display_errors",0);
$img_src = $_SESSION['imgSrc'];
//configuring application to post.
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_REDIRECT_URL";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_actions";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
//echo "Response: $response<br>";
parse_str($response, $params);
$access_token = $params['access_token'];
//Posting Image to The Facebook
$api_url = 'https://graph.facebook.com/v2.0/me/photos';
$attachment = array(
'url' => "{$img_src}",
'access_token' => $access_token
);
$api_response = UseCurl($api_url, $attachment);
$post_result = json_decode($api_response, TRUE);
print_r($post_result);
function UseCurl($url, $attachment){
$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_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
}
?>
推荐答案
如果服务器中有要发布的图像,则只需使用url
参数上传照片.就是这样-
If you have images in the server that you wish to post, you can simply use the url
parameter to upload the photo. Just like this-
$api_url = 'https://graph.facebook.com/v2.0/me/photos';
$attachment = array(
'url' => "{image-url}",
'access_token' => $access_token
);
$api_response = UseCurl($api_url, $attachment);
$post_result = json_decode($api_response, TRUE);
print_r($post_result);
function UseCurl($url, $attachment){
$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_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
这篇关于通过提供文件源,使用Graph API将图像发布到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!