Post Picture Facebook - 图形API - PHP

时间:2014-06-19 23:20:46

标签: php facebook facebook-graph-api facebook-php-sdk

我正在尝试通过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();

?>

由于

1 个答案:

答案 0 :(得分:2)

第1步:

您可以使用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


第2步:

/GET提出/{id}?fields=picture请求。

您将在回复中找到图片网址 - $response->{'picture'}


第3步:

将此图片网址用作picture API的/feed参数。