Search code examples
phplaravelfacebookfacebook-php-sdk

Adding photo so facebook php sdk post in laravel


I have a working call to my facebook php sdk in laravel, so that I make an ajax call, or multiple, and it hits my controller successfully posting my message to facebook.

I have a variable that gets the image url from the request as well, but I'm not sure how I need to change this to integrate it. Should I add my $imagePost to the $data array? And if so, would it still go to the feed url?

I'm just not sure how to simply add the photo into the equation

public function postToFacebook(Request $request)
{
    $campaignPost = $request->post;
    $imagePost = $request->url;
    $facebookPageID = $request->facebookPageID;
    $facebookToken = $request->facebookToken;

    $fb = App::make('SammyK\LaravelFacebookSdk\LaravelFacebookSdk');

    $data = array('message' => $campaignPost);
    $res = $fb->post($facebookPageID . '/feed/', $data, $facebookToken);
}

Solution

  • To create a media post, all you have to do is to pass source along with message. It should be an instance of FacebookFile entity. Calling fileToUpload('/path/to/file.jpg') with absolute image path also return an instance of FacebookFile.

    source