我正在尝试通过Graph API在Facebook上为我的页面创建页面发布链接,但首先我需要上传图片,以便我可以在帖子中使用它。
有人可以帮我发布/上传Facebook中的电脑中的这张照片,然后获取其链接以使用它来创建我的帖子吗?
这是我用来创建页面帖子链接的PHP代码。它缺少图片,我需要首先从我的计算机上传,而不是使用其URL。
<?php
session_start();
require_once( 'Facebook/FacebookHttpable.php' );
require_once( 'Facebook/FacebookCurl.php' );
require_once( 'Facebook/FacebookCurlHttpClient.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookClientException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/FacebookPermissionException.php' );
require_once( 'Facebook/FacebookServerException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/GraphUser.php' );
use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookClientException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookPermissionException;
use Facebook\FacebookServerException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;
FacebookSession::setDefaultApplication('app','password');
$session = new FacebookSession('token');
$message = 'Test';
$link = "http://www.example.com.br";
$description = 'Description';
$name = 'Name';
$caption = 'Caption';
// $picture = Here I need to put the URL from the picture posted on Facebook
$request = new FacebookRequest(
$session,
'POST',
'/page_id/feed',
array (
'message' => $message,
'link' => $link,
'description' => $description,
'published' => 'false',
'name' => $name,
'caption' => $caption,
//'picture' => $picture,
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
?>
由于
答案 0 :(得分:2)
您可以使用API /photos
将照片上传到Facebook(这会将照片上传到您的相册) -
API :\POST /me/photos
参数:
'source' => //The photo, encoded as form data,
'no_story' => true, // no story will be generated on uploading the photo
'message' => //optional
获取图像原始数据 -
<强> main.php 强>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<p><label for="source">Photo</label><input type="file" name="source" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>
<强> upload.php的强>
if (!empty($_FILES)) {
$uploaddir = './uploads/'; // Upload folder
$uploadfile = $uploaddir . basename($_FILES['source']['name']);
if (move_uploaded_file($_FILES['source']['tmp_name'], $uploadfile)) {
$attachment = array(
'message' => "hi",
'source' => "@" . realpath($uploadfile),
'no_story' => true
);
$api_response = new FacebookRequest($session,'POST','/me/photos',$attachment);
$result = json_decode($api_response, TRUE);
$id = $result->id;
}
}
我们将在下一步中使用此$id
。
向/GET
提出/{id}?fields=picture
请求。
您将在回复中找到图片网址 - $response->{'picture'}
将此图片网址用作picture
API的/feed
参数。