O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Alerting Yourself

Problem

I’d like to receive email alerts when my app is serving up 200ND (No Data) or 404 (Not Found) errors.

Solution

Since you can make calls from any server to Facebook through the Client, you can schedule a cron job on your server to use something like curl to request a page that runs the following code:

<?php
$hoursAhead = 3;
$appId = 12345;
$appName = 'My App name goes here';
$myEmail = 'me@mydomain.com';

$yesterday = mktime($hoursAhead,0,0,date("m"),date("d")-1,date("Y"));
$query = 'SELECT canvas_page_views_http_code_200ND, canvas_page_views
_http_code_404 from metrics WHERE end_time = ' . $yesterday . ' AND period = 86400;';
$metrics = $facebook->api_client->fql_query($query);

if($metrics && ($metrics[0]['canvas_page_views_http_code_200ND']
 > 0 || $metrics[0]['canvas_page_views_http_code_404'] > 0) ){
    $headers = 'From: do-not-reply@mydomain.com' . "\r\n" . 'X-Priority: 1';

    $message = $appName . ' had ' . $metrics[0]['canvas_page_views
_http_code_200ND'] . ' 200ND errors';
    $message .= ' and ' .  $metrics[0]['canvas_page_views_http_code_404']
 . ' 404 errors';
    $message .= ' on ' . date('F dS, Y', $yesterday) . '.';
    $message .= ' View more info at http://www.facebook.com/business/
insights/app.php?id=' . $appId . '&tab=httpreq';

    $success = mail($myEmail, '[Facebook Errors]: ' . $appName, $message, $headers);
}
?>

The little bit of configuration at the top will customize this for your app.

Discussion

Note that this code requires you to have an SMTP server running locally ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required