Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Posting a Comment with Photo

Facebook API: Posting a Comment with Photo

10

Posting a comment with a photo attached has been around for a while on Facebook, but I seen any code examples explaining the process. As a result, I’ve created this brief tutorial to show how simple the API calls are. You can use the API to not only upload a new photo to a comment, but also link to existing photos uploaded elsewhere.

If you’re planning to comment on the user’s behalf, be sure to log them into the application first with publish_stream or publish_actions permission – this will allow you to comment for the user using the API. If you are planning to comment using a page, be sure to get the correct access_token for that page using the manage_pages permission.

Adding a Comment with Existing Photo

The easiest way to add a comment with a photo is to reference an existing image that’s already been uploaded, either to Facebook or elsewhere. The following code sets the basics of adding a comment with a simple message and an existing image from another website:

The $facebook->setAccessToken( ACCESS_TOKEN ); line is not needed if you’re planning on logging the user in beforehand. However, you can replace the ACCESS_TOKEN with a valid access_token for testing purposes.

Adding a Comment and Uploading a Photo

If you prefer to upload a photo instead of linking to an existing one, that’s possible too. But a small change in the code is required – instead of using the attachment_url parameter, you must use the source parameter. You can even choose to not leave a message at all, and just comment with a photo – just leave out the message parameter.

The below code demonstrates how to upload a photo as a comment with no message:

If you use the source parameter, remember to prefix the path with @ so it’s uploaded correctly to Facebook. Missing out the @ will cause a API error or post your comment with just the message.

The Result

The below screenshot demonstrates the result of the two API calls above. The first comment has a message and an image from an external location, while the second comment only has the meme.jpg image attached.

Facebook Comment with Photos

Facebook, Facebook Graph API, PHP

10 comments on “Facebook API: Posting a Comment with Photo”

  1. rasyid says:
    December 25, 2014 at 5:59 AM

    how about tagging friend on comments ?

    Reply
    • Niraj Shah says:
      December 30, 2014 at 12:32 PM

      You can only tag pages in comments. See the Publishing section on the Facebook Comment Documentation. You can use the @[page-id] syntax to tag a page.

      Reply
      • dighsal says:
        June 17, 2016 at 3:04 AM

        my photo comment is shown in my activity log, not wallpaper. how can photo comment appear in my wallpaper? i used photo path(source).

  2. dighsalsuan says:
    June 7, 2016 at 8:45 AM

    i am using a second one. so i can leave a photo comment. but it just appear in activity log. not wallpaper. how can i see the photo comment in wallpaper?

    Reply
    • Niraj Shah says:
      June 17, 2016 at 12:28 PM

      What do you mean by “wallpaper”? If you mean “cover photo”, I don’t think you can leave comments on cover photos.

      Reply
      • dighsal says:
        June 20, 2016 at 5:41 AM

        I mean timeline(newspeed also.). i can see photo comment using second code in my activity log. not timeline.

  3. dighsal says:
    June 22, 2016 at 7:51 AM

    i have a error.
    uncaught oauthexception(#100) invalid fbid. thrown in base_facebook.php.
    i was searching a long time but i don’t know that’s solution.
    do you know about this error?

    Reply
    • Niraj Shah says:
      June 22, 2016 at 9:47 AM

      Using base_facebook.php suggests you are using a really old version of the Facebook PHP SDK. Please upgrade to a newer version. Also, the variable fbid isn’t used in my code so I don’t know where you got that from.

      Reply
      • dighsal says:
        June 22, 2016 at 10:27 AM

        thank you so much for your reply. i fixed that error. but i have a other problem. i can’t not upload the photo comment in my wall. i can upload the photo comment in my activity log only. how can i fix this problem?

      • Niraj Shah says:
        July 6, 2016 at 11:48 AM

        Comments don’t usually appear on your “timeline” (or wall). They appear in your Activity Log (like if you made a comment yourself and not using the API), and on friend’s “Timeline”. This is the correct behaviour and not a bug or error.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bash Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Hacking HTML5 Input Sanitization iOS JavaScript jQuery Laravel 5 Laravel 5.2 Linux Mac OS NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy