您好我是Facebook应用程序开发的新手,并在我的第一个应用程序上工作。 我的应用程序基本上应该在用户发布流上发布一张新照片,为此我首先要做的是图像创建过程就是这样:
<?php
//Image Processing:
$brandName = "nweing";
$imageUrl = "newimage.jpg";
$imageId = imagecreatefromjpeg($imageUrl);
$xCord = 5;
$yCord = 10;
$fontUrl = "GOTHIC.TTF";
$imageText = "Image Text";
echo $imageUrl . $imageId;
$imageColor = imagecolorallocate($imageId, 255, 255, 255);
$imageFont = 1;
$fontSize = 16;
//imagestring($imageUrl, $imageFont, $xCord, $yCord, $imageText, $imageColor);
imagefttext($imageId, $fontSize, 0, $xCord, $yCord, $imageColor, $fontUrl, $imageText);
header("Content-type: image/png");
imagepng($imageId);
imagedestroy($imageId);
?>
然后我将代码配置为指示here,以发布具有相应应用值的照片:
<?php
$app_id = "366325366724856";
$app_secret = "b14ac6d2b7345db259599b06983e881";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'My Album';
$album_description = 'My Album Description';
$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_stream";
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'];
// 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>';
} ?>
但是他的代码是要求用户通过html表单上传图片,但我希望我在早期代码中制作的图像能够发布。 所以我的问题是如何将该图像传递给Graph API端点以在用户流中发布。
感谢-你。
答案 0 :(得分:2)
您可以将图片保存到临时文件,然后使用this answer中的照片上传代码
$app_id = "366325366724856";
$app_secret = "b14ac6d2b7345db259599b06983e881";
$post_login_url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
$album_name = 'My Album';
$album_description = 'My Album Description';
$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_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
exit;
}
// Make sure you have read & write access to this folder
$tmpfile = "/tmp/" . md5(rand()) . ".png";
$brandName = "nweing";
$imageUrl = "newimage.jpg";
$imageId = imagecreatefromjpeg($imageUrl);
$xCord = 5;
$yCord = 10;
$fontUrl = "GOTHIC.TTF";
$imageText = "Image Text";
$imageColor = imagecolorallocate($imageId, 255, 255, 255);
$imageFont = 1;
$fontSize = 16;
imagefttext($imageId, $fontSize, 0, $xCord, $yCord, $imageColor, $fontUrl, $imageText);
imagepng($imageId,$tmpfile);
imagedestroy($imageId);
$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'];
// 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;
//upload photo
$args = array('message'=>'Photo caption',);
$args[basename($tmpfile)] = '@' . realpath($tmpfile);
$ch = curl_init();
$url = 'http://graph.facebook.com/' . $album_id . '/photos?access_token=' . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
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);
unlink($tmpfile);
//returns the photo id
print_r(json_decode($data,true));
?>