【发布时间】:2014-07-08 07:19:23
【问题描述】:
我可以使用以下代码将图像上传到 facebook 帐户:
<?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 帐户。
如下所示:
<?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>';
}
?>
但我不知道该怎么做。 你能帮我解决这个问题吗? 谢谢,
更新:我已经更新了我的代码。请参阅下面的更新代码:
<?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;
}
}
?>
【问题讨论】:
-
也就是说你的图片在服务器上对吧?
标签: php facebook facebook-graph-api facebook-php-sdk