Posted By

MaRmAR on 09/15/14


Tagged

php notification api graph facebook


Versions (?)

Send Facebook notification


 / Published in: PHP
 

How to post a notification to Facebook's Graph API.

  1. // Use your APP ID and APP Secret from developer.facebook.com/apps
  2. define("FB_APP_TOKEN", $your_app_id . "|" . $your_app_secret );
  3.  
  4. /**
  5.  * Send Facebook notification using CURL
  6.  * @param string $recipientFbid Scoped recipient's FB ID
  7.  * @param string $text Text of notification (<150 chars)
  8.  * @param string $url Relative URL to use when user clicks the notification
  9.  * @return String
  10.  */
  11. function sendNotification($recipientFbid, $text, $url) {
  12. $href = urlencode($url);
  13. $post_data = "access_token=". FB_APP_TOKEN ."&template={$text}&href={$href}";
  14.  
  15. $curl = curl_init();
  16.  
  17. curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/v2.1/". $recipientFbid ."/notifications");
  18. curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  19. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  20. $data = curl_exec($curl);
  21. curl_close($curl);
  22.  
  23. return $data;
  24. }

Report this snippet  

You need to login to post a comment.