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!







Manually debugging the SugarCRM REST API with cURL
The SugarCRM REST API is particularly difficult to debug; it’s full of bugs and inconsistencies in behaviour, and in general a malformed request simply returns an HTTP 200 code with the data string “null”. Nothing useful is posted in the application log.
To that end, I have found it essential to have cURL at my disposal to quickly push different query types into Sugar, in order to figure out which fields or parts of my API submission are causing problems.
I start by creating a couple of files containing the parameter and JSON data that I want to submit. First, we need to authenticate with the API, so we create a file like this one:
method=login &input_type=json &response_type=json &rest_data={ "user_auth":{ "user_name":"api-test-user", "version":".01", "username":"api-test-user", "password":"ca63fe5287c48bac307e6c50b6f43d43" }, "application_name":"Custom API" }This can then be submitted to the server like so:
Note that in this instance, the @ in the curl parameter means that we’re specifying a file to sumbit as data. If we wanted to submit a field directly, that would be done like this:
In order to get this format the JSON response nicely, I like to use the python JSON tool:
This provides a nicely formatted response like this:
{ "id": "88s7e18fbbrt5muo1mig1eov12", "module_name": "Users", "name_value_list": { "mobile_max_list_entries": { "name": "mobile_max_list_entries", "value": 10 }, "mobile_max_subpanel_entries": { "name": "mobile_max_subpanel_entries", "value": 3 }, "user_currency_id": { "name": "user_currency_id", "value": "-99" }, "user_currency_name": { "name": "user_currency_name", "value": "British Sterling" }, "user_decimal_seperator": { "name": "user_decimal_seperator", "value": "." }, "user_default_dateformat": { "name": "user_default_dateformat", "value": "d.m.Y" }, "user_default_team_id": { "name": "user_default_team_id", "value": "1" }, "user_default_timeformat": { "name": "user_default_timeformat", "value": "h:ia" }, "user_id": { "name": "user_id", "value": "4a4f2d1f-cbd4-b6b8-e3f1-102a322c6574" }, "user_is_admin": { "name": "user_is_admin", "value": true }, "user_language": { "name": "user_language", "value": "en_UK" }, "user_name": { "name": "user_name", "value": "api-test-user" }, "user_number_seperator": { "name": "user_number_seperator", "value": "," } } }The first part of this (the “id”) is the session ID which is required for subsequent queries.
Now you’re ready to submit general queries to the API, for example:
method=get_entry &input_type=json &response_type=json &rest_data={ "session":"88s7e18fbbrt5muo1mig1eov12", "module_name":"Accounts", "id":"76d9f1ab-312e-c673-d542-102a322c6574", "track_view":false }This will fetch the account with ID 76d9f1ab-312e-c673-d542-102a322c6574.
If you want to create an entry, you can test it like this:
method=set_entry &input_type=json &response_type=json &rest_data={ "session":"88s7e18fbbrt5muo1mig1eov12", "module_name":"Calls", "name_value_list":[ { "name":"parent_type", "value":"Contacts" }, { "name":"direction", "value":"Outbound" }, { "name":"date_start", "value":"2012-11-26 19:36:02" }, { "name":"parent_id", "value":"76d9f1ab-312e-c673-d542-102a322c6574" }, { "name":"assigned_user_id", "value":"4a4f2d1f-cbd4-b6b8-e3f1-102a322c6574" }, { "name":"name", "value":"Test call via API" }, { "name":"description", "value":"This is the description" } ], "track_view":false }I hope this is helpful!