Quickly see how many of your users are on Gravatar

A couple of the companies that I work with have wanted to evaluate the business case for integrating with Gravatar. In order to do this, we wanted to know how many of our users had Gravatar profiles, so I built this very simple PHP script which reads an input file containing email addresses (one per line), and outputs whether or not a Gravatar profile was found.

If you have PHP-CLI installed, you can simple execute this from a bash console.

<?php

# This is the list of email addresses
# that we're going to parse. Should be
# one per line.
$handle = fopen("uniques.txt", "r");

# Print a clear line
echo "\n";

# Iterate over each line in the file
while (!feof($handle)) {

  # Per Gravatar spec, trim the whitespace and
  # convert all to lowercase.
  $address = strtolower(trim(fgets($handle)));

  # Take the MD5 hash of the address
  $hash = md5($address);
  echo "$address,$hash,";

  # Perform the web lookup
  $web = curl_init("http://en.gravatar.com/${hash}.xml");
  curl_setopt($web, CURLOPT_RETURNTRANSFER, TRUE);
  $response = curl_exec($web);

  # Get the response code from the server.
  # For purposes of this process we don't care
  # about the contents of the result.
  $http_code = curl_getinfo($web, CURLINFO_HTTP_CODE);
  switch ($http_code) {
  case 200:
    # Response was 200, so print out the URL for
    # the profile.
    echo "http://en.gravatar.com/${hash}\n";
    break;
  case 404:
    # API response was 404, so no gravatar exists
    echo "NOT_FOUND\n";
    break;
  default:
    # Not one of the expected return codes from
    # the API server. Print an error.
    echo "ERROR($http_code)\n";
  }
  curl_close($web);
}

fclose($handle);

?>

I hope you find this helpful!

This entry was posted in APIs, General, Linux, PHP. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Michael
    Posted July 12, 2012 at 8:11 pm | Permalink

    Thanks for sharing this! I modified mine to show a running total like so:

    <?php

    # This is the list of email addresses
    # that we're going to parse. Should be
    # one per line.
    $handle = fopen("uniques.txt", "r");

    # Print a clear line
    echo "\n";
    $total = 0;
    $count = 0;
    # Iterate over each line in the file
    while (!feof($handle)) {

    # Per Gravatar spec, trim the whitespace and
    # convert all to lowercase.
    $address = strtolower(trim(fgets($handle)));
    $total++;
    # Take the MD5 hash of the address
    $hash = md5($address);

    # Perform the web lookup
    $web = curl_init("http://en.gravatar.com/${hash}.xml");
    curl_setopt($web, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($web);

    # Get the response code from the server.
    # For purposes of this process we don't care
    # about the contents of the result.
    $http_code = curl_getinfo($web, CURLINFO_HTTP_CODE);
    switch ($http_code) {
    case 200:
    # Response was 200, so print out the URL for
    # the profile.
    echo "\rhttp://en.gravatar.com/${hash}.xml";
    $count++;
    break;
    case 404:
    # API response was 404, so no gravatar exists
    echo "\rNOT_FOUND";
    break;
    default:
    # Not one of the expected return codes from
    # the API server. Print an error.
    echo "ERROR($http_code)\n";
    }
    echo "\n$count/$total";
    curl_close($web);
    }

    fclose($handle);

    • Timothy Creswick
      Posted October 29, 2012 at 10:21 pm | Permalink

      Hi that’s cool. Glad you found it useful :-)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What is 14 + 5 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple question (so we know that you are a human)