Permalink
Browse files

add sample code for callback implementations

  • Loading branch information...
1 parent 482aa38 commit 6fb24bbd71da471a36ebd2d80266351c3c0cead3 @weizhu weizhu committed May 26, 2010
Showing with 72 additions and 0 deletions.
  1. +2 −0 README
  2. +43 −0 samples/asp.net/callback.ashx
  3. +27 −0 samples/php/callback.php
View
@@ -0,0 +1,2 @@
+This project contains code samples for using Facebook real-time updates.
+See http://developers.facebook.com/docs/api/realtime to documentation.
@@ -0,0 +1,43 @@
+<%@ WebHandler Language="C#" Class="SubCallback" %>
+/**
+ * Here is a sample C#/ASP.NET code to implement the callback endpont.
+ * If you have Visual Studio, just create a new item of type
+ * "Generic Handler" to create a .ashx file and copy this code.
+ */
+using System;
+using System.Web;
+using System.IO;
+
+public class SubCallback : IHttpHandler {
+ // This is secret verify_token that you should pass
+ // to Facebook when add/modify a subscription.
+ // Make sure to replace it with your own string!!
+ public const string VERIFY_TOKEN = "abc";
+
+ public void ProcessRequest (HttpContext context) {
+ context.Response.ContentType = "text/plain";
+ HttpRequest request = context.Request;
+ if (request.HttpMethod == "GET" &&
+ request.Params["hub.mode"] == "subscribe" &&
+ request.Params["hub.verify_token"] == VERIFY_TOKEN)
+ {
+ context.Response.ContentType = "text/plain";
+ context.Response.Write(request.Params["hub.challenge"]);
+ }
+ else if (request.HttpMethod == "POST")
+ {
+ StreamReader reader = new StreamReader(request.InputStream);
+ string jsonString = reader.ReadToEnd();
+ // Now use your favorate JSON decode and have fun with the data
+
+ }
+
+ }
+
+ public bool IsReusable {
+ get {
+ return false;
+ }
+ }
+
+}
@@ -0,0 +1,27 @@
+<?php
+/**
+ * This is sample subscription endpoint for using Facebook real-time update
+ * See http://developers.facebook.com/docs/api/realtime to additional
+ * documentation
+ */
+
+// Please make sure to REPLACE the value of VERIFY_TOKEN 'abc' with
+// your own secret string. This is the value to pass to Facebook
+// when add/modify this subscription.
+define('VERIFY_TOKEN', 'abc');
+$method = $_SERVER['REQUEST_METHOD'];
+
+// In PHP, dots and spaces in query parameter names are converted to
+// underscores automatically. So we need to check "hub_mode" instead
+// of "hub.mode".
+if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
+ $_GET['hub_verify_token'] == VERIFY_TOKEN) {
+ echo $_GET['hub_challenge'];
+} else if ($method == 'POST') {
+ $updates = json_decode(file_get_contents("php://input"), true);
+ // Replace with your own code here to handle the update
+ // Note the request must complete within 15 seconds.
+ // Otherwise Facebook server will consider it a timeout and
+ // resend the push notification again.
+ error_log('updates = ' . print_r($updates, true));
+}

0 comments on commit 6fb24bb

Please sign in to comment.