2

I am trying to create a page post link for my page in Facebook via Graph API, but first I need to upload a picture so I can use it in the post.

Can someone help me to post/upload this picture that are in my computer in Facebook, and then get its link to use it to create my post?

This is the PHP code that I am using to create the page post link. It is missing the picture, that I need to upload first from my computer and than use its 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();

?>

Thanks

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
user3697768
  • 359
  • 1
  • 7
  • 18

1 Answers1

2

Step 1:

You can upload the photo to the facebook with the API /photos (this will upload a photo to your album)-

API: \POST /me/photos

Parameters:

'source' => //The photo, encoded as form data,
'no_story' => true, // no story will be generated on uploading the photo
'message' => //optional

To get the image raw data-

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;

   }    
}

We'll use this $id in the next step.


Step 2:

Make the /GET request to /{id}?fields=picture.

You'll get the picture url in the response- $response->{'picture'}


Step 3:

Use this picture url as the picture parameter with the /feed API.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • Thanks, it seems to work perfectly! The only problem is that I am creating this code to create more than one post per time, and for that I need to upload more than one picture. Is there a way to get the pictures from my computer, only saying its location, like 'images/picture.jpeg', without the necessity to choose the pictures manually and press the button upload? Thanks again! – user3697768 Jun 22 '14 at 04:35
  • I hope you've figured this out – Sahil Mittal Jun 22 '14 at 08:31
  • Unfortunately not. Is there a way to do that? Without the necessity to use the form to choose the photo and press the button upload so I can upload more than one photo per time? Thanks – user3697768 Jun 22 '14 at 14:21
  • You want to upload the files without using the form, right? Go through this: http://stackoverflow.com/a/14035631/1343690 – Sahil Mittal Jun 22 '14 at 17:31
  • That's it, thanks! I am having only one more problem to get the $id from the photo posted, I am getting this error: Warning: json_decode() expects parameter 1 to be string, object given in C:\xampp\htdocs\faceads\facebook.php on line 64 Notice: Trying to get property of non-object in C:\xampp\htdocs\faceads\facebook.php on line 65 I tried to do the API request in the way I did to creaat the page link in the code I sent in the question, but I don't know how to get the ID from it. I posted a question to it here: http://bit.ly/1nzpNHU – user3697768 Jun 24 '14 at 00:42
  • Do you know why I am getting this error or how can I get the $id in my other question? – user3697768 Jun 24 '14 at 00:43
  • Why you need to get picture url ? , You can pass that photo_id to object_attachment directly when you are posting to /me/feed – Swap-IOS-Android Jul 21 '17 at 08:59